Formula Student / Mbed 2 deprecated Canbus_test

Dependencies:   mbed

Committer:
ArmandLambrechts
Date:
Mon Nov 27 12:34:44 2017 +0000
Revision:
1:12ddbe69b6e6
Parent:
0:d9f4fedf5253
Child:
2:373260769485
canbus naar controller, id moet erachter gezet worden

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ArmandLambrechts 0:d9f4fedf5253 1 /*** Thread: CAN ***
ArmandLambrechts 1:12ddbe69b6e6 2 * Test CAN to controller
ArmandLambrechts 1:12ddbe69b6e6 3 *TODO
ArmandLambrechts 1:12ddbe69b6e6 4 *2 Ints samenvoegen zodat we naar het juiste register kunnen sturen, register is fixed, de waardes voor de controller zijn afhankelijk van een sensor.
ArmandLambrechts 0:d9f4fedf5253 5 */
ArmandLambrechts 0:d9f4fedf5253 6 #include "mbed.h"
ArmandLambrechts 0:d9f4fedf5253 7 #include "CAN.h"
ArmandLambrechts 1:12ddbe69b6e6 8 #include <string>
ArmandLambrechts 0:d9f4fedf5253 9 /* CAN (RD TD) */
ArmandLambrechts 0:d9f4fedf5253 10 CAN can(PA_11, PA_12);
ArmandLambrechts 0:d9f4fedf5253 11
ArmandLambrechts 0:d9f4fedf5253 12 DigitalOut canEN(PA_10);
ArmandLambrechts 0:d9f4fedf5253 13 /* Heartbeat */
ArmandLambrechts 0:d9f4fedf5253 14 DigitalOut led(LED1);
ArmandLambrechts 0:d9f4fedf5253 15
ArmandLambrechts 0:d9f4fedf5253 16 /* Structures to help pack and unpack the 8 CAN data bytes */
ArmandLambrechts 0:d9f4fedf5253 17 typedef struct bytes_64 {
ArmandLambrechts 0:d9f4fedf5253 18 uint8_t b0;
ArmandLambrechts 0:d9f4fedf5253 19 uint8_t b1;
ArmandLambrechts 0:d9f4fedf5253 20 uint8_t b2;
ArmandLambrechts 0:d9f4fedf5253 21 uint8_t b3;
ArmandLambrechts 0:d9f4fedf5253 22 uint8_t b4;
ArmandLambrechts 0:d9f4fedf5253 23 uint8_t b5;
ArmandLambrechts 0:d9f4fedf5253 24 uint8_t b6;
ArmandLambrechts 0:d9f4fedf5253 25 uint8_t b7;
ArmandLambrechts 0:d9f4fedf5253 26 } bytes_64;
ArmandLambrechts 0:d9f4fedf5253 27
ArmandLambrechts 0:d9f4fedf5253 28 typedef union {
ArmandLambrechts 0:d9f4fedf5253 29 uint64_t data;
ArmandLambrechts 0:d9f4fedf5253 30 bytes_64 bytes;
ArmandLambrechts 0:d9f4fedf5253 31 } data_packed;
ArmandLambrechts 0:d9f4fedf5253 32
ArmandLambrechts 0:d9f4fedf5253 33
ArmandLambrechts 0:d9f4fedf5253 34 /* main */
ArmandLambrechts 0:d9f4fedf5253 35 int main() {
ArmandLambrechts 0:d9f4fedf5253 36
ArmandLambrechts 0:d9f4fedf5253 37 printf("\r\n---Start---\r\n");
ArmandLambrechts 0:d9f4fedf5253 38
ArmandLambrechts 0:d9f4fedf5253 39 data_packed send_data; //struct for send message
ArmandLambrechts 0:d9f4fedf5253 40 data_packed receive_data; //struct for rcv message
ArmandLambrechts 0:d9f4fedf5253 41 CANMessage msg; //message object for rcv
ArmandLambrechts 1:12ddbe69b6e6 42 int send_id = 513; //Id motorcontroller 0x201 -> 513
ArmandLambrechts 0:d9f4fedf5253 43 int send_status = 0;
ArmandLambrechts 0:d9f4fedf5253 44
ArmandLambrechts 1:12ddbe69b6e6 45 canEN = 0; //Voor de tranceiver
ArmandLambrechts 1:12ddbe69b6e6 46
ArmandLambrechts 1:12ddbe69b6e6 47 //send_data.data = 0x0CCD31; //0CCD snelheid motor, 31 register in controller. WERKT
ArmandLambrechts 0:d9f4fedf5253 48 send_data.data = 0;
ArmandLambrechts 1:12ddbe69b6e6 49 can.frequency(1000000); //1m freq
ArmandLambrechts 1:12ddbe69b6e6 50 can.mode(CAN::LocalTest);
ArmandLambrechts 1:12ddbe69b6e6 51 //can.mode(CAN::Normal); //1vd2 comment
ArmandLambrechts 0:d9f4fedf5253 52 /* Loop */
ArmandLambrechts 1:12ddbe69b6e6 53 int x = 0x0CCD;
ArmandLambrechts 1:12ddbe69b6e6 54 int y = 0x31;
ArmandLambrechts 1:12ddbe69b6e6 55 /*/////////////////////////////////////////////////////////////
ArmandLambrechts 1:12ddbe69b6e6 56 int Result;
ArmandLambrechts 1:12ddbe69b6e6 57 Result = a[5];
ArmandLambrechts 1:12ddbe69b6e6 58 Result = Result << 8;
ArmandLambrechts 1:12ddbe69b6e6 59 Result = Result | b[2];
ArmandLambrechts 1:12ddbe69b6e6 60 *//////////////////////////////////////////////////////////////
ArmandLambrechts 1:12ddbe69b6e6 61
ArmandLambrechts 1:12ddbe69b6e6 62 unsigned pow = 10;
ArmandLambrechts 1:12ddbe69b6e6 63 while(y >= pow)
ArmandLambrechts 1:12ddbe69b6e6 64 pow *= 10;
ArmandLambrechts 1:12ddbe69b6e6 65 send_data.data = x * pow + y;
ArmandLambrechts 1:12ddbe69b6e6 66
ArmandLambrechts 0:d9f4fedf5253 67 while (true) {
ArmandLambrechts 0:d9f4fedf5253 68
ArmandLambrechts 1:12ddbe69b6e6 69 send_status = can.write(CANMessage(send_id, (char*) &send_data, 3));
ArmandLambrechts 0:d9f4fedf5253 70
ArmandLambrechts 0:d9f4fedf5253 71 if (send_status == true) {
ArmandLambrechts 1:12ddbe69b6e6 72 //send_data.data = send_data.data + 1;
ArmandLambrechts 1:12ddbe69b6e6 73
ArmandLambrechts 0:d9f4fedf5253 74 }
ArmandLambrechts 0:d9f4fedf5253 75
ArmandLambrechts 0:d9f4fedf5253 76 /* Receive */
ArmandLambrechts 0:d9f4fedf5253 77 if (can.read(msg)) {
ArmandLambrechts 0:d9f4fedf5253 78 //move message bytes to receive, so we can access as a uint64_t (u long long)
ArmandLambrechts 0:d9f4fedf5253 79 receive_data.bytes.b7 = msg.data[7];
ArmandLambrechts 0:d9f4fedf5253 80 receive_data.bytes.b6 = msg.data[6];
ArmandLambrechts 0:d9f4fedf5253 81 receive_data.bytes.b5 = msg.data[5];
ArmandLambrechts 0:d9f4fedf5253 82 receive_data.bytes.b4 = msg.data[4];
ArmandLambrechts 0:d9f4fedf5253 83 receive_data.bytes.b3 = msg.data[3];
ArmandLambrechts 0:d9f4fedf5253 84 receive_data.bytes.b2 = msg.data[2];
ArmandLambrechts 0:d9f4fedf5253 85 receive_data.bytes.b1 = msg.data[1];
ArmandLambrechts 0:d9f4fedf5253 86 receive_data.bytes.b0 = msg.data[0];
ArmandLambrechts 0:d9f4fedf5253 87
ArmandLambrechts 0:d9f4fedf5253 88 printf("Message received ID,Msg: %d, %llu\r\n", msg.id, receive_data.data);
ArmandLambrechts 0:d9f4fedf5253 89 }
ArmandLambrechts 0:d9f4fedf5253 90
ArmandLambrechts 0:d9f4fedf5253 91 /* Heartbeat */
ArmandLambrechts 0:d9f4fedf5253 92 led = !led;
ArmandLambrechts 0:d9f4fedf5253 93
ArmandLambrechts 0:d9f4fedf5253 94 wait(0.5);
ArmandLambrechts 0:d9f4fedf5253 95 } //while
ArmandLambrechts 0:d9f4fedf5253 96 }