/
ABRobot.c
298 lines (284 loc) · 8.01 KB
/
ABRobot.c
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/*============================================================================*
___ ___ ___ _ _ *
/ \ | _ ) | _ \ ___ | |__ ___ | |_ o O O *
| - | | _ \ | / / _ \ | '_ \ / _ \ | _| o *
|_|_| |___/ |_|_\ \___/ |_.__/ \___/ _\__| TS__[O] *
_|"""""|_|"""""|_|"""""|_|"""""|_|"""""|_|"""""|_|"""""| {======| *
"`-0-0-'"`-0-0-'"`-0-0-'"`-0-0-'"`-0-0-'"`-0-0-'"`-0-0-'./o--000' *
* *
* Auto Balance Robot controller firmware for Nuvoton Cortex M4 series *
* *
* Written by T.L. Shen for Nuvoton Technology. *
* tlshen@nuvoton.com/tzulan611126@gmail.com *
* *
*============================================================================*
*/
#include <stdio.h>
#include <stdlib.h>
#ifdef M451
#include "M451Series.h"
#else
#include "Driver\DrvGPIO.h"
#include "Driver\DrvSYS.h"
#include "Driver\DrvUART.h"
#include "Driver\DrvUSB.h"
#include "Driver\DrvI2C.h"
#endif
#include "Def.h"
#include "FlashCtrl.h"
#include "NVT_I2C.h"
#include "Timer_Ctrl.h"
#include "I2CDev.h"
#include "retarget.h"
#include "AHRSLib.h"
#include "Sensors.h"
#include "Report.h"
#include "Calibrate.h"
#include "LED.h"
#include "battery.h"
#include "motors.h"
#include "gps.h"
#ifdef OPTION_RC
#include "RC.h"
#include "RC_ssv.h"
#include "ssv7241.h"
#include "stabilizer.h"
#include "controller.h"
int freq=0;
#endif
#define MAG_INTERVAL 4
void setupSystemClock()
{
#ifdef M451
SYS_UnlockReg();
/* Enable HIRC clock */
CLK_EnableXtalRC(CLK_PWRCTL_HIRCEN_Msk);
/* Waiting for HIRC clock ready */
CLK_WaitClockReady(CLK_STATUS_HIRCSTB_Msk);
/* Switch HCLK clock source to HIRC */
CLK_SetHCLK(CLK_CLKSEL0_HCLKSEL_HIRC, CLK_CLKDIV0_HCLK(1));
/* Set core clock as PLL_CLOCK from PLL and SysTick source to HCLK/2*/
CLK_SetCoreClock(SYSTEM_CLOCK);
CLK_SetSysTickClockSrc(CLK_CLKSEL0_STCLKSEL_HCLK_DIV2);
SYS_LockReg();
#else
uint32_t u32PllCr;
uint16_t i;
UNLOCKREG();
DrvSYS_SetOscCtrl(E_SYS_OSC22M, 1);
while (DrvSYS_GetChipClockSourceStatus(E_SYS_OSC22M) != 1);
DrvSYS_SelectPLLSource(E_SYS_INTERNAL_22M);
u32PllCr = DrvSYS_GetPLLContent(E_SYS_INTERNAL_22M, SYSTEM_CLOCK);
/*Delay for 12M or 22M stable*/
for (i=0;i<10000;i++);
DrvSYS_SetPLLContent(u32PllCr);
SYSCLK->PLLCON.OE = 0;
SYSCLK->PLLCON.PD = 0;
/*Delay for PLL stable*/
for (i=0;i<10000;i++);
/* Change HCLK clock source to be PLL. */
DrvSYS_SelectHCLKSource(2);
LOCKREG(); // Lock the protected registers
#endif
}
void setupCommandUART()
{
/* Enable peripheral clock */
CLK_EnableModuleClock(UART0_MODULE);
/* Peripheral clock source */
CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UARTSEL_HIRC, CLK_CLKDIV0_UART(1));
/* Set PD multi-function pins for UART0 RXD, TXD */
SYS->GPD_MFPL = SYS_GPD_MFPL_PD0MFP_UART0_RXD | SYS_GPD_MFPL_PD1MFP_UART0_TXD;
/* Reset UART module */
SYS_ResetModule(UART0_RST);
/* Configure UART0 and set UART0 Baudrate */
UART_Open(UART0, 115200);
}
void setupUART()
{
#ifdef M451
setupCommandUART();
#else
STR_UART_T param;
DrvGPIO_InitFunction(E_FUNC_UART1);
DrvSYS_SelectIPClockSource(E_SYS_UART_CLKSRC,3);
param.u32BaudRate = UART_BAUD_RATE;
param.u8cDataBits = DRVUART_DATABITS_8;
param.u8cStopBits = DRVUART_STOPBITS_1;
param.u8cParity = DRVUART_PARITY_NONE;
param.u8cRxTriggerLevel = DRVUART_FIFO_1BYTES;
param.u8TimeOut = 0;
DrvUART_Open(UART_PORT1, ¶m);
#endif
printf("Version:%d\n", VERSION_CODE);
}
void setup()
{
setupSystemClock();
setup_system_tick(SYSTEM_TICK_FREQ);
setupUART();
#ifdef GPS
setupGPS();
#endif
I2C_Init();
FlashInit();
UpdateBoardVersion(false);
#ifdef OPTION_RC
RC_Init();
if(IsSSVConnected())
Battery_Init();
LED_Init();
TIMER_Init();
stabilizerInit();
#endif
#ifdef ABROBOT
ABRobotMotorInit();
#endif
nvtAHRSInit();
SensorsInit();
ChronographSet(ChronMain);
}
void CommandProcess()
{
// Read incoming control messages
if (Serial_available() >= 2)
{
char start=Serial_read();
if (start == '@') {// Start of new control message
int command = Serial_read(); // Commands
if (command == 'h') {//Hook AHRS Stack Device
// Read ID
char id[2];
id[0] = GetChar();
id[1] = GetChar();
// Reply with synch message
printf("@HOOK");
Serial_write(id, 2);
}
else if (command == 'v') {//Check Version
CheckVersion();
}
else if (command == 'c') {// A 'c'calibration command
SensorCalibration();
}
else if (command == 'b') {// 'b'lock erase flash
FlashControl();
}
else if (command == 'p') {// Set 'p'id command
SetPID();
}
else if (command == 'm') {// Set report 'm'ode
char mode = GetChar();
if (mode == 'e') {// Report AHRS by 'e'uler angle
report_mode = REPORT_AHRS_EULER;
}
else if (mode == 'q') {// Report // Report AHRS by 'q'quaternion
report_mode = REPORT_AHRS_QUATERNION;
}
else if (mode == 'r') {// Report sensor 'r'aw data
report_mode = REPORT_SENSORS_RAW;
}
else if (mode == 'c') {// Report sensor 'c'alibrated data
report_mode = REPORT_SENSORS_CALIBRATED;
}
else if (mode == 'm') {// Report 'm'otor power distribution
report_mode = REPORT_MOTOR_POWER;
}
else if (mode == 'v') {// Report 'v'elocity
report_mode = REPORT_VELOCITY;
}
else if (mode == 's') {// Report 's'tatus
report_status();
}
else if (mode == 'p') {// Report controller 'p'id
char type = GetChar();
if (type == 'p') // 'p'id
report_mode = REPORT_PID;
else if (type == 'r') //'r'ate pid
report_mode = REPORT_RATE_PID;
else if (type == 'a') //'a'ltitude hold pid
report_mode = REPORT_ALTHOLD_PID;
}
}
else if (command == 'f') {// Set report 'f'ormat
char format = GetChar();
if (format == 'b') {// Report 'b'inary format
report_format = REPORT_FORMAT_BINARY;
}
else if (format == 't') {// Report 't'ext format
report_format = REPORT_FORMAT_TEXT;
}
}
else if (command == 's') {// 's'tream output control
char mode = GetChar();
if (mode == 's') {// 's'tart stream
stream_mode = STREAM_START;
}
else if (mode == 'p') {// 'p'ause stream
stream_mode = STREAM_PAUSE;
}
else if (mode == 't') {// 't'oggle stream
if(stream_mode==STREAM_START)
stream_mode = STREAM_PAUSE;
else
stream_mode = STREAM_START;
}
}
}
else {
if (report_format == REPORT_FORMAT_TEXT) {
printf("Unknown command.\n");
}
} // Skip character
}
}
// Main Control loop
void loop()
{
static uint32_t nextTick = 0;
while(getTickCount()<nextTick);
nextTick = getTickCount()+TICK_FRAME_PERIOD;
CommandProcess();
#ifdef GPS
GPSCommandProcess();
#endif
SensorsRead(SENSOR_ACC|SENSOR_GYRO|SENSOR_MAG|SENSOR_BARO,1);
#ifdef ABROBOT
SensorsRead(SENSOR_HALL,1);
#endif
#ifdef OPTION_RC
if(IsSSVConnected()) {
ssv_rc_update();
if(getTickCount()%1000)
UpdateBattery();
}
if(ChronographRead(ChronRC)>= OUTPUT_RC_INTERVAL) {
SensorsDynamicCalibrate(SENSOR_GYRO|SENSOR_MAG);
ChronographSet(ChronRC);
computeRC();
armDetect();
}
#endif
if(getMagMode()||!(GetSensorCalState()&(1<<MAG)))
nvtUpdateAHRS(SENSOR_ACC|SENSOR_GYRO|SENSOR_MAG);
else
nvtUpdateAHRS(SENSOR_ACC|SENSOR_GYRO);
if((GetFrameCount()%18)==0)
report_sensors();
IncFrameCount(1);
#ifdef OPTION_RC
if(GetFrameCount()==MOTORS_ESC_DELAY)
motorsStart();
stabilizer();
if((GetFrameCount()%12)==0)
UpdateLED();
#endif
}
/*-----------------------------------------------------------------------------------*/
/* Fly Controller Main Function */
/*-----------------------------------------------------------------------------------*/
int32_t main (void)
{
setup();
while(true) loop();
}