5 * @brief Display-ORTE bridge
7 * Copyright: (c) 2010 CTU Dragons
8 * CTU FEE - Department of Control Engineering
15 #include <roboorte_robottype.h>
16 //#include <pthread.h>
24 struct sercom_data* sercom;
26 /* Subtract the `struct timespec' values X and Y,
27 storing the result in RESULT (result = x - y).
28 Return 1 if the difference is negative, otherwise 0. */
31 timespec_subtract (struct timespec *result,
35 /* Perform the carry for the later subtraction by updating Y. */
36 if (x->tv_nsec < y->tv_nsec) {
37 int num_sec = (y->tv_nsec - x->tv_nsec) / 1000000000 + 1;
38 y->tv_nsec -= 1000000000 * num_sec;
41 if (x->tv_nsec - y->tv_nsec > 1000000000) {
42 int num_sec = (x->tv_nsec - y->tv_nsec) / 1000000000;
43 y->tv_nsec += 1000000000 * num_sec;
47 /* Compute the time remaining to wait.
48 `tv_nsec' is certainly positive. */
49 result->tv_sec = x->tv_sec - y->tv_sec;
50 result->tv_nsec = x->tv_nsec - y->tv_nsec;
52 /* Return 1 if result is negative. */
53 return x->tv_sec < y->tv_sec;
56 int miliseconds_since(struct timespec *t)
58 struct timespec now, diff;
60 clock_gettime(CLOCK_MONOTONIC, &now);
61 timespec_subtract(&diff, t, &now);
62 return diff.tv_sec * 1000 + diff.tv_nsec/1000000;
67 void rcv_pwr_voltage_cb (const ORTERecvInfo *info, void *vinstance,
68 void *recvCallBackParam)
70 UDE_hw_status_t status = STATUS_FAILED;
71 static UDE_hw_status_t last_status;
72 static struct timespec last_sent;
73 struct robottype_orte_data *orte_data = (struct robottype_orte_data *)recvCallBackParam;
74 switch (info->status) {
76 uoled_display_voltage(&orte_data->pwr_voltage);
80 status = STATUS_FAILED;
83 if (status != last_status ||
84 miliseconds_since(&last_sent) > 1000) {
85 uoled_display_status(PWR, STATUS_OK);
86 clock_gettime(CLOCK_MONOTONIC, &last_sent);
91 void rcv_odo_data_cb(const ORTERecvInfo *info, void *vinstance,
92 void *recvCallBackParam)
94 UDE_hw_status_t status = STATUS_FAILED;
95 static UDE_hw_status_t last_status;
96 static struct timespec last_sent;
97 //struct odo_data_type *instance = (struct odo_data_type *)vinstance;
98 switch (info->status) {
103 status = STATUS_FAILED;
106 /* Neni potreba aktualizovat stav na displeji 25x za sekundu */
107 if (status != last_status ||
108 miliseconds_since(&last_sent) > 1000) {
109 uoled_display_status(ODO, STATUS_OK);
110 clock_gettime(CLOCK_MONOTONIC, &last_sent);
112 last_status = status;
115 void rcv_motion_irc_cb(const ORTERecvInfo *info, void *vinstance,
116 void *recvCallBackParam)
118 UDE_hw_status_t status = STATUS_FAILED;
119 static UDE_hw_status_t last_status;
120 static struct timespec last_sent;
121 switch (info->status) {
126 status = STATUS_FAILED;
129 /* Neni potreba aktualizovat stav na displeji 25x za sekundu */
130 if (status != last_status ||
131 miliseconds_since(&last_sent) > 1000) {
132 uoled_display_status(MOT, STATUS_OK);
133 clock_gettime(CLOCK_MONOTONIC, &last_sent);
135 last_status = status;
138 void rcv_camera_result_cb(const ORTERecvInfo *info, void *vinstance,
139 void *recvCallBackParam)
141 UDE_hw_status_t status = STATUS_FAILED;
142 static UDE_hw_status_t last_status;
143 static struct timespec last_sent;
144 //struct camera_result_type *instance = (struct camera_result_type *)vinstance;
145 switch (info->status) {
150 status = STATUS_FAILED;
153 if (status != last_status ||
154 miliseconds_since(&last_sent) > 1000) {
155 uoled_display_status(CAM, STATUS_OK);
156 clock_gettime(CLOCK_MONOTONIC, &last_sent);
158 last_status = status;
161 void rcv_hokuyo_scan_cb(const ORTERecvInfo *info, void *vinstance,
162 void *recvCallBackParam)
164 UDE_hw_status_t status = STATUS_FAILED;
165 static UDE_hw_status_t last_status;
166 static struct timespec last_sent;
167 //struct hokuyo_scan_type *instance = (struct hokuyo_scan_type *)vinstance;
168 switch (info->status) {
173 status = STATUS_FAILED;
176 if (status != last_status ||
177 miliseconds_since(&last_sent) > 1000) {
178 uoled_display_status(HOK, STATUS_OK);
179 clock_gettime(CLOCK_MONOTONIC, &last_sent);
181 last_status = status;
184 void rcv_ref_pos_cb (const ORTERecvInfo *info, void *vinstance,
185 void *recvCallBackParam)
187 struct robottype_orte_data *orte_data = (struct robottype_orte_data *)recvCallBackParam;
188 switch (info->status) {
190 uoled_display_position(&orte_data->ref_pos);
197 void rcv_est_pos_odo_cb (const ORTERecvInfo *info, void *vinstance,
198 void *recvCallBackParam)
200 UDE_hw_status_t status = STATUS_FAILED;
201 static UDE_hw_status_t last_status;
202 static struct timespec last_sent;
203 struct robottype_orte_data *orte_data = (struct robottype_orte_data *)recvCallBackParam;
204 switch (info->status) {
206 uoled_display_position(&orte_data->est_pos_odo);
210 status = STATUS_FAILED;
213 if (status != last_status ||
214 miliseconds_since(&last_sent) > 1000) {
215 uoled_display_status(ODO, STATUS_OK);
216 clock_gettime(CLOCK_MONOTONIC, &last_sent);
218 last_status = status;
221 void rcv_fsm_main_cb(const ORTERecvInfo *info, void *vinstance,
222 void *recvCallBackParam)
224 char status[MGS_LENGTH_DISPLAY_SM];
225 static char last_status[MGS_LENGTH_DISPLAY_SM];
226 static struct timespec last_sent;
227 struct fsm_state_type *fsm_state = (struct fsm_state_type *)vinstance;
228 switch (info->status) {
230 strncpy(status, fsm_state->state_name, MGS_LENGTH_DISPLAY_SM);
231 status[MGS_LENGTH_DISPLAY_SM-1]='\0';
232 //uoled_display_fsm(FSM_MAIN, fsm_state->state_name);
236 //uoled_display_fsm(FSM_MAIN, "?");
239 if (strcmp(status,last_status) != 0 ||
240 miliseconds_since(&last_sent) > 1000) {
241 uoled_display_fsm(FSM_MAIN, status);
242 clock_gettime(CLOCK_MONOTONIC, &last_sent);
244 strncpy(last_status,status,MGS_LENGTH_DISPLAY_SM);
247 void rcv_fsm_act_cb(const ORTERecvInfo *info, void *vinstance,
248 void *recvCallBackParam)
250 char status[MGS_LENGTH_DISPLAY_SM];
251 static char last_status[MGS_LENGTH_DISPLAY_SM];
252 static struct timespec last_sent;
253 struct fsm_state_type *fsm_state = (struct fsm_state_type *)vinstance;
254 switch (info->status) {
256 strncpy(status, fsm_state->state_name, MGS_LENGTH_DISPLAY_SM);
257 status[MGS_LENGTH_DISPLAY_SM-1]='\0';
263 if (strcmp(status,last_status) != 0 ||
264 miliseconds_since(&last_sent) > 1000) {
265 uoled_display_fsm(FSM_ACT, status);
266 clock_gettime(CLOCK_MONOTONIC, &last_sent);
268 strncpy(last_status,status,MGS_LENGTH_DISPLAY_SM);
271 void rcv_fsm_motion_cb(const ORTERecvInfo *info, void *vinstance,
272 void *recvCallBackParam)
274 char status[MGS_LENGTH_DISPLAY_SM];
275 static char last_status[MGS_LENGTH_DISPLAY_SM];
276 static struct timespec last_sent;
277 struct fsm_state_type *fsm_state = (struct fsm_state_type *)vinstance;
278 switch (info->status) {
280 strncpy(status, fsm_state->state_name, MGS_LENGTH_DISPLAY_SM);
281 status[MGS_LENGTH_DISPLAY_SM-1]='\0';
287 if (strcmp(status,last_status) != 0 ||
288 miliseconds_since(&last_sent) > 1000) {
289 uoled_display_fsm(FSM_MOVE, status);
290 clock_gettime(CLOCK_MONOTONIC, &last_sent);
292 strncpy(last_status,status,MGS_LENGTH_DISPLAY_SM);
298 int main(int argc, char *argv[])
300 //fd_set read_fd_set;
301 //struct timeval timeout;
305 struct robottype_orte_data orte;
308 //struct can_frame frame;
309 tty = getenv("DISPLAY_TTY");
312 printf("displayd: enviroment variable 'DISPLAY_TTY' was not set, using default value '%s'.\n", tty);
315 sercom = uoled_init(tty, NULL);
316 if (strcmp(tty, "/dev/null") != 0) {
318 printf("displayd: init serial communication OK\n");
320 printf("displayd: init serial communication failed\n");
326 /* orte initialization */
327 if(robottype_roboorte_init(&orte)) {
328 printf("Roboorte initialization failed! Exiting...\n");
332 printf("Roboorte OK\n");
334 /* creating publishers */
335 //robottype_publisher_pwr_voltage_create(&orte, NULL, NULL);
336 //robottype_publisher_motion_status_create(&orte, NULL, NULL);
337 //robottype_publisher_odo_data_create(&orte, NULL, NULL);
338 //robottype_publisher_motion_irc_create(&orte, NULL, NULL);
339 //robottype_publisher_robot_cmd_create(&orte, NULL, NULL);
340 //robottype_publisher_corr_trig_create(&orte, NULL, NULL);
341 //robottype_publisher_corr_distances_create(&orte, NULL, NULL);
342 //robottype_publisher_actuator_status_create(&orte, NULL, NULL);
343 //printf("Publishers OK\n");
345 /* creating subscribers */
346 robottype_subscriber_odo_data_create(&orte, rcv_odo_data_cb, &orte);
347 robottype_subscriber_motion_irc_create(&orte, rcv_motion_irc_cb, &orte);
348 robottype_subscriber_pwr_voltage_create(&orte, rcv_pwr_voltage_cb, &orte);
349 robottype_subscriber_est_pos_odo_create(&orte, rcv_est_pos_odo_cb, &orte);
350 robottype_subscriber_ref_pos_create(&orte, rcv_ref_pos_cb, &orte);
351 //robottype_subscriber_pwr_alert_create(&orte, rcv_pwr_alert_cb, &orte);
352 //robottype_subscriber_motion_speed_create(&orte, rcv_motion_speed_cb, &orte);
353 //robottype_subscriber_can_msg_create(&orte, rcv_can_msg_cb, &orte);
354 robottype_subscriber_hokuyo_scan_create(&orte, rcv_hokuyo_scan_cb, &orte);
355 robottype_subscriber_camera_result_create(&orte, rcv_camera_result_cb, &orte);
356 robottype_subscriber_fsm_main_create(&orte, rcv_fsm_main_cb, &orte);
357 robottype_subscriber_fsm_act_create(&orte, rcv_fsm_act_cb, &orte);
358 robottype_subscriber_fsm_motion_create(&orte, rcv_fsm_motion_cb, &orte);
360 printf("subscribers created OK\n");
362 struct pollfd pfd[1] = {{
370 if (strcmp(tty, "/dev/null") != 0) {
371 ret = poll(pfd, 1, 500/*ms*/);
373 #warning TODO: process received serial data here
374 // nejeka_fce_co_obstara_ktery_publisher_se_zavola = zfce...
375 // zfce(uoled_recieve_cmd());
378 usleep(500/*ms*/*1000);
382 uoled_display_alive();