-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjetson.c
More file actions
208 lines (184 loc) · 5.48 KB
/
jetson.c
File metadata and controls
208 lines (184 loc) · 5.48 KB
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
#include "jetson.h"
/**
* @addtogroup Jetcom
* @{
*/
thread_t *jetson_write_thread;
thread_t *jetson_save_thread;
uint16_t ball_degree;
int16_t robot_speed;
int16_t robot_azimuth;
uint8_t received_command;/**< command received from jetson*/
uint16_t jetson_degree;
int16_t jetson_speed;
int16_t jetson_azimuth;
msg_t send_jetson(msg_t command){
return chMsgSend(jetson_write_thread, command);
}
static THD_WORKING_AREA(waJetsonWriteThread, 128);
/**
* @brief
* ## Jetson write thread
* Waits for the command, which then correctly sends to Jetson.
* @param JetsonWriteThread
* @param arg
* @returns
*
*
*/
static THD_FUNCTION(JetsonWriteThread, arg) {
(void)arg;
thread_t *master;
int16_t command;
uint16_t i = 0;
sdPut(JETSON_SERIAL, INIT_COMMAND);
while (1) {
master = chMsgWait();
command = chMsgGet(master);
chMsgRelease(master, MSG_OK);
if(command == CALIBRATION_VALUES){
calibration_memory(JETSON_LOAD_CALIBRATION);
sdPut(JETSON_SERIAL, LINE_CALIBRATION_COMMAND);
for(i = 0; i < NUMBER_OF_SENSORS; i++){
sdPut(JETSON_SERIAL, line_calibration_values_out[i]);
//sdPut(JETSON_SERIAL, *((uint8_t*)&(line_calibration_values_out[i])+1));
//sdPut(JETSON_SERIAL, *((uint8_t*)&(line_calibration_values_out[i])+0));
}
} else {
sdPut(JETSON_SERIAL, command);
}
}
}
static THD_WORKING_AREA(waJetsonReadThread, 128);
/**
* @brief
* ## Jetson read thread
* Receives all commands from Jetson and sends them for further processing.
* @param JetsonReadThread
* @param arg
* @returns
*
*
*/
static THD_FUNCTION(JetsonReadThread, arg) {
(void)arg;
int16_t i;
while (1) {
received_command = sdGet (JETSON_SERIAL);
//sdPut(&SD3, received_command);
if(received_command == JETSON_MOVE_COMMAND){
jetson_degree = sdGet(JETSON_SERIAL);
//sdPut(&SD3, jetson_degree);
jetson_speed = (int16_t)sdGet(JETSON_SERIAL) - 100;
//sdPut(&SD3, jetson_speed);
jetson_azimuth = (int16_t)sdGet(JETSON_SERIAL) - 100;
//sdPut(&SD3, jetson_azimuth);
if(jetson_speed < 0){
jetson_degree += 180;
jetson_speed = -jetson_speed;
jetson_degree %= 360;
}
chMsgSend(jetson_save_thread, SAVE_JETSON_VALUES);
} else if(received_command == LINE_CALIBRATION_COMMAND){
send_to_line_mailbox(CALIBRATION);
//chprintf((BaseSequentialStream *)&SD4, "jetson: som tu\n");
} else if(received_command == INIT_COMMAND){
//led_command(FOURTH_ON);
for(i = 0; i < NUMBER_OF_SENSORS; i++){
line_calibration_values_in[i] = sdGet(JETSON_SERIAL);
//line_calibration_values_in[i] = (line_calibration_values_in[i] << 8) | sdGet (JETSON_SERIAL);
//chprintf((BaseSequentialStream *)&SD4, "jetson: nacital som %d\n", line_calibration_values_in[i]);
}
calibration_memory(JETSON_SAVE_CALIBRATION);
send_to_line_mailbox(LOAD_JETSON_CALIBRATION);
} else if(received_command == START_COMMAND){
send_to_main_mailbox(START);
} else if(received_command == STOP_COMMAND){
send_to_main_mailbox(STOP);
} else if(received_command == DRIBLER_COMMAND){
send_to_main_mailbox(DRIBLER_ON_OFF+sdGet(JETSON_SERIAL));
} else if(received_command == KICK_COMMAND){
send_to_main_mailbox(KICK);
//chprintf((BaseSequentialStream *)&SD4, "jetson: kick\n");
} else if(received_command == START_ULTRASONIC_COMMAND){
//led_command(FIRST_ON);
send_to_ultrasonic_mailbox(START_ULTRASONIC);
} else if(received_command == STOP_ULTRASONIC_COMMAND){
send_to_ultrasonic_mailbox(STOP_ULTRASONIC);
}
}
}
msg_t get_jetson_values(void){
return chMsgSend(jetson_save_thread, LOAD_JETSON_VALUES);
}
static THD_WORKING_AREA(waJetsonSaveThread, 128);
/**
* @brief
* ## Jetson save thread
* Makes copies of movement values from [Jetson read thread]
* (@ref THD_FUNCTION(JetsonReadThread, arg)) and makes them available
* from outside with function @ref get_jetson_values.
* @param JetsonSaveThread
* @param arg
* @returns
*
*
*/
static THD_FUNCTION(JetsonSaveThread, arg) {
(void)arg;
thread_t *master;
msg_t command;
uint16_t copy_jetson_degree = 0;
int16_t copy_jetson_speed = 0;
int16_t copy_jetson_azimuth = 0;
while (1) {
master = chMsgWait();
command = chMsgGet(master);
if(command == SAVE_JETSON_VALUES){
copy_jetson_degree = jetson_degree;
copy_jetson_speed = jetson_speed;
copy_jetson_azimuth = jetson_azimuth;
} else if(command == LOAD_JETSON_VALUES){
ball_degree = copy_jetson_degree;
robot_speed = copy_jetson_speed;
robot_azimuth = copy_jetson_azimuth;
}
chMsgRelease(master, MSG_OK);
}
}
static THD_WORKING_AREA(waButtonGoThread, 128);
/**
* @brief
* ## Button go thread
* check go button on top of robot
* @param ButtonGoThread
* @param arg
* @returns
*
*
*/
static THD_FUNCTION(ButtonGoThread, arg) {
(void)arg;
int8_t old_state = palReadPad(GPIOD, 14);
int8_t new_state;
while (1) {
if(palReadPad(GPIOD, 14)){
new_state = 100;
}
if((old_state == 0 && new_state > 0) || (old_state > 0 && new_state == 0)){
old_state = new_state;
send_to_main_mailbox(START_STOP);
}
chThdSleepMilliseconds(1);
new_state--;
}
}
void jetson_init(void) {
jetson_write_thread = chThdCreateStatic(waJetsonWriteThread, sizeof(waJetsonWriteThread), NORMALPRIO, JetsonWriteThread, NULL);
jetson_save_thread = chThdCreateStatic(waJetsonSaveThread, sizeof(waJetsonSaveThread), NORMALPRIO, JetsonSaveThread, NULL);
chThdCreateStatic(waJetsonReadThread, sizeof(waJetsonReadThread), NORMALPRIO, JetsonReadThread, NULL);
//chThdCreateStatic(waButtonGoThread, sizeof(waButtonGoThread), NORMALPRIO, ButtonGoThread, NULL);
}
/**
* @}
*/