Mario Bambagini
/
can
program which tests the CAR project (concerning the CAN bus)
Revision 1:8446a0d14877, committed 2015-08-31
- Comitter:
- mariob
- Date:
- Mon Aug 31 22:27:29 2015 +0000
- Parent:
- 0:8097eeede645
- Commit message:
- program which tests the CAR project (concerning the CAN comunication)
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
net.hpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/main.cpp Sun Aug 02 12:55:55 2015 +0000 +++ b/main.cpp Mon Aug 31 22:27:29 2015 +0000 @@ -27,21 +27,30 @@ DigitalOut led_body(LED1); DigitalOut led_engine(LED2); DigitalOut led_time(LED3); +DigitalOut led_driver(LED4); CAN can(p9, p10); can_cmd_body_t can_cmd_body; +can_cmd_engine_t can_cmd_engine; +can_cmd_driver_t can_cmd_driver; +can_cmd_time_t can_cmd_time; int counter = 0; +int echo = 0; -#define BODY_ 2 -#define ENGINE_ 13 -#define TIME_ 40 +#define BODY_ 4 +#define ENGINE_ 2 +#define DRIVER_ 5 +#define TIME_ 11 void send() { - printf("send()\r\n"); + //printf("send()\r\n"); static int body_n_sent = 0; + static int engine_n_sent = 0; + static int driver_n_sent = 0; + static int time_n_sent = 0; counter++; @@ -50,12 +59,47 @@ can_cmd_body.payload.msg.light_l = (body_n_sent&0x0002)==0x0002; can_cmd_body.payload.msg.light_c = (body_n_sent&0x0004)==0x0004; if(can.write(CANMessage(CAN_CMD_BODY_ID, (char*)(can_cmd_body.payload.buf), 4))) - printf("BODY OK\r\n"); + led_body = !led_body;//printf("BODY OK\r\n"); else printf("BODY NOT OK\r\n"); - led_body = !led_body; + body_n_sent++; } + + if ((counter%ENGINE_)==0) { + can_cmd_engine.payload.msg.steering = engine_n_sent; + can_cmd_engine.payload.msg.power = engine_n_sent+2; + can_cmd_engine.payload.msg.direction = engine_n_sent%2; + can_cmd_engine.payload.msg.breaking = (engine_n_sent%2)?0:1; + if(can.write(CANMessage(CAN_CMD_ENGINE_ID, (char*)(can_cmd_engine.payload.buf), 4))) + led_engine = !led_engine;//printf("ENGINE OK\r\n"); + else + printf("ENGINE NOT OK\r\n"); + + engine_n_sent++; + } + + if ((counter%DRIVER_)==0) { + can_cmd_driver.payload.msg.cmd = 0x0A0A; + echo = counter; + can_cmd_driver.payload.msg.data = counter; + if(can.write(CANMessage(CAN_CMD_DRIVER_ID, (char*)(can_cmd_driver.payload.buf), 8))) + led_driver = !led_driver;//printf("DRIVER OK\r\n"); + else + printf("DRIVER NOT OK\r\n"); + + driver_n_sent++; + } + + if ((counter%TIME_)==0) { + can_cmd_time.payload.msg.time = time(NULL); + if(can.write(CANMessage(CAN_CMD_TIME_ID, (char*)(can_cmd_time.payload.buf), 4))) + led_time = !led_time;//printf("TIME OK\r\n"); + else + printf("TIME NOT OK\r\n"); + + time_n_sent++; + } } @@ -76,15 +120,27 @@ */ printf("main()\r\n"); - ticker.attach(&send, 1); - + ticker.attach(&send, 0.5); +set_time(0); CANMessage msg; while(1) { if(can.read(msg)) { - can_sts_body_payload_t x; - x.buf[0] = msg.data[0]; x.buf[1] = msg.data[1]; x.buf[2] = msg.data[2]; x.buf[3] = msg.data[3]; - x.buf[4] = msg.data[4]; x.buf[5] = msg.data[5]; x.buf[6] = msg.data[6]; x.buf[7] = msg.data[7]; - printf("Message received: %d %d %d %d\r\n", x.buf[0], x.msg.eye_back_l, x.msg.eye_back_r, x.msg.eye_front); + switch(msg.id) { + case CAN_CMD_BODY_ID: + can_sts_body_payload_t x; + x.buf[0] = msg.data[0]; x.buf[1] = msg.data[1]; x.buf[2] = msg.data[2]; x.buf[3] = msg.data[3]; + x.buf[4] = msg.data[4]; x.buf[5] = msg.data[5]; x.buf[6] = msg.data[6]; x.buf[7] = msg.data[7]; + printf("Message received: %d %d %d %d\r\n", x.buf[0], x.msg.eye_back_l, x.msg.eye_back_r, x.msg.eye_front); + break; + case CAN_STS_DRIVER_ID: + can_sts_driver_payload_t y; + y.buf[0] = msg.data[0]; y.buf[1] = msg.data[1]; y.buf[2] = msg.data[2]; y.buf[3] = msg.data[3]; + if (y.msg.data == echo) + printf("ECHO %d\r\n", y.msg.data); + else + printf("NO ECHO %d\r\n", y.msg.data); + break; + } } wait(1.0); }
--- a/net.hpp Sun Aug 02 12:55:55 2015 +0000 +++ b/net.hpp Mon Aug 31 22:27:29 2015 +0000 @@ -7,6 +7,11 @@ #define CAN_FLAG_RECEIVED 0x01 #define CAN_FLAG_SEND 0x02 +#define CAN_THREAD_PERIOD 100 //ms + + + +#define CMD_ECHO 0x0A0A /************************************************** * GLOBAL DEFINES @@ -18,10 +23,24 @@ #define CAN_CMD_DRIVER_ID 0x11 #define CAN_STS_DRIVER_ID 0x10 -#define PERIOD_1s 10 -#define PERIOD_500ms 5 +#define PERIOD_3s (3000/CAN_THREAD_PERIOD) +#define PERIOD_1s (1000/CAN_THREAD_PERIOD) +#define PERIOD_500ms (500/CAN_THREAD_PERIOD) +#define PERIOD_400ms (400/CAN_THREAD_PERIOD) +#define PERIOD_300ms (300/CAN_THREAD_PERIOD) +#define PERIOD_200ms (200/CAN_THREAD_PERIOD) +#define PERIOD_100ms (100/CAN_THREAD_PERIOD) -#define CAN_STS_BODY_PERIOD PERIOD_500ms +#define CAN_STS_BODY_PERIOD PERIOD_1s + + +#define CAN_MISSING_DETECTION PERIOD_3s + +#define CAN_MISSING_BODY_ID 0 +#define CAN_MISSING_ENGINE_ID 1 +#define CAN_MISSING_TIME_ID 2 + +#define CAN_RX_PERIODIC_MSG 3 /************************************************** * GLOBAL TYPES @@ -100,7 +119,7 @@ uint32 data; } msg; } can_cmd_driver_payload_t; -typedef struct can_driver_time_s { +typedef struct can_cmd_driver_s { can_cmd_driver_payload_t payload; uint8 flag; } can_cmd_driver_t; @@ -119,6 +138,6 @@ /************************************************** * DATA STRUCTURES **************************************************/ -void init_can(CAN *device); + #endif //__NET_H__