forked from dccharacter/AHRS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
237 lines (191 loc) · 5.73 KB
/
main.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
/**
******************************************************************************
* @file main.c
* @author MCD Application Team
* @version V1.1.0
* @date 20-September-2012
* @brief Main program body
******************************************************************************
* @attention
*
* <h2><center>© COPYRIGHT 2012 STMicroelectronics</center></h2>
*
* Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
* You may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.st.com/software_license_agreement_liberty_v2
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "main.h"
/** @addtogroup STM32F3-Discovery_Demo
* @{
*/
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
#define ABS(x) (x < 0) ? (-x) : x
/* Private variables ---------------------------------------------------------*/
RCC_ClocksTypeDef RCC_Clocks;
__IO uint32_t USBConnectTimeOut = 100;
__IO uint8_t UserButtonPressed = 0;
__IO uint8_t PrevXferComplete = 1;
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
int putchar(int ch);
void TimeBase_Do1ms (void);
void TimeBase_Do10ms (void);
void TimeBase_Do20ms (void);
void TimeBase_Do50ms (void);
void TimeBase_Do100ms (void);
void TimeBase_Do1s (void);
void TimeBase_Do5s (void);
void USB_Config (void);
#define SERVO
/**
* @brief Main program.
* @param None
* @retval None
*/
int main(void)
{
/* SysTick end of count event each 10ms */
RCC_GetClocksFreq(&RCC_Clocks);
uint32_t sysTickFrequency = 10000; //10KHz
SysTick_Config(RCC_Clocks.HCLK_Frequency / sysTickFrequency);
/* Initialize LEDs and User Button available on STM32F3-Discovery board */
STM_EVAL_LEDInit(LED3);
STM_EVAL_LEDInit(LED4);
STM_EVAL_LEDInit(LED5);
STM_EVAL_LEDInit(LED6);
STM_EVAL_LEDInit(LED7);
STM_EVAL_LEDInit(LED8);
STM_EVAL_LEDInit(LED9);
STM_EVAL_LEDInit(LED10);
STM_EVAL_PBInit(BUTTON_USER, BUTTON_MODE_EXTI);
Set_System();
#ifdef USART_USE_USB
/* Configure the USB */
USB_Config();
#endif //#ifdef USART_USE_USB
Demo_GyroConfig();
UpdateGyroBias();
Demo_CompassConfig();
#ifdef SERVO
configureServo();
#endif //#ifdef SERVO
myPrintf("\f\f");
GyroExtiConfig();
readAllSensors(GyroTempBuffer, AccTempBuffer, MagTempBuffer);
GyroDRDFlag = 1;
/* Infinite loop */
while (1)
{
timeBase1ms (TimeBase_Do1ms);
timeBase10ms (TimeBase_Do10ms);
timeBase20ms (TimeBase_Do20ms);
timeBase50ms (TimeBase_Do50ms);
timeBase100ms (TimeBase_Do100ms);
timeBase1s (TimeBase_Do1s);
timeBase5s (TimeBase_Do5s);
if (GyroDRDFlag) {
GyroDRDFlag = 0;
updateQuaternions(QuaternionsBuffer);
}
}
}
void TimeBase_Do1ms (void) {
}
void TimeBase_Do10ms (void) {
}
void TimeBase_Do20ms (void) {
#ifdef USB_JOYSTICK
static uint8_t Mouse_Buffer[7] = {0};
getEulerAsArray(Mouse_Buffer);
if (STM_EVAL_PBGetState(BUTTON_USER) == RESET) {
Mouse_Buffer[6] = 0; } else {
Mouse_Buffer[6] = 1; }
/* Reset the control token to inform upper layer that a transfer is ongoing */
PrevXferComplete = 0;
/* Copy mouse position info in ENDP1 Tx Packet Memory Area*/
USB_SIL_Write(EP1_IN, Mouse_Buffer, 7);
/* Enable endpoint for transmission */
SetEPTxValid(ENDP1);
#endif
#ifdef SERVO
getEulerAngles(euler);
setServoPositions(euler);
#endif //#ifdef SERVO
}
void TimeBase_Do50ms (void) {
#ifdef OUT_QUATERNION
STM_EVAL_LEDOn(LED10);
myPrintf("%f,%f,%f,%f,\n", QuaternionsBuffer[0], QuaternionsBuffer[1], QuaternionsBuffer[2], QuaternionsBuffer[3]);
STM_EVAL_LEDOff(LED10);
#endif //#ifdef OUT_QUATERNION
#ifdef OUT_EULER
myPrintf("%f,%f,%f,\r\n", euler[0], euler[1], euler[2]);
#endif //#ifdef OUT_EULER
}
void TimeBase_Do100ms (void) {
STM_EVAL_LEDToggle(LED5);
}
void TimeBase_Do1s (void) {
STM_EVAL_LEDToggle(LED7);
}
void TimeBase_Do5s (void) {
STM_EVAL_LEDToggle(LED9);
}
#ifdef USART_USE_USART
int putchar(int ch) {
while (!USART_GetFlagStatus(USART2, USART_FLAG_TC));
USART_SendData(USART2, ch);
return ch;
}
#endif //#ifdef USART_USE_USART
#ifdef USART_USE_USB
/**
* @brief Configure the USB.
* @param None
* @retval None
*/
void USB_Config(void)
{
Set_USBClock();
USB_Interrupts_Config();
USB_Init();
//!!!çàâèñíåò, ïîêà íå ïîäêë. usb
//while (bDeviceState != CONFIGURED)
//{}
}
#endif //#ifdef USART_USE_USB
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t* file, uint32_t line)
{
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* Infinite loop */
while (1)
{
}
}
#endif
/**
* @}
*/
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/