telemetry node

Committer:
LukeMar
Date:
Sun Mar 17 01:01:21 2019 +0000
Revision:
0:8128714922a1
Child:
1:69a8c8a7cf1d
3/16/19

Who changed what in which revision?

UserRevisionLine numberNew contents of line
LukeMar 0:8128714922a1 1 // Program to test the CAN bus using pins 29 and 30 on the mbed connected
LukeMar 0:8128714922a1 2 // to an MCP2551 CAN transceiver bus IC
LukeMar 0:8128714922a1 3 // Note that this program will only continue to transmit while the TX message is <= 8 bytes long
LukeMar 0:8128714922a1 4
LukeMar 0:8128714922a1 5 #include "mbed.h"
LukeMar 0:8128714922a1 6
LukeMar 0:8128714922a1 7 #define RUDDER_ID 1 //Address of this CAN device
LukeMar 0:8128714922a1 8 #define BNO_ID 2 //Address of Control CAN device
LukeMar 0:8128714922a1 9 #define TELE_ID 3
LukeMar 0:8128714922a1 10 #define RC_ID 4
LukeMar 0:8128714922a1 11
LukeMar 0:8128714922a1 12 Serial pc(USBTX, USBRX); //tx, and rx for tera term
LukeMar 0:8128714922a1 13 DigitalOut led1(LED1); //heartbeat
LukeMar 0:8128714922a1 14 DigitalOut led2(LED2); //CAN read activity
LukeMar 0:8128714922a1 15 DigitalOut led3(LED3); //CAN write activity
LukeMar 0:8128714922a1 16
LukeMar 0:8128714922a1 17 CAN can(p30,p29); //CAN interface
LukeMar 0:8128714922a1 18
LukeMar 0:8128714922a1 19
LukeMar 0:8128714922a1 20 Ticker pulse;
LukeMar 0:8128714922a1 21
LukeMar 0:8128714922a1 22 float BNOangle;
LukeMar 0:8128714922a1 23 float RC_out;
LukeMar 0:8128714922a1 24 char datastr[20];
LukeMar 0:8128714922a1 25 CANMessage msg_read;
LukeMar 0:8128714922a1 26
LukeMar 0:8128714922a1 27 float CanValue(){
LukeMar 0:8128714922a1 28 strcpy(datastr, (char*)msg_read.data+1);
LukeMar 0:8128714922a1 29 float value = strtod(datastr,NULL);
LukeMar 0:8128714922a1 30 return value;
LukeMar 0:8128714922a1 31 }
LukeMar 0:8128714922a1 32
LukeMar 0:8128714922a1 33 void alive(void){
LukeMar 0:8128714922a1 34 led1 = !led1;
LukeMar 0:8128714922a1 35 if(led1)
LukeMar 0:8128714922a1 36 pulse.attach(&alive, .2); // the address of the function to be attached (flip) and the interval (2 seconds)
LukeMar 0:8128714922a1 37 else
LukeMar 0:8128714922a1 38 pulse.attach(&alive, 1.3); // the address of the function to be attached (flip) and the interval (2 seconds)
LukeMar 0:8128714922a1 39 }
LukeMar 0:8128714922a1 40
LukeMar 0:8128714922a1 41 void Bno_Read(){
LukeMar 0:8128714922a1 42
LukeMar 0:8128714922a1 43 while(can.read(msg_read)){ //if message is available, read into msg
LukeMar 0:8128714922a1 44 if(msg_read.id == BNO_ID){ //if its the BNO ID
LukeMar 0:8128714922a1 45 if(msg_read.data[0] == 's' || msg_read.data[0] == 'S'){
LukeMar 0:8128714922a1 46 led2 = !led2;
LukeMar 0:8128714922a1 47 BNOangle = CanValue();
LukeMar 0:8128714922a1 48 pc.printf("BNO: %.1f\n",BNOangle);
LukeMar 0:8128714922a1 49 }//if the first letter was an 's', it is a servo command
LukeMar 0:8128714922a1 50 } //was the message to this device?
LukeMar 0:8128714922a1 51 }//while a CAN message has been read
LukeMar 0:8128714922a1 52 //return datastr;
LukeMar 0:8128714922a1 53
LukeMar 0:8128714922a1 54 }
LukeMar 0:8128714922a1 55 void RC_Read(){
LukeMar 0:8128714922a1 56
LukeMar 0:8128714922a1 57 while(can.read(msg_read)){ //if message is available, read into msg
LukeMar 0:8128714922a1 58 if(msg_read.id == RC_ID){ //if its the BNO ID
LukeMar 0:8128714922a1 59 if(msg_read.data[0] == 'r' || msg_read.data[0] == 'R'){
LukeMar 0:8128714922a1 60 led3 = !led3;
LukeMar 0:8128714922a1 61 RC_out = CanValue();
LukeMar 0:8128714922a1 62 pc.printf("RC: %.1f\n",RC_out);
LukeMar 0:8128714922a1 63 }//if the first letter was an 's', it is a servo command
LukeMar 0:8128714922a1 64 } //was the message to this device?
LukeMar 0:8128714922a1 65 }//while a CAN message has been read
LukeMar 0:8128714922a1 66 //return datastr;
LukeMar 0:8128714922a1 67
LukeMar 0:8128714922a1 68 }
LukeMar 0:8128714922a1 69
LukeMar 0:8128714922a1 70 int main() {
LukeMar 0:8128714922a1 71
LukeMar 0:8128714922a1 72 Thread::wait(200);
LukeMar 0:8128714922a1 73
LukeMar 0:8128714922a1 74 pulse.attach(&alive, 2.0); // the address of the function to be attached (alive) and the interval (2 seconds)
LukeMar 0:8128714922a1 75 can.frequency(500000);
LukeMar 0:8128714922a1 76 pc.baud(115200);
LukeMar 0:8128714922a1 77
LukeMar 0:8128714922a1 78 pc.printf("%s\r\n", __FILE__);
LukeMar 0:8128714922a1 79
LukeMar 0:8128714922a1 80 while(1){
LukeMar 0:8128714922a1 81 Bno_Read();
LukeMar 0:8128714922a1 82 RC_Read();
LukeMar 0:8128714922a1 83
LukeMar 0:8128714922a1 84 }
LukeMar 0:8128714922a1 85
LukeMar 0:8128714922a1 86
LukeMar 0:8128714922a1 87 }//main