![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
telemetry node
main.cpp@1:69a8c8a7cf1d, 2019-04-01 (annotated)
- Committer:
- LukeMar
- Date:
- Mon Apr 01 04:24:18 2019 +0000
- Revision:
- 1:69a8c8a7cf1d
- Parent:
- 0:8128714922a1
4/1/19
Who changed what in which revision?
User | Revision | Line number | New 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 | 1:69a8c8a7cf1d | 9 | #define TELE_ID 3 |
LukeMar | 1:69a8c8a7cf1d | 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 | 1:69a8c8a7cf1d | 16 | DigitalOut led4(LED4); |
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 | 1:69a8c8a7cf1d | 26 | float ff = 1.0; |
LukeMar | 0:8128714922a1 | 27 | |
LukeMar | 1:69a8c8a7cf1d | 28 | float data; //telemetry data transmition |
LukeMar | 0:8128714922a1 | 29 | |
LukeMar | 1:69a8c8a7cf1d | 30 | float CanValue() |
LukeMar | 1:69a8c8a7cf1d | 31 | { |
LukeMar | 1:69a8c8a7cf1d | 32 | strcpy(datastr, (char*)msg_read.data+1); |
LukeMar | 1:69a8c8a7cf1d | 33 | float value = strtod(datastr,NULL); |
LukeMar | 1:69a8c8a7cf1d | 34 | return value; |
LukeMar | 0:8128714922a1 | 35 | } |
LukeMar | 0:8128714922a1 | 36 | |
LukeMar | 1:69a8c8a7cf1d | 37 | //void alive(void) |
LukeMar | 1:69a8c8a7cf1d | 38 | //{ |
LukeMar | 1:69a8c8a7cf1d | 39 | // led1 = !led1; |
LukeMar | 1:69a8c8a7cf1d | 40 | // if(led1) |
LukeMar | 1:69a8c8a7cf1d | 41 | // pulse.attach(&alive, .2); // the address of the function to be attached (flip) and the interval (2 seconds) |
LukeMar | 1:69a8c8a7cf1d | 42 | // else |
LukeMar | 1:69a8c8a7cf1d | 43 | // pulse.attach(&alive, 1.3); // the address of the function to be attached (flip) and the interval (2 seconds) |
LukeMar | 1:69a8c8a7cf1d | 44 | //} |
LukeMar | 1:69a8c8a7cf1d | 45 | void Time_Read(); |
LukeMar | 1:69a8c8a7cf1d | 46 | void Lat_Read(); |
LukeMar | 1:69a8c8a7cf1d | 47 | void Long_Read(); |
LukeMar | 1:69a8c8a7cf1d | 48 | void Bno_Read(); |
LukeMar | 1:69a8c8a7cf1d | 49 | void Rudder_Read(); |
LukeMar | 1:69a8c8a7cf1d | 50 | void Mast_Read(); |
LukeMar | 1:69a8c8a7cf1d | 51 | |
LukeMar | 1:69a8c8a7cf1d | 52 | |
LukeMar | 1:69a8c8a7cf1d | 53 | int main() |
LukeMar | 1:69a8c8a7cf1d | 54 | { |
LukeMar | 1:69a8c8a7cf1d | 55 | |
LukeMar | 1:69a8c8a7cf1d | 56 | Thread::wait(200); |
LukeMar | 1:69a8c8a7cf1d | 57 | |
LukeMar | 1:69a8c8a7cf1d | 58 | // pulse.attach(&alive, 2.0); // the address of the function to be attached (alive) and the interval (2 seconds) |
LukeMar | 1:69a8c8a7cf1d | 59 | can.frequency(500000); |
LukeMar | 1:69a8c8a7cf1d | 60 | pc.baud(115200); |
LukeMar | 1:69a8c8a7cf1d | 61 | |
LukeMar | 1:69a8c8a7cf1d | 62 | pc.printf("%s\r\n", __FILE__); |
LukeMar | 1:69a8c8a7cf1d | 63 | |
LukeMar | 1:69a8c8a7cf1d | 64 | while(1) { |
LukeMar | 1:69a8c8a7cf1d | 65 | //while(msg_read.data[0] != 'm' || msg_read.data[0] != 'M') {Thread::wait(ff);} |
LukeMar | 1:69a8c8a7cf1d | 66 | //Time_Read(); |
LukeMar | 1:69a8c8a7cf1d | 67 | |
LukeMar | 1:69a8c8a7cf1d | 68 | //while(msg_read.data[0] != 'm' || msg_read.data[0] != 'M') {Thread::wait(ff);} |
LukeMar | 1:69a8c8a7cf1d | 69 | //Lat_Read(); |
LukeMar | 1:69a8c8a7cf1d | 70 | |
LukeMar | 1:69a8c8a7cf1d | 71 | //while(msg_read.data[0] != 'm' || msg_read.data[0] != 'M') {Thread::wait(ff);} |
LukeMar | 1:69a8c8a7cf1d | 72 | //Long_Read(); |
LukeMar | 0:8128714922a1 | 73 | |
LukeMar | 1:69a8c8a7cf1d | 74 | //while(msg_read.data[0] != 'b' || msg_read.data[0] != 'B') {Thread::wait(ff);} |
LukeMar | 1:69a8c8a7cf1d | 75 | Bno_Read(); |
LukeMar | 1:69a8c8a7cf1d | 76 | |
LukeMar | 1:69a8c8a7cf1d | 77 | //while(msg_read.data[0] != 'r' || msg_read.data[0] != 'R') {Thread::wait(ff);} |
LukeMar | 1:69a8c8a7cf1d | 78 | Rudder_Read(); |
LukeMar | 1:69a8c8a7cf1d | 79 | |
LukeMar | 1:69a8c8a7cf1d | 80 | //while(msg_read.data[0] != 'm' || msg_read.data[0] != 'M') {Thread::wait(ff);} |
LukeMar | 1:69a8c8a7cf1d | 81 | Mast_Read(); |
LukeMar | 1:69a8c8a7cf1d | 82 | |
LukeMar | 1:69a8c8a7cf1d | 83 | } |
LukeMar | 1:69a8c8a7cf1d | 84 | |
LukeMar | 1:69a8c8a7cf1d | 85 | |
LukeMar | 1:69a8c8a7cf1d | 86 | }//main |
LukeMar | 1:69a8c8a7cf1d | 87 | |
LukeMar | 1:69a8c8a7cf1d | 88 | |
LukeMar | 1:69a8c8a7cf1d | 89 | void Bno_Read() |
LukeMar | 1:69a8c8a7cf1d | 90 | { |
LukeMar | 1:69a8c8a7cf1d | 91 | |
LukeMar | 1:69a8c8a7cf1d | 92 | while(can.read(msg_read)) { //if message is available, read into msg |
LukeMar | 1:69a8c8a7cf1d | 93 | |
LukeMar | 1:69a8c8a7cf1d | 94 | if(msg_read.id == BNO_ID) { //if its the BNO ID |
LukeMar | 1:69a8c8a7cf1d | 95 | |
LukeMar | 1:69a8c8a7cf1d | 96 | if(msg_read.data[0] == 'b' || msg_read.data[0] == 'B') { |
LukeMar | 1:69a8c8a7cf1d | 97 | |
LukeMar | 1:69a8c8a7cf1d | 98 | led1 = !led1; |
LukeMar | 1:69a8c8a7cf1d | 99 | BNOangle = CanValue(); |
LukeMar | 1:69a8c8a7cf1d | 100 | //data = BNOangle; //a similar form of the code everywhere |
LukeMar | 1:69a8c8a7cf1d | 101 | //myFloat.m_float = data; |
LukeMar | 1:69a8c8a7cf1d | 102 | //data = myFloat.m_float; // get the float back from the union |
LukeMar | 1:69a8c8a7cf1d | 103 | pc.printf("BNO: %.1f", BNOangle); //data); |
LukeMar | 1:69a8c8a7cf1d | 104 | |
LukeMar | 1:69a8c8a7cf1d | 105 | }//if the first letter was an 's', it is a servo command |
LukeMar | 1:69a8c8a7cf1d | 106 | } //was the message to this device? |
LukeMar | 1:69a8c8a7cf1d | 107 | |
LukeMar | 1:69a8c8a7cf1d | 108 | }//while a CAN message has been read |
LukeMar | 1:69a8c8a7cf1d | 109 | //return datastr; |
LukeMar | 0:8128714922a1 | 110 | |
LukeMar | 0:8128714922a1 | 111 | } |
LukeMar | 0:8128714922a1 | 112 | |
LukeMar | 1:69a8c8a7cf1d | 113 | void Lat_Read() |
LukeMar | 1:69a8c8a7cf1d | 114 | { |
LukeMar | 1:69a8c8a7cf1d | 115 | |
LukeMar | 1:69a8c8a7cf1d | 116 | while(can.read(msg_read)) { //if message is available, read into msg |
LukeMar | 1:69a8c8a7cf1d | 117 | |
LukeMar | 1:69a8c8a7cf1d | 118 | if(msg_read.id == BNO_ID) { //if its the BNO ID |
LukeMar | 1:69a8c8a7cf1d | 119 | |
LukeMar | 1:69a8c8a7cf1d | 120 | if(msg_read.data[0] == 'L' || msg_read.data[0] == 'l') { |
LukeMar | 1:69a8c8a7cf1d | 121 | |
LukeMar | 1:69a8c8a7cf1d | 122 | //led2 = !led2; |
LukeMar | 1:69a8c8a7cf1d | 123 | BNOangle = CanValue(); |
LukeMar | 1:69a8c8a7cf1d | 124 | //data = BNOangle; //a similar form of the code everywhere |
LukeMar | 1:69a8c8a7cf1d | 125 | //myFloat.m_float = data; |
LukeMar | 1:69a8c8a7cf1d | 126 | //data = myFloat.m_float; // get the float back from the union |
LukeMar | 1:69a8c8a7cf1d | 127 | pc.printf("Lat: %.1f", BNOangle); //data); |
LukeMar | 1:69a8c8a7cf1d | 128 | //Thread::wait(ff); |
LukeMar | 1:69a8c8a7cf1d | 129 | }//if the first letter was an 's', it is a servo command |
LukeMar | 1:69a8c8a7cf1d | 130 | } //was the message to this device? |
LukeMar | 1:69a8c8a7cf1d | 131 | }//while a CAN message has been read |
LukeMar | 1:69a8c8a7cf1d | 132 | //return datastr; |
LukeMar | 1:69a8c8a7cf1d | 133 | |
LukeMar | 1:69a8c8a7cf1d | 134 | } |
LukeMar | 1:69a8c8a7cf1d | 135 | |
LukeMar | 1:69a8c8a7cf1d | 136 | void Long_Read() |
LukeMar | 1:69a8c8a7cf1d | 137 | { |
LukeMar | 1:69a8c8a7cf1d | 138 | |
LukeMar | 1:69a8c8a7cf1d | 139 | while(can.read(msg_read)) { //if message is available, read into msg |
LukeMar | 1:69a8c8a7cf1d | 140 | |
LukeMar | 1:69a8c8a7cf1d | 141 | //if(msg_read.id == BNO_ID) { //if its the BNO ID |
LukeMar | 1:69a8c8a7cf1d | 142 | |
LukeMar | 1:69a8c8a7cf1d | 143 | if(msg_read.data[0] == 'O' || msg_read.data[0] == 'o') { |
LukeMar | 1:69a8c8a7cf1d | 144 | |
LukeMar | 1:69a8c8a7cf1d | 145 | //led2 = !led2; |
LukeMar | 1:69a8c8a7cf1d | 146 | BNOangle = CanValue(); |
LukeMar | 1:69a8c8a7cf1d | 147 | //data = BNOangle; //a similar form of the code everywhere |
LukeMar | 1:69a8c8a7cf1d | 148 | //myFloat.m_float = data; |
LukeMar | 1:69a8c8a7cf1d | 149 | //data = myFloat.m_float; // get the float back from the union |
LukeMar | 1:69a8c8a7cf1d | 150 | pc.printf("Long: %.1f", BNOangle); //data); |
LukeMar | 1:69a8c8a7cf1d | 151 | //Thread::wait(ff); |
LukeMar | 1:69a8c8a7cf1d | 152 | }//if the first letter was an 's', it is a servo command |
LukeMar | 1:69a8c8a7cf1d | 153 | //} //was the message to this device? |
LukeMar | 1:69a8c8a7cf1d | 154 | }//while a CAN message has been read |
LukeMar | 1:69a8c8a7cf1d | 155 | //return datastr; |
LukeMar | 1:69a8c8a7cf1d | 156 | |
LukeMar | 1:69a8c8a7cf1d | 157 | } |
LukeMar | 1:69a8c8a7cf1d | 158 | |
LukeMar | 1:69a8c8a7cf1d | 159 | void Time_Read() |
LukeMar | 1:69a8c8a7cf1d | 160 | { |
LukeMar | 1:69a8c8a7cf1d | 161 | float time; |
LukeMar | 1:69a8c8a7cf1d | 162 | while(can.read(msg_read)) { //if message is available, read into msg |
LukeMar | 1:69a8c8a7cf1d | 163 | |
LukeMar | 1:69a8c8a7cf1d | 164 | //if(msg_read.id == BNO_ID) { //if its the BNO ID |
LukeMar | 1:69a8c8a7cf1d | 165 | |
LukeMar | 1:69a8c8a7cf1d | 166 | if(msg_read.data[0] == 't' || msg_read.data[0] == 'T') { |
LukeMar | 1:69a8c8a7cf1d | 167 | |
LukeMar | 1:69a8c8a7cf1d | 168 | led2 = !led2; |
LukeMar | 1:69a8c8a7cf1d | 169 | time = CanValue(); |
LukeMar | 1:69a8c8a7cf1d | 170 | //data = BNOangle; //a similar form of the code everywhere |
LukeMar | 1:69a8c8a7cf1d | 171 | //myFloat.m_float = data; |
LukeMar | 1:69a8c8a7cf1d | 172 | //data = myFloat.m_float; // get the float back from the union |
LukeMar | 1:69a8c8a7cf1d | 173 | pc.printf("Time: %.1f", time); //data); |
LukeMar | 1:69a8c8a7cf1d | 174 | //Thread::wait(ff); |
LukeMar | 1:69a8c8a7cf1d | 175 | }//if the first letter was an 's', it is a servo command |
LukeMar | 1:69a8c8a7cf1d | 176 | // } //was the message to this device? |
LukeMar | 1:69a8c8a7cf1d | 177 | }//while a CAN message has been read |
LukeMar | 1:69a8c8a7cf1d | 178 | //return datastr; |
LukeMar | 1:69a8c8a7cf1d | 179 | |
LukeMar | 1:69a8c8a7cf1d | 180 | } |
LukeMar | 1:69a8c8a7cf1d | 181 | |
LukeMar | 1:69a8c8a7cf1d | 182 | void Rudder_Read() |
LukeMar | 1:69a8c8a7cf1d | 183 | { |
LukeMar | 1:69a8c8a7cf1d | 184 | |
LukeMar | 1:69a8c8a7cf1d | 185 | while(can.read(msg_read)) { //if message is available, read into msg |
LukeMar | 1:69a8c8a7cf1d | 186 | //if(msg_read.id == RC_ID) { //if its the BNO ID |
LukeMar | 1:69a8c8a7cf1d | 187 | if(msg_read.data[0] == 'r' || msg_read.data[0] == 'R') { |
LukeMar | 1:69a8c8a7cf1d | 188 | led3 = !led3; |
LukeMar | 1:69a8c8a7cf1d | 189 | RC_out = CanValue(); //I know this is redundant but i use |
LukeMar | 1:69a8c8a7cf1d | 190 | //data = RC_out; //a similar form of the code everywhere |
LukeMar | 1:69a8c8a7cf1d | 191 | //myFloat.m_float = data; |
LukeMar | 1:69a8c8a7cf1d | 192 | //data = myFloat.m_float; // get the float back from the union |
LukeMar | 1:69a8c8a7cf1d | 193 | pc.printf("Rudder: %.1f", RC_out); //data); |
LukeMar | 1:69a8c8a7cf1d | 194 | //Thread::wait(ff); |
LukeMar | 1:69a8c8a7cf1d | 195 | }//if the first letter was an 's', it is a servo command |
LukeMar | 1:69a8c8a7cf1d | 196 | // } //was the message to this device? |
LukeMar | 1:69a8c8a7cf1d | 197 | }//while a CAN message has been read |
LukeMar | 1:69a8c8a7cf1d | 198 | //return datastr; |
LukeMar | 1:69a8c8a7cf1d | 199 | |
LukeMar | 1:69a8c8a7cf1d | 200 | } |
LukeMar | 1:69a8c8a7cf1d | 201 | |
LukeMar | 1:69a8c8a7cf1d | 202 | void Mast_Read() |
LukeMar | 1:69a8c8a7cf1d | 203 | { |
LukeMar | 1:69a8c8a7cf1d | 204 | |
LukeMar | 1:69a8c8a7cf1d | 205 | while(can.read(msg_read)) { //if message is available, read into msg |
LukeMar | 1:69a8c8a7cf1d | 206 | // if(msg_read.id == RC_ID) { //if its the BNO ID |
LukeMar | 1:69a8c8a7cf1d | 207 | if(msg_read.data[0] == 'm' || msg_read.data[0] == 'M') { |
LukeMar | 1:69a8c8a7cf1d | 208 | led4 = !led4; |
LukeMar | 1:69a8c8a7cf1d | 209 | RC_out = CanValue(); //I know this is redundant but i use |
LukeMar | 1:69a8c8a7cf1d | 210 | //data = RC_out; //a similar form of the code everywhere |
LukeMar | 1:69a8c8a7cf1d | 211 | //myFloat.m_float = data; |
LukeMar | 1:69a8c8a7cf1d | 212 | //data = myFloat.m_float; // get the float back from the union |
LukeMar | 1:69a8c8a7cf1d | 213 | pc.printf("Mast: %.1f\n", RC_out); //data); |
LukeMar | 1:69a8c8a7cf1d | 214 | //Thread::wait(ff); |
LukeMar | 1:69a8c8a7cf1d | 215 | }//if the first letter was an 's', it is a servo command |
LukeMar | 1:69a8c8a7cf1d | 216 | //} //was the message to this device? |
LukeMar | 1:69a8c8a7cf1d | 217 | }//while a CAN message has been read |
LukeMar | 1:69a8c8a7cf1d | 218 | //return datastr; |
LukeMar | 1:69a8c8a7cf1d | 219 | |
LukeMar | 1:69a8c8a7cf1d | 220 | } |