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>
23 struct sercom_data* sercom;
25 /* Subtract the `struct timespec' values X and Y,
26 storing the result in RESULT (result = x - y).
27 Return 1 if the difference is negative, otherwise 0. */
30 timespec_subtract (struct timespec *result,
34 /* Perform the carry for the later subtraction by updating Y. */
35 if (x->tv_nsec < y->tv_nsec) {
36 int num_sec = (y->tv_nsec - x->tv_nsec) / 1000000000 + 1;
37 y->tv_nsec -= 1000000000 * num_sec;
40 if (x->tv_nsec - y->tv_nsec > 1000000000) {
41 int num_sec = (x->tv_nsec - y->tv_nsec) / 1000000000;
42 y->tv_nsec += 1000000000 * num_sec;
46 /* Compute the time remaining to wait.
47 `tv_nsec' is certainly positive. */
48 result->tv_sec = x->tv_sec - y->tv_sec;
49 result->tv_nsec = x->tv_nsec - y->tv_nsec;
51 /* Return 1 if result is negative. */
52 return x->tv_sec < y->tv_sec;
55 int miliseconds_since(struct timespec *t)
57 struct timespec now, diff;
59 clock_gettime(CLOCK_MONOTONIC, &now);
60 timespec_subtract(&diff, t, &now);
61 return diff.tv_sec * 1000 + diff.tv_nsec/1000000;
66 void rcv_pwr_voltage_cb (const ORTERecvInfo *info, void *vinstance,
67 void *recvCallBackParam)
69 UDE_hw_status_t status = STATUS_FAILED;
70 static UDE_hw_status_t last_status;
71 static struct timespec last_sent;
72 struct robottype_orte_data *orte_data = (struct robottype_orte_data *)recvCallBackParam;
73 switch (info->status) {
75 uoled_display_voltage(&orte_data->pwr_voltage);
79 status = STATUS_FAILED;
82 if (status != last_status ||
83 miliseconds_since(&last_sent) > 1000) {
84 uoled_display_status(PWR, STATUS_OK);
85 clock_gettime(CLOCK_MONOTONIC, &last_sent);
90 void rcv_odo_data_cb(const ORTERecvInfo *info, void *vinstance,
91 void *recvCallBackParam)
93 UDE_hw_status_t status = STATUS_FAILED;
94 static UDE_hw_status_t last_status;
95 static struct timespec last_sent;
96 //struct odo_data_type *instance = (struct odo_data_type *)vinstance;
97 switch (info->status) {
102 status = STATUS_FAILED;
105 /* Neni potreba aktualizovat stav na displeji 25x za sekundu */
106 if (status != last_status ||
107 miliseconds_since(&last_sent) > 1000) {
108 uoled_display_status(HOK, STATUS_OK);
109 clock_gettime(CLOCK_MONOTONIC, &last_sent);
111 last_status = status;
114 void rcv_motion_irc_cb(const ORTERecvInfo *info, void *vinstance,
115 void *recvCallBackParam)
117 UDE_hw_status_t status = STATUS_FAILED;
118 static UDE_hw_status_t last_status;
119 static struct timespec last_sent;
120 switch (info->status) {
125 status = STATUS_FAILED;
128 /* Neni potreba aktualizovat stav na displeji 25x za sekundu */
129 if (status != last_status ||
130 miliseconds_since(&last_sent) > 1000) {
131 uoled_display_status(MOT, STATUS_OK);
132 clock_gettime(CLOCK_MONOTONIC, &last_sent);
134 last_status = status;
137 void rcv_camera_result_cb(const ORTERecvInfo *info, void *vinstance,
138 void *recvCallBackParam)
140 UDE_hw_status_t status = STATUS_FAILED;
141 static UDE_hw_status_t last_status;
142 static struct timespec last_sent;
143 //struct camera_result_type *instance = (struct camera_result_type *)vinstance;
144 switch (info->status) {
149 status = STATUS_FAILED;
152 if (status != last_status ||
153 miliseconds_since(&last_sent) > 1000) {
154 uoled_display_status(CAM, STATUS_OK);
155 clock_gettime(CLOCK_MONOTONIC, &last_sent);
157 last_status = status;
160 void rcv_ref_pos_cb (const ORTERecvInfo *info, void *vinstance,
161 void *recvCallBackParam)
163 struct robottype_orte_data *orte_data = (struct robottype_orte_data *)recvCallBackParam;
164 switch (info->status) {
166 uoled_display_position(&orte_data->ref_pos);
173 void rcv_est_pos_odo_cb (const ORTERecvInfo *info, void *vinstance,
174 void *recvCallBackParam)
176 UDE_hw_status_t status = STATUS_FAILED;
177 static UDE_hw_status_t last_status;
178 static struct timespec last_sent;
179 struct robottype_orte_data *orte_data = (struct robottype_orte_data *)recvCallBackParam;
180 switch (info->status) {
182 uoled_display_position(&orte_data->est_pos_odo);
186 status = STATUS_FAILED;
189 if (status != last_status ||
190 miliseconds_since(&last_sent) > 1000) {
191 uoled_display_status(ODO, STATUS_OK);
192 clock_gettime(CLOCK_MONOTONIC, &last_sent);
194 last_status = status;
197 void rcv_fsm_main_cb(const ORTERecvInfo *info, void *vinstance,
198 void *recvCallBackParam)
200 char status[MGS_LENGTH_DISPLAY_SM];
201 static char last_status[MGS_LENGTH_DISPLAY_SM];
202 static struct timespec last_sent;
203 struct fsm_state_type *fsm_state = (struct fsm_state_type *)vinstance;
204 switch (info->status) {
206 strncpy(status, fsm_state->state_name, MGS_LENGTH_DISPLAY_SM);
207 status[MGS_LENGTH_DISPLAY_SM-1]='\0';
208 //uoled_display_fsm(FSM_MAIN, fsm_state->state_name);
212 //uoled_display_fsm(FSM_MAIN, "?");
215 if (strcmp(status,last_status) != 0 ||
216 miliseconds_since(&last_sent) > 1000) {
217 uoled_display_fsm(FSM_MAIN, status);
218 clock_gettime(CLOCK_MONOTONIC, &last_sent);
220 strncpy(last_status,status,MGS_LENGTH_DISPLAY_SM);
223 void rcv_fsm_act_cb(const ORTERecvInfo *info, void *vinstance,
224 void *recvCallBackParam)
226 char status[MGS_LENGTH_DISPLAY_SM];
227 static char last_status[MGS_LENGTH_DISPLAY_SM];
228 static struct timespec last_sent;
229 struct fsm_state_type *fsm_state = (struct fsm_state_type *)vinstance;
230 switch (info->status) {
232 strncpy(status, fsm_state->state_name, MGS_LENGTH_DISPLAY_SM);
233 status[MGS_LENGTH_DISPLAY_SM-1]='\0';
239 if (strcmp(status,last_status) != 0 ||
240 miliseconds_since(&last_sent) > 1000) {
241 uoled_display_fsm(FSM_ACT, status);
242 clock_gettime(CLOCK_MONOTONIC, &last_sent);
244 strncpy(last_status,status,MGS_LENGTH_DISPLAY_SM);
247 void rcv_fsm_motion_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_MOVE, status);
266 clock_gettime(CLOCK_MONOTONIC, &last_sent);
268 strncpy(last_status,status,MGS_LENGTH_DISPLAY_SM);
274 int main(int argc, char *argv[])
276 //fd_set read_fd_set;
277 //struct timeval timeout;
281 struct robottype_orte_data orte;
284 //struct can_frame frame;
285 tty = getenv("DISPLAY_TTY");
288 printf("displayd: enviroment variable 'DISPLAY_TTY' was not set, using default value '%s'.\n", tty);
291 sercom = uoled_init(tty, NULL);
292 if (strcmp(tty, "/dev/null") != 0) {
294 printf("displayd: init serial communication OK\n");
296 printf("displayd: init serial communication failed\n");
302 /* orte initialization */
303 if(robottype_roboorte_init(&orte)) {
304 printf("Roboorte initialization failed! Exiting...\n");
308 printf("Roboorte OK\n");
310 /* creating publishers */
311 //robottype_publisher_pwr_voltage_create(&orte, NULL, NULL);
312 //robottype_publisher_motion_status_create(&orte, NULL, NULL);
313 //robottype_publisher_odo_data_create(&orte, NULL, NULL);
314 //robottype_publisher_motion_irc_create(&orte, NULL, NULL);
315 //robottype_publisher_robot_cmd_create(&orte, NULL, NULL);
316 //robottype_publisher_corr_trig_create(&orte, NULL, NULL);
317 //robottype_publisher_corr_distances_create(&orte, NULL, NULL);
318 //robottype_publisher_actuator_status_create(&orte, NULL, NULL);
319 //printf("Publishers OK\n");
321 /* creating subscribers */
322 robottype_subscriber_odo_data_create(&orte, rcv_odo_data_cb, &orte);
323 robottype_subscriber_motion_irc_create(&orte, rcv_motion_irc_cb, &orte);
324 robottype_subscriber_pwr_voltage_create(&orte, rcv_pwr_voltage_cb, &orte);
325 robottype_subscriber_est_pos_odo_create(&orte, rcv_est_pos_odo_cb, &orte);
326 robottype_subscriber_ref_pos_create(&orte, rcv_ref_pos_cb, &orte);
327 //robottype_subscriber_pwr_alert_create(&orte, rcv_pwr_alert_cb, &orte);
328 //robottype_subscriber_motion_speed_create(&orte, rcv_motion_speed_cb, &orte);
329 //robottype_subscriber_can_msg_create(&orte, rcv_can_msg_cb, &orte);
330 //robottype_subscriber_hokuyo_pitch_create(&orte, rcv_hokuyo_pitch_cb, &orte);
331 robottype_subscriber_camera_result_create(&orte, rcv_camera_result_cb, &orte);
332 robottype_subscriber_fsm_main_create(&orte, rcv_fsm_main_cb, &orte);
333 robottype_subscriber_fsm_act_create(&orte, rcv_fsm_act_cb, &orte);
334 robottype_subscriber_fsm_motion_create(&orte, rcv_fsm_motion_cb, &orte);
336 printf("subscribers created OK\n");
338 struct pollfd pfd[1] = {{
346 if (strcmp(tty, "/dev/null") != 0) {
347 ret = poll(pfd, 1, 500/*ms*/);
349 #warning TODO: process received serial data here
352 usleep(500/*ms*/*1000);
356 uoled_display_alive();