University of Texas Solar Vehicles Team / Mbed 2 deprecated motor-control

Dependencies:   mbed

Committer:
jjohnle
Date:
Sat Nov 09 18:50:12 2019 +0000
Revision:
3:f282664610ba
Parent:
2:2a4822c7c91a
Child:
4:7d027ceba0a0
works;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jjohnle 3:f282664610ba 1 // Motor Control Board Program
jjohnle 3:f282664610ba 2 // This program operates the Tritium controller and also sends data
jjohnle 3:f282664610ba 3 // over the separate car CAN.
jjohnle 3:f282664610ba 4
jjohnle 2:2a4822c7c91a 5 #include "mbed.h"
jjohnle 1:863bdd011cf8 6
jjohnle 3:f282664610ba 7 // CAN base address and offsets (Tritium)
jjohnle 0:c61d18b01f8c 8 #define DC_BASE 0x220 // Driver controls base address
jjohnle 0:c61d18b01f8c 9 #define DC_DRIVE 0x01 // Offset for motor drive command
jjohnle 0:c61d18b01f8c 10 #define DC_POWER 0x02 // Offset for motor power command
jjohnle 0:c61d18b01f8c 11 #define DC_RESET 0x03 // Offset for reset command
jjohnle 0:c61d18b01f8c 12 #define DC_SWITCH 0x04 // Offset for phase current measurement
jjohnle 0:c61d18b01f8c 13
jjohnle 3:f282664610ba 14 #define MC_BASE 0x240 // Motor controls base address
jjohnle 3:f282664610ba 15 #define MC_BUS 0x02 // Bus measurement offset
jjohnle 3:f282664610ba 16 #define MC_VEL 0x03 // Velocity measurement offset
jjohnle 3:f282664610ba 17 #define MC_PHCUR 0x04 // Phase Current offset
jjohnle 3:f282664610ba 18 #define MC_VOVEC 0x05 // Voltage Vector offset
jjohnle 3:f282664610ba 19 #define MC_CUVEC 0x06 // current vector offset
jjohnle 3:f282664610ba 20 #define MC_BEMF 0x07 // back emf offset
jjohnle 3:f282664610ba 21 #define MC_TEMP 0x0B // heat sink and motor temp offset
jjohnle 3:f282664610ba 22 #define MC_AMPH 0x0E // odometer and bus amp ohours measuremeant
jjohnle 0:c61d18b01f8c 23 #define MAX_VELOCITY 100 // motor velocity in m/s
jjohnle 0:c61d18b01f8c 24 #define MAX_CURRENT 1.0 // desired motor current as percentage of max current
jjohnle 0:c61d18b01f8c 25
jjohnle 3:f282664610ba 26 #define DC_BUS_CURRENT 0x900
jjohnle 3:f282664610ba 27 #define DC_BUS_VOLTAGE 0x901
jjohnle 3:f282664610ba 28 #define PHASE_B_CURRENT 0x902
jjohnle 3:f282664610ba 29 #define PHASE_C_CURRENT 0x903
jjohnle 3:f282664610ba 30 #define VEHICLE_VELOCITY 0x904
jjohnle 3:f282664610ba 31 #define MOTOR_VELOCITY 0x905
jjohnle 3:f282664610ba 32 #define VD 0x906
jjohnle 3:f282664610ba 33 #define VQ 0x907
jjohnle 3:f282664610ba 34 #define ID 0x908
jjohnle 3:f282664610ba 35 #define IQ 0x909
jjohnle 3:f282664610ba 36 #define BEMFD 0x90A
jjohnle 3:f282664610ba 37 #define BEMFQ 0x90B
jjohnle 3:f282664610ba 38 #define HEAT_SINK_TEMPERATURE 0x90C
jjohnle 3:f282664610ba 39 #define MOTOR_TEMPERATURE 0x90D
jjohnle 3:f282664610ba 40 #define DC_BUS_AMP_HOURS 0x90E
jjohnle 3:f282664610ba 41 #define ODOMETER 0x90F
jjohnle 0:c61d18b01f8c 42
jjohnle 3:f282664610ba 43 CAN can1(PD_0,PD_1,125000); // can1 is car CAN (Rx, Tx, speed)
jjohnle 3:f282664610ba 44 CAN can2(PB_5,PB_6,50000); // can2 is motor controller CAN (Rx, Tx, speed)
jjohnle 0:c61d18b01f8c 45 Serial pc(USBTX, USBRX);
jjohnle 0:c61d18b01f8c 46
jjohnle 3:f282664610ba 47 AnalogIn ain(PB_0);
jjohnle 3:f282664610ba 48
jjohnle 3:f282664610ba 49 DigitalOut LED8(PF_2);
jjohnle 3:f282664610ba 50 DigitalOut LED7(PA_7);
jjohnle 3:f282664610ba 51 DigitalOut LED6(PF_10);
jjohnle 3:f282664610ba 52 DigitalOut LED5(PF_5);
jjohnle 3:f282664610ba 53 DigitalOut LED4a(PF_3);
jjohnle 3:f282664610ba 54 DigitalOut LED3a(PC_3);
jjohnle 3:f282664610ba 55 DigitalOut LED2a(PC_0);
jjohnle 3:f282664610ba 56 DigitalOut LED1a(PA_3);
jjohnle 3:f282664610ba 57
jjohnle 3:f282664610ba 58 #define MAX_VELOCITY 100 // motor velocity in m/s
jjohnle 3:f282664610ba 59 #define MAX_CURRENT 1.0 // desired motor current as percentage of max current
jjohnle 3:f282664610ba 60
jjohnle 3:f282664610ba 61 int main() {
jjohnle 3:f282664610ba 62 float current = MAX_CURRENT;
jjohnle 3:f282664610ba 63 float velocity = MAX_VELOCITY;
jjohnle 3:f282664610ba 64 float bus_current = MAX_CURRENT;
jjohnle 3:f282664610ba 65 float DCbuscur;
jjohnle 3:f282664610ba 66 float DCbusvolt;
jjohnle 3:f282664610ba 67 double pedal_position;
jjohnle 3:f282664610ba 68 float data[2];
jjohnle 3:f282664610ba 69 float data2[2];
jjohnle 3:f282664610ba 70 float meas;
jjohnle 3:f282664610ba 71 double avgval;
jjohnle 3:f282664610ba 72 int n;
jjohnle 3:f282664610ba 73 int dummy;
jjohnle 3:f282664610ba 74 int alive;
jjohnle 1:863bdd011cf8 75
jjohnle 3:f282664610ba 76 // other ids we need to read
jjohnle 3:f282664610ba 77 float phaseBcurrent;
jjohnle 3:f282664610ba 78 float phaseCcurrent;
jjohnle 3:f282664610ba 79 float vehicleVel;
jjohnle 3:f282664610ba 80 float motorVel;
jjohnle 3:f282664610ba 81 float vd;
jjohnle 3:f282664610ba 82 float vq;
jjohnle 3:f282664610ba 83 float Id;
jjohnle 3:f282664610ba 84 float Iq;
jjohnle 3:f282664610ba 85 float BEMFd;
jjohnle 3:f282664610ba 86 float BEMFq;
jjohnle 3:f282664610ba 87 float heatSinkTemp;
jjohnle 3:f282664610ba 88 float motorTemp;
jjohnle 3:f282664610ba 89 float DCBusAmpHours;
jjohnle 3:f282664610ba 90 float odometerValue;
jjohnle 1:863bdd011cf8 91
jjohnle 3:f282664610ba 92 //char const * serial = "0002173";
jjohnle 3:f282664610ba 93 // can1.frequency(500000);
jjohnle 3:f282664610ba 94 int id;
jjohnle 3:f282664610ba 95 int id2;
jjohnle 3:f282664610ba 96 int id3;
jjohnle 3:f282664610ba 97 char rdata[8];
jjohnle 3:f282664610ba 98 char rdata2[8];
jjohnle 3:f282664610ba 99 char rdata3[8];
jjohnle 3:f282664610ba 100 char rdata4[8];
jjohnle 3:f282664610ba 101 char rdata5[8];
jjohnle 3:f282664610ba 102 char rdata6[8];
jjohnle 3:f282664610ba 103 char rdata7[8];
jjohnle 3:f282664610ba 104 char rdata8[8];
jjohnle 3:f282664610ba 105 char rdata9[8];
jjohnle 0:c61d18b01f8c 106
jjohnle 3:f282664610ba 107 CANMessage msg;
jjohnle 3:f282664610ba 108 dummy = 0;
jjohnle 3:f282664610ba 109 alive = 0;
jjohnle 0:c61d18b01f8c 110
jjohnle 3:f282664610ba 111 union {
jjohnle 3:f282664610ba 112 char rcvdata[4];
jjohnle 3:f282664610ba 113 float rxdata;
jjohnle 3:f282664610ba 114 } urxdata;
jjohnle 0:c61d18b01f8c 115
jjohnle 3:f282664610ba 116 id = DC_BASE + DC_DRIVE;
jjohnle 3:f282664610ba 117 id2 = DC_BASE + DC_POWER;
jjohnle 3:f282664610ba 118 id3 = MC_BASE + DC_POWER;
jjohnle 0:c61d18b01f8c 119
jjohnle 3:f282664610ba 120 while (1) {
jjohnle 3:f282664610ba 121 n = 0;
jjohnle 3:f282664610ba 122 avgval = 0.0;
jjohnle 3:f282664610ba 123 while(n < 100) {
jjohnle 3:f282664610ba 124 meas = ain.read();
jjohnle 3:f282664610ba 125 avgval = avgval + meas;
jjohnle 3:f282664610ba 126 n++ ;
jjohnle 2:2a4822c7c91a 127 }
jjohnle 3:f282664610ba 128 pedal_position = avgval/100.0;
jjohnle 0:c61d18b01f8c 129
jjohnle 3:f282664610ba 130 if(pedal_position > 0.005)
jjohnle 3:f282664610ba 131 LED1a = 1;
jjohnle 3:f282664610ba 132 else
jjohnle 3:f282664610ba 133 LED1a = 0;
jjohnle 3:f282664610ba 134 if(pedal_position > 0.01)
jjohnle 3:f282664610ba 135 LED2a = 1;
jjohnle 3:f282664610ba 136 else
jjohnle 3:f282664610ba 137 LED2a = 0;
jjohnle 3:f282664610ba 138 if(pedal_position > 0.015)
jjohnle 3:f282664610ba 139 LED3a = 1;
jjohnle 3:f282664610ba 140 else
jjohnle 3:f282664610ba 141 LED3a = 0;
jjohnle 3:f282664610ba 142 if(pedal_position > 0.02)
jjohnle 3:f282664610ba 143 LED4a = 1;
jjohnle 3:f282664610ba 144 else
jjohnle 3:f282664610ba 145 LED4a = 0;
jjohnle 3:f282664610ba 146 if(pedal_position > 0.025)
jjohnle 3:f282664610ba 147 LED5 = 1;
jjohnle 3:f282664610ba 148 else
jjohnle 3:f282664610ba 149 LED5 = 0;
jjohnle 3:f282664610ba 150 if(pedal_position > 0.03)
jjohnle 3:f282664610ba 151 LED6 = 1;
jjohnle 3:f282664610ba 152 else
jjohnle 3:f282664610ba 153 LED6 = 0;
jjohnle 3:f282664610ba 154 if(pedal_position > 0.035)
jjohnle 3:f282664610ba 155 LED7 = 1;
jjohnle 3:f282664610ba 156 else
jjohnle 3:f282664610ba 157 LED7 = 0;
jjohnle 3:f282664610ba 158
jjohnle 3:f282664610ba 159 current = MAX_CURRENT * pedal_position;
jjohnle 3:f282664610ba 160 velocity = 9.0;
jjohnle 3:f282664610ba 161
jjohnle 3:f282664610ba 162 data[1] = current; // Flipped because of endianness
jjohnle 3:f282664610ba 163 data[0] = velocity;
jjohnle 3:f282664610ba 164
jjohnle 3:f282664610ba 165 if (!can2.write(CANMessage(id, (char*)data, 8))) // send current and velocity to Tritum
jjohnle 3:f282664610ba 166 printf("Drive failed \n\r");
jjohnle 3:f282664610ba 167
jjohnle 3:f282664610ba 168 data2[1] = bus_current;
jjohnle 3:f282664610ba 169 data2[0] = 0.0;
jjohnle 3:f282664610ba 170 if (!can2.write(CANMessage(id2, (char*)data2, 8)))
jjohnle 3:f282664610ba 171 dummy = 0;
jjohnle 3:f282664610ba 172
jjohnle 3:f282664610ba 173 wait_ms(10); // Need message every 250ms to maintain operation
jjohnle 3:f282664610ba 174
jjohnle 3:f282664610ba 175 // WE ARE READING STUFFF HERE //
jjohnle 3:f282664610ba 176 if(can2.read(msg) && msg.id == id3 ) { // Tritium Bus
jjohnle 3:f282664610ba 177 for(int i = 0; i < msg.len; i++) {
jjohnle 3:f282664610ba 178 rdata[i] = msg.data[i];
jjohnle 0:c61d18b01f8c 179 }
jjohnle 0:c61d18b01f8c 180
jjohnle 3:f282664610ba 181 // sending value to CAR can
jjohnle 3:f282664610ba 182 if (!can1.write(CANMessage(0x242, (char*) rdata, 8))) {
jjohnle 3:f282664610ba 183 pc.printf("Cannot write to CAN\n");
jjohnle 3:f282664610ba 184 }
jjohnle 3:f282664610ba 185 wait(0.1);
jjohnle 3:f282664610ba 186
jjohnle 3:f282664610ba 187 urxdata.rcvdata[3] = rdata[7];
jjohnle 3:f282664610ba 188 urxdata.rcvdata[2] = rdata[6];
jjohnle 3:f282664610ba 189 urxdata.rcvdata[1] = rdata[5];
jjohnle 3:f282664610ba 190 urxdata.rcvdata[0] = rdata[4];
jjohnle 3:f282664610ba 191 DCbuscur = urxdata.rxdata;
jjohnle 3:f282664610ba 192 urxdata.rcvdata[3] = rdata[3];
jjohnle 3:f282664610ba 193 urxdata.rcvdata[2] = rdata[2];
jjohnle 3:f282664610ba 194 urxdata.rcvdata[1] = rdata[1];
jjohnle 3:f282664610ba 195 urxdata.rcvdata[0] = rdata[0];
jjohnle 3:f282664610ba 196 DCbusvolt = urxdata.rxdata;
jjohnle 3:f282664610ba 197 }
jjohnle 3:f282664610ba 198
jjohnle 3:f282664610ba 199
jjohnle 3:f282664610ba 200 // reading vehicle and motor velocity
jjohnle 3:f282664610ba 201 else if(can2.read(msg) && msg.id == (MC_BASE+MC_VEL)) {
jjohnle 3:f282664610ba 202 for (int i = 0; i < msg.len; i++) {
jjohnle 3:f282664610ba 203 rdata2[i] = msg.data[i];
jjohnle 3:f282664610ba 204 }
jjohnle 3:f282664610ba 205
jjohnle 3:f282664610ba 206 // sending value to CAR can
jjohnle 3:f282664610ba 207 if (!can1.write(CANMessage(0x243, (char*) rdata2, 8))) {
jjohnle 3:f282664610ba 208 pc.printf("Cannot write to CAN\n");
jjohnle 3:f282664610ba 209 }
jjohnle 3:f282664610ba 210 wait(0.1);
jjohnle 3:f282664610ba 211
jjohnle 3:f282664610ba 212 urxdata.rcvdata[3] = rdata2[7];
jjohnle 3:f282664610ba 213 urxdata.rcvdata[2] = rdata2[6];
jjohnle 3:f282664610ba 214 urxdata.rcvdata[1] = rdata2[5];
jjohnle 3:f282664610ba 215 urxdata.rcvdata[0] = rdata2[4];
jjohnle 3:f282664610ba 216 vehicleVel = urxdata.rxdata;
jjohnle 3:f282664610ba 217 urxdata.rcvdata[3] = rdata2[3];
jjohnle 3:f282664610ba 218 urxdata.rcvdata[2] = rdata2[2];
jjohnle 3:f282664610ba 219 urxdata.rcvdata[1] = rdata2[1];
jjohnle 3:f282664610ba 220 urxdata.rcvdata[0] = rdata2[0];
jjohnle 3:f282664610ba 221 motorVel = urxdata.rxdata;
jjohnle 3:f282664610ba 222 }
jjohnle 3:f282664610ba 223
jjohnle 0:c61d18b01f8c 224 // reading phase currents
jjohnle 3:f282664610ba 225 else if(can2.read(msg) && msg.id == (MC_BASE+MC_PHCUR)) {
jjohnle 3:f282664610ba 226 for (int i = 0; i < msg.len; i++) {
jjohnle 3:f282664610ba 227 rdata3[i] = msg.data[i];
jjohnle 3:f282664610ba 228 }
jjohnle 3:f282664610ba 229
jjohnle 3:f282664610ba 230 // sending value to CAR can
jjohnle 3:f282664610ba 231 if (!can1.write(CANMessage(0x244, (char*) rdata3, 8))) {
jjohnle 3:f282664610ba 232 pc.printf("Cannot write to CAN\n");
jjohnle 3:f282664610ba 233 }
jjohnle 3:f282664610ba 234 wait(0.1);
jjohnle 3:f282664610ba 235
jjohnle 3:f282664610ba 236 urxdata.rcvdata[3] = rdata3[7];
jjohnle 3:f282664610ba 237 urxdata.rcvdata[2] = rdata3[6];
jjohnle 3:f282664610ba 238 urxdata.rcvdata[1] = rdata3[5];
jjohnle 3:f282664610ba 239 urxdata.rcvdata[0] = rdata3[4];
jjohnle 3:f282664610ba 240 phaseCcurrent = urxdata.rxdata;
jjohnle 3:f282664610ba 241 urxdata.rcvdata[3] = rdata3[3];
jjohnle 3:f282664610ba 242 urxdata.rcvdata[2] = rdata3[2];
jjohnle 3:f282664610ba 243 urxdata.rcvdata[1] = rdata3[1];
jjohnle 3:f282664610ba 244 urxdata.rcvdata[0] = rdata3[0];
jjohnle 3:f282664610ba 245 phaseBcurrent = urxdata.rxdata;
jjohnle 0:c61d18b01f8c 246 }
jjohnle 0:c61d18b01f8c 247
jjohnle 0:c61d18b01f8c 248 // reading motor voltage vector
jjohnle 3:f282664610ba 249 else if(can2.read(msg) && msg.id == (MC_BASE+MC_VOVEC)) {
jjohnle 3:f282664610ba 250 for (int i = 0; i < msg.len; i++) {
jjohnle 3:f282664610ba 251 rdata4[i] = msg.data[i];
jjohnle 0:c61d18b01f8c 252 }
jjohnle 0:c61d18b01f8c 253
jjohnle 3:f282664610ba 254 // sending value to CAR can
jjohnle 3:f282664610ba 255 if (!can1.write(CANMessage(0x245, (char*) rdata4, 8))) {
jjohnle 3:f282664610ba 256 pc.printf("Cannot write to CAN\n");
jjohnle 0:c61d18b01f8c 257 }
jjohnle 3:f282664610ba 258 wait(0.1);
jjohnle 0:c61d18b01f8c 259
jjohnle 3:f282664610ba 260 urxdata.rcvdata[3] = rdata4[7];
jjohnle 3:f282664610ba 261 urxdata.rcvdata[2] = rdata4[6];
jjohnle 3:f282664610ba 262 urxdata.rcvdata[1] = rdata4[5];
jjohnle 3:f282664610ba 263 urxdata.rcvdata[0] = rdata4[4];
jjohnle 3:f282664610ba 264 vd = urxdata.rxdata;
jjohnle 3:f282664610ba 265 urxdata.rcvdata[3] = rdata4[3];
jjohnle 3:f282664610ba 266 urxdata.rcvdata[2] = rdata4[2];
jjohnle 3:f282664610ba 267 urxdata.rcvdata[1] = rdata4[1];
jjohnle 3:f282664610ba 268 urxdata.rcvdata[0] = rdata4[0];
jjohnle 3:f282664610ba 269 vq = urxdata.rxdata;
jjohnle 3:f282664610ba 270 }
jjohnle 3:f282664610ba 271
jjohnle 3:f282664610ba 272 // reading current vector
jjohnle 3:f282664610ba 273 else if(can2.read(msg) && msg.id == (MC_BASE+MC_CUVEC)) {
jjohnle 3:f282664610ba 274 for (int i = 0; i < msg.len; i++) {
jjohnle 3:f282664610ba 275 rdata5[i] = msg.data[i];
jjohnle 0:c61d18b01f8c 276 }
jjohnle 0:c61d18b01f8c 277
jjohnle 3:f282664610ba 278 // sending value to CAR can
jjohnle 3:f282664610ba 279 if (!can1.write(CANMessage(0x246, (char*) rdata5, 8))) {
jjohnle 3:f282664610ba 280 pc.printf("Cannot write to CAN\n");
jjohnle 3:f282664610ba 281 }
jjohnle 3:f282664610ba 282 wait(0.1);
jjohnle 3:f282664610ba 283
jjohnle 3:f282664610ba 284 urxdata.rcvdata[3] = rdata5[7];
jjohnle 3:f282664610ba 285 urxdata.rcvdata[2] = rdata5[6];
jjohnle 3:f282664610ba 286 urxdata.rcvdata[1] = rdata5[5];
jjohnle 3:f282664610ba 287 urxdata.rcvdata[0] = rdata5[4];
jjohnle 3:f282664610ba 288 Id = urxdata.rxdata;
jjohnle 3:f282664610ba 289 urxdata.rcvdata[3] = rdata5[3];
jjohnle 3:f282664610ba 290 urxdata.rcvdata[2] = rdata5[2];
jjohnle 3:f282664610ba 291 urxdata.rcvdata[1] = rdata5[1];
jjohnle 3:f282664610ba 292 urxdata.rcvdata[0] = rdata5[0];
jjohnle 3:f282664610ba 293 Iq = urxdata.rxdata;
jjohnle 3:f282664610ba 294 }
jjohnle 3:f282664610ba 295
jjohnle 3:f282664610ba 296 // reading back emf
jjohnle 3:f282664610ba 297 else if(can2.read(msg) && msg.id == (MC_BASE+MC_BEMF)) {
jjohnle 3:f282664610ba 298 for (int i = 0; i < msg.len; i++) {
jjohnle 3:f282664610ba 299 rdata6[i] = msg.data[i];
jjohnle 0:c61d18b01f8c 300 }
jjohnle 0:c61d18b01f8c 301
jjohnle 3:f282664610ba 302 // sending value to CAR can
jjohnle 3:f282664610ba 303 if (!can1.write(CANMessage(0x247, (char*) rdata6, 8))) {
jjohnle 3:f282664610ba 304 pc.printf("Cannot write to CAN\n");
jjohnle 3:f282664610ba 305 }
jjohnle 3:f282664610ba 306 wait(0.1);
jjohnle 0:c61d18b01f8c 307
jjohnle 3:f282664610ba 308 urxdata.rcvdata[3] = rdata6[7];
jjohnle 3:f282664610ba 309 urxdata.rcvdata[2] = rdata6[6];
jjohnle 3:f282664610ba 310 urxdata.rcvdata[1] = rdata6[5];
jjohnle 3:f282664610ba 311 urxdata.rcvdata[0] = rdata6[4];
jjohnle 3:f282664610ba 312 BEMFd = urxdata.rxdata;
jjohnle 3:f282664610ba 313 urxdata.rcvdata[3] = rdata6[3];
jjohnle 3:f282664610ba 314 urxdata.rcvdata[2] = rdata6[2];
jjohnle 3:f282664610ba 315 urxdata.rcvdata[1] = rdata6[1];
jjohnle 3:f282664610ba 316 urxdata.rcvdata[0] = rdata6[0];
jjohnle 3:f282664610ba 317 BEMFq = urxdata.rxdata;
jjohnle 2:2a4822c7c91a 318 }
jjohnle 2:2a4822c7c91a 319
jjohnle 3:f282664610ba 320 // reading heatsink and motor temp
jjohnle 3:f282664610ba 321 else if(can2.read(msg) && msg.id == (MC_BASE+MC_TEMP)) {
jjohnle 3:f282664610ba 322 for (int i = 0; i < msg.len; i++) {
jjohnle 3:f282664610ba 323 rdata7[i] = msg.data[i];
jjohnle 2:2a4822c7c91a 324 }
jjohnle 2:2a4822c7c91a 325
jjohnle 3:f282664610ba 326 // sending value to CAR can
jjohnle 3:f282664610ba 327 if (!can1.write(CANMessage(0x24B, (char*) rdata7, 8))) {
jjohnle 3:f282664610ba 328 pc.printf("Cannot write to CAN\n");
jjohnle 2:2a4822c7c91a 329 }
jjohnle 3:f282664610ba 330 wait(0.1);
jjohnle 2:2a4822c7c91a 331
jjohnle 3:f282664610ba 332 urxdata.rcvdata[3] = rdata7[7];
jjohnle 3:f282664610ba 333 urxdata.rcvdata[2] = rdata7[6];
jjohnle 3:f282664610ba 334 urxdata.rcvdata[1] = rdata7[5];
jjohnle 3:f282664610ba 335 urxdata.rcvdata[0] = rdata7[4];
jjohnle 3:f282664610ba 336 heatSinkTemp = urxdata.rxdata;
jjohnle 3:f282664610ba 337 urxdata.rcvdata[3] = rdata7[3];
jjohnle 3:f282664610ba 338 urxdata.rcvdata[2] = rdata7[2];
jjohnle 3:f282664610ba 339 urxdata.rcvdata[1] = rdata7[1];
jjohnle 3:f282664610ba 340 urxdata.rcvdata[0] = rdata7[0];
jjohnle 3:f282664610ba 341 motorTemp = urxdata.rxdata;
jjohnle 3:f282664610ba 342 wait_ms(10); // wait to reset
jjohnle 3:f282664610ba 343 }
jjohnle 2:2a4822c7c91a 344
jjohnle 3:f282664610ba 345 // reading odometer and bus amp ohours measuremeant
jjohnle 3:f282664610ba 346 else if(can2.read(msg) && msg.id == (MC_BASE+MC_AMPH)) {
jjohnle 3:f282664610ba 347 for (int i = 0; i < msg.len; i++) {
jjohnle 3:f282664610ba 348 rdata8[i] = msg.data[i];
jjohnle 2:2a4822c7c91a 349 }
jjohnle 2:2a4822c7c91a 350
jjohnle 3:f282664610ba 351 // sending value to CAR can
jjohnle 3:f282664610ba 352 if (!can1.write(CANMessage(0x24E, (char*) rdata8, 8))) {
jjohnle 3:f282664610ba 353 pc.printf("Cannot write to CAN\n");
jjohnle 2:2a4822c7c91a 354 }
jjohnle 2:2a4822c7c91a 355 wait(0.1);
jjohnle 3:f282664610ba 356
jjohnle 3:f282664610ba 357 urxdata.rcvdata[3] = rdata8[7];
jjohnle 3:f282664610ba 358 urxdata.rcvdata[2] = rdata8[6];
jjohnle 3:f282664610ba 359 urxdata.rcvdata[1] = rdata8[5];
jjohnle 3:f282664610ba 360 urxdata.rcvdata[0] = rdata8[4];
jjohnle 3:f282664610ba 361 DCBusAmpHours = urxdata.rxdata;
jjohnle 3:f282664610ba 362 urxdata.rcvdata[3] = rdata8[3];
jjohnle 3:f282664610ba 363 urxdata.rcvdata[2] = rdata8[2];
jjohnle 3:f282664610ba 364 urxdata.rcvdata[1] = rdata8[1];
jjohnle 3:f282664610ba 365 urxdata.rcvdata[0] = rdata8[0];
jjohnle 3:f282664610ba 366 odometerValue = urxdata.rxdata;
jjohnle 2:2a4822c7c91a 367 }
jjohnle 3:f282664610ba 368
jjohnle 3:f282664610ba 369 if(alive % 100 == 0){
jjohnle 3:f282664610ba 370 printf("Motor board is running \n\r");
jjohnle 3:f282664610ba 371 printf(" Requested Motor Current: %f\n\r", current);
jjohnle 3:f282664610ba 372 printf(" Requested Motor Velocity: %f\n\r", velocity);
jjohnle 3:f282664610ba 373 printf(" DC Bus Current (A) = %f",DCbuscur);
jjohnle 3:f282664610ba 374 printf("\r\n");
jjohnle 3:f282664610ba 375 printf(" DC Bus Voltage (V) = %f",DCbusvolt);
jjohnle 3:f282664610ba 376 printf("\r\n");
jjohnle 3:f282664610ba 377
jjohnle 3:f282664610ba 378 // Printing other values
jjohnle 3:f282664610ba 379 printf(" Vehicle Velocity (RPM) = %f",vehicleVel);
jjohnle 3:f282664610ba 380 printf("\r\n");
jjohnle 3:f282664610ba 381 printf(" Motor Velocity (V) = %f",motorVel);
jjohnle 3:f282664610ba 382 printf("\r\n");
jjohnle 3:f282664610ba 383 printf(" Phase B Current (A-rms) = %f",phaseBcurrent);
jjohnle 3:f282664610ba 384 printf("\r\n");
jjohnle 3:f282664610ba 385 printf(" Phase C Current (A-rms) = %f",phaseCcurrent);
jjohnle 3:f282664610ba 386 printf("\r\n");
jjohnle 3:f282664610ba 387 printf(" Vd (V) = %f",vd);
jjohnle 3:f282664610ba 388 printf("\r\n");
jjohnle 3:f282664610ba 389 printf(" Vq (V) = %f",vq);
jjohnle 3:f282664610ba 390 printf("\r\n");
jjohnle 3:f282664610ba 391
jjohnle 3:f282664610ba 392 printf(" Id (A) = %f",Id);
jjohnle 3:f282664610ba 393 printf("\r\n");
jjohnle 3:f282664610ba 394 printf(" Iq (A) = %f",Iq);
jjohnle 3:f282664610ba 395 printf("\r\n");
jjohnle 3:f282664610ba 396 printf(" BEMFd (V) = %f",BEMFd);
jjohnle 3:f282664610ba 397 printf("\r\n");
jjohnle 3:f282664610ba 398 printf(" BEMFq (V) = %f",BEMFq);
jjohnle 3:f282664610ba 399 printf("\r\n");
jjohnle 3:f282664610ba 400 printf(" Heat Sink Temperature (Celsius) = %f",heatSinkTemp);
jjohnle 3:f282664610ba 401 printf("\r\n");
jjohnle 3:f282664610ba 402 printf(" Motor Temperature (Celsius) = %f",motorTemp);
jjohnle 3:f282664610ba 403 printf("\r\n");
jjohnle 3:f282664610ba 404 printf(" DC Bus (Ah) = %f",DCBusAmpHours);
jjohnle 3:f282664610ba 405 printf("\r\n");
jjohnle 3:f282664610ba 406 printf(" Odometer (Distance) (m) = %f",odometerValue);
jjohnle 3:f282664610ba 407 printf("\r\n");
jjohnle 3:f282664610ba 408
jjohnle 3:f282664610ba 409
jjohnle 3:f282664610ba 410 // blinking LED
jjohnle 3:f282664610ba 411 LED8 = !LED8;
jjohnle 3:f282664610ba 412 if (!can1.write(CANMessage(id, (char*)data, 8))) //send current and velocity over car CAN
jjohnle 3:f282664610ba 413 printf("Car CAN failed \n\r");
jjohnle 3:f282664610ba 414 }
jjohnle 3:f282664610ba 415 alive++;
jjohnle 3:f282664610ba 416 }
jjohnle 3:f282664610ba 417 }