-
Notifications
You must be signed in to change notification settings - Fork 12
/
main.c
executable file
·130 lines (122 loc) · 3.68 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
#include "ch.h"
#include "hal.h"
#include "stm32f4xx_conf.h"
#include "comm_usb.h"
#include "comm.h"
#include "led_rgb.h"
#include "encoder.h"
#include "controller.h"
#include "chprintf.h"
#include <stdio.h>
#include <string.h>
#include "math.h"
#include "gpio.h"
#include "config.h"
#include "packet.h"
#include "scope.h"
static THD_WORKING_AREA(led_update_wa, 1024);
static THD_FUNCTION(led_update, arg) {
(void)arg;
chRegSetThreadName("LED update");
Config *config = config_get_configuration();
for(;;) {
ControllerFault fault = controller_get_fault();
if (fault == NO_FAULT)
{
if (packet_connect_event())
{
led_rgb_set(0x00FFFF);
chThdSleepMilliseconds(100);
led_rgb_set(0);
chThdSleepMilliseconds(100);
led_rgb_set(0x00FFFF);
chThdSleepMilliseconds(100);
led_rgb_set(0);
chThdSleepMilliseconds(100);
led_rgb_set(0x00FFFF);
chThdSleepMilliseconds(100);
led_rgb_set(0);
chThdSleepMilliseconds(100);
}
else if (controller_get_state() == RUNNING)
{
float command = controller_get_command_current();
if (command > 0.0) // Forward
{
float pcnt = command / config->maxCurrent;
led_rgb_set(0x00FF00);
if (pcnt >= 1.0 / 400)
chThdSleepMilliseconds((int)(pcnt * 400));
if (pcnt < 1.0)
{
led_rgb_set(0);
chThdSleepMilliseconds((int)((1 - pcnt) * 400));
}
}
else if (command < 0.0) // Reverse
{
float pcnt = -command / config->maxCurrent;
led_rgb_set(0xFF0000);
if (pcnt >= 1.0 / 400)
chThdSleepMilliseconds((int)(pcnt * 400));
if (pcnt < 1.0)
{
led_rgb_set(0);
chThdSleepMilliseconds((int)((1 - pcnt) * 400));
}
}
else // Neutral
{
led_rgb_set(0xFF5500);
chThdSleepMilliseconds(350);
led_rgb_set(0);
chThdSleepMilliseconds(50);
}
}
else if (comm_usb_serial_is_active())
{
led_rgb_set(0x0000FF);
chThdSleepMilliseconds(250);
led_rgb_set(0);
chThdSleepMilliseconds(250);
}
else
{
led_rgb_set(0x0000FF);
chThdSleepMilliseconds(500);
led_rgb_set(0);
chThdSleepMilliseconds(500);
}
}
else
{
for (int i = 0; i < (int)fault; i++)
{
led_rgb_set(0xFF1100);
chThdSleepMilliseconds(250);
led_rgb_set(0);
chThdSleepMilliseconds(250);
}
chThdSleepMilliseconds(250);
}
}
}
int main(void) {
halInit();
chSysInit();
gpio_init();
config_init();
chThdSleepMilliseconds(250);
led_rgb_init();
encoder_init();
scope_init();
controller_init();
chThdCreateStatic(led_update_wa, sizeof(led_update_wa), NORMALPRIO, led_update, NULL);
comm_init();
comm_usb_serial_init();
for(;;)
{
/*controller_print();*/
chThdSleepMilliseconds(10);
}
}