-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
238 lines (216 loc) · 6.78 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/******** Kurione2 main program ********/
#include "mbed.h"
#include "Communication.hpp" // 通信ライブラリ
#define SERIAL_DEBUG
//#define DISABLE_WDT // WDTを使わない場合(標準でON)
#define THRUSTER_HH 1700;
#define THRUSTER_HL 1600;
#define THRUSTER_NN 100;
#define THRUSTER_LH 1400;
#define THRUSTER_LL 1300;
#define SERVO_H 2000;
#define SERVO_N 1500;
#define SERVO_L 1000;
//Serial pc(USBTX, USBRX);
PinName rs485_txd = PA_9;
PinName rs485_rxd = PA_10;
//PinName rs485_nre = PA_12;
//PinName rs485_de = PA_11;
UnbufferedSerial debugger(PA_9, PA_10);
//Serial debugger(USBTX, USBRX);
PwmOut motors[] ={
PwmOut (PA_6_ALT0), // TIM16_CH1
PwmOut (PA_7_ALT1), // TIM17_CH1
PwmOut (PA_8), // TIM1_CH1
PwmOut (PB_5), // TIM3_CH2
PwmOut (PA_3), // TIM15_CH2
PwmOut (PB_0_ALT0) // TIM3_CH3
};
DigitalOut LED_right_green(PB_7);
DigitalOut LED_left_red(PB_6);
DigitalOut emergency_signal(PA_1);
AnalogIn v_sense(PA_4);
AnalogIn i_sense(PA_5); // フィルタ:1k, 0.1uF, 10k
const float ISENSE_OFFSET_VOLTAGE = 0.47f;
const float VSENSE_OFFSET_VOLTAGE = 3.0f;
float voltage,current; // 電圧,電流値(float計算値)
int voltage_int, current_int; // 電圧電流値(int修正値)
int wdt_cnt; // ウオッチドックタイマ変数(地上から信号無かったら強電供給を止める)
const int WDT_MAX = 100;//50; // WDT発動値 → 高速か
//Communication communication(rs485_txd,rs485_rxd,rs485_de,rs485_nre);
//Communication communication(rs485_txd,rs485_rxd,PF_0,PF_1);
Communication communication(USBTX,USBRX,PF_0,PF_1);
int u[6];
//bool is_upd[6];
void updateMotors();
void updateCommunication();
int measureBattery(); // バッテリーの電圧,電流を測定
void sendBatteryData(void); // バッテリーの各種情報をRS485で送る
void pcFlip();
int main()
{
u[0] = THRUSTER_NN;
u[1] = THRUSTER_NN;
u[2] = THRUSTER_NN;
u[3] = SERVO_N;
u[4] = SERVO_N;
u[5] = SERVO_N;
//is_upd[6] = {true,true,true,true,true,true}; //信号アップデートが必要か
//wait_ms(5000);
communication.init(Communication::ROLL_MAIN);
//pc.baud(115200);
//pc.printf("start !\n");
/*
motors[0].pulsewidth_us(1000);
motors[1].pulsewidth_us(1100);
motors[2].pulsewidth_us(1200);
motors[3].pulsewidth_us(1300);
motors[4].pulsewidth_us(1400);
motors[5].pulsewidth_us(1500);
*/
updateMotors();
//debugger.putc(int(communication.freshSerialBuffer()));
wait_us(10000);
int cnt = 0;
LED_left_red = 1;
LED_right_green = 0;
//LED_right_green = 1;
#ifdef SERIAL_DEBUG
debugger.attach(pcFlip, UnbufferedSerial::RxIrq);
#endif
wdt_cnt = 0;
//emergency_signal = 1;
emergency_signal = 0;
while(1) {
measureBattery();
updateCommunication();
updateMotors();
#ifdef SERIAL_DEBUG
cnt++; // シリアルデバッグ
if (cnt>=80){
if (cnt<=85){
//debugger.printf("u[%d]:%4d, ",cnt-80, u[cnt-80]);
}
if (cnt >= 100){
//debugger.printf("\n");
cnt = 0;
}
}
#endif
#ifndef DISABLE_WTD
wdt_cnt++;
#endif
if (wdt_cnt>WDT_MAX) {
wdt_cnt = 0;
//emergency_signal = 1; // WDT発動し,強電供給を停止
LED_right_green = 0;
u[0] = THRUSTER_NN;
u[1] = THRUSTER_NN;
u[2] = THRUSTER_NN;
u[3] = SERVO_N;
u[4] = SERVO_N;
u[5] = SERVO_N;
updateMotors();
}
//wait_ms(20);
wait_us(10000);
}
}
int measureBattery(void) {
float i,v;
v = v_sense; // 測定生値
i = i_sense;
voltage = v/0.011f+VSENSE_OFFSET_VOLTAGE;
current = 25*(3.3f*i-ISENSE_OFFSET_VOLTAGE);
voltage_int = (int)(voltage*10); // 10*V
current_int = (int)(current*10); // 10*I
if((voltage_int<0)||(voltage_int>1000)||(current_int<-100)||(current_int>2000)){
return -1; // 値が外れている場合
}else{
return 0;
}
}
void updateMotors(void) {
for(int i = 0; i<6; i++){
//if (is_upd[i])
motors[i].pulsewidth_us(u[i]);
}
}
void updateCommunication(void) {
//debugger.printf("update! \n");
if (communication.flag_rdat_check){
int s = communication.decode();
//debugger.printf("here: %d \n", s);
//if ((s>10)||(s<-10)){
// debugger.printf("reset !! \n", s);
// NVIC_SystemReset();// ないと動かない(2022/08/07)
//}
communication.flag_rdat_check = false;
switch (communication.receive_command_dat){
case Command::SIGNAL_LAND_TO_MAIN:
for (int i = 0; i<6; i++){
u[i] = communication.receive_num_dat.front();
communication.receive_num_dat.erase(communication.receive_num_dat.begin());
//communication.receive_num_dat.pop_back();
}
wdt_cnt = 0; // wdtカウントをリセット
//wait_ms(10);
wait_us(2000); // 待ち時間高速化
sendBatteryData(); // 10ms後にバッテリー情報送信
LED_right_green = 1;
break;
case Command::POWER_SUPPLY_STOP:
//emergency_signal = 1; // 強電供給停止
break;
case Command::POWER_SUPPLY_START:
//emergency_signal = 0; // 強電供給開始
break;
case Command::BATT_RESET:
NVIC_SystemReset();
break;
default:
break;
}
}
}
void sendBatteryData(void) {
communication.freshSendNumDat();
communication.send_command_dat = Command::SIGNAL_BATT_TO_LAND;
communication.send_num_dat.push_back(voltage_int); // 電圧情報登録
communication.send_num_dat.push_back(current_int); // 電流情報登録
communication.send_num_dat.push_back(emergency_signal==0); // 強電供給状況
communication.encode();
communication.sendDat();
}
void pcFlip(void) {
char c ;
debugger.read(&c,1);
switch(c) {
case '0':
u[0] = THRUSTER_HL;
break;
case '1':
u[1] = THRUSTER_HL;
break;
case '2':
u[2] = THRUSTER_HL;
break;
case '3':
u[3] = SERVO_H;
break;
case '4':
u[4] = SERVO_H;
break;
case '5':
u[5] = SERVO_H;
break;
default:
u[0] = THRUSTER_NN;
u[1] = THRUSTER_NN;
u[2] = THRUSTER_NN;
u[3] = SERVO_N;
u[4] = SERVO_N;
u[5] = SERVO_N;
break;
}
}