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

Dependencies:   mbed

Committer:
jjohnle
Date:
Sat Nov 09 18:35:01 2019 +0000
Revision:
2:2a4822c7c91a
Parent:
1:863bdd011cf8
Child:
3:f282664610ba
borked

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jjohnle 2:2a4822c7c91a 1 #include "mbed.h"
jjohnle 2:2a4822c7c91a 2 #include "shared_values.h"
jjohnle 1:863bdd011cf8 3
jjohnle 0:c61d18b01f8c 4 #define DC_BASE 0x220 // Driver controls base address
jjohnle 0:c61d18b01f8c 5 #define DC_DRIVE 0x01 // Offset for motor drive command
jjohnle 0:c61d18b01f8c 6 #define DC_POWER 0x02 // Offset for motor power command
jjohnle 0:c61d18b01f8c 7 #define DC_RESET 0x03 // Offset for reset command
jjohnle 0:c61d18b01f8c 8 #define DC_SWITCH 0x04 // Offset for phase current measurement
jjohnle 0:c61d18b01f8c 9
jjohnle 1:863bdd011cf8 10 #define MC_BASE 0x240 // Motor controls base address
jjohnle 1:863bdd011cf8 11 #define MC_BUS 0x02 // Bus measurement offset
jjohnle 1:863bdd011cf8 12 #define MC_VEL 0x03 // Velocity measurement offset
jjohnle 1:863bdd011cf8 13 #define MC_PHCUR 0x04 // Phase Current offset
jjohnle 1:863bdd011cf8 14 #define MC_VOVEC 0x05 // Voltage Vector offset
jjohnle 1:863bdd011cf8 15 #define MC_CUVEC 0x06 // current vector offset
jjohnle 1:863bdd011cf8 16 #define MC_BEMF 0x07 // back emf offset
jjohnle 1:863bdd011cf8 17 #define MC_TEMP 0x0B // heat sink and motor temp offset
jjohnle 1:863bdd011cf8 18 #define MC_AMPH 0x0E // odometer and bus amp ohours measuremeant
jjohnle 0:c61d18b01f8c 19 #define MAX_VELOCITY 100 // motor velocity in m/s
jjohnle 0:c61d18b01f8c 20 #define MAX_CURRENT 1.0 // desired motor current as percentage of max current
jjohnle 0:c61d18b01f8c 21
jjohnle 2:2a4822c7c91a 22 #define DC_BUS_CURRENT 0x600
jjohnle 2:2a4822c7c91a 23 #define DC_BUS_VOLTAGE 0x601
jjohnle 2:2a4822c7c91a 24 #define PHASE_B_CURRENT 0x602
jjohnle 2:2a4822c7c91a 25 #define PHASE_C_CURRENT 0x603
jjohnle 2:2a4822c7c91a 26 #define VEHICLE_VELOCITY 0x604
jjohnle 2:2a4822c7c91a 27 #define MOTOR_VELOCITY 0x605
jjohnle 2:2a4822c7c91a 28 #define VD 0x606
jjohnle 2:2a4822c7c91a 29 #define VQ 0x607
jjohnle 2:2a4822c7c91a 30 #define ID 0x608
jjohnle 2:2a4822c7c91a 31 #define IQ 0x609
jjohnle 2:2a4822c7c91a 32 #define BEMFD 0x60A
jjohnle 2:2a4822c7c91a 33 #define BEMFQ 0x60B
jjohnle 2:2a4822c7c91a 34 #define HEAT_SINK_TEMPERATURE 0x60C
jjohnle 2:2a4822c7c91a 35 #define MOTOR_TEMPERATURE 0x60D
jjohnle 2:2a4822c7c91a 36 #define DC_BUS_AMP_HOURS 0x60E
jjohnle 2:2a4822c7c91a 37 #define ODOMETER 0x60F
jjohnle 0:c61d18b01f8c 38
jjohnle 2:2a4822c7c91a 39 float current = MAX_CURRENT;
jjohnle 2:2a4822c7c91a 40 float velocity = MAX_VELOCITY;
jjohnle 2:2a4822c7c91a 41 float bus_current = MAX_CURRENT;
jjohnle 2:2a4822c7c91a 42 double pedal_position;
jjohnle 2:2a4822c7c91a 43 double avgval;
jjohnle 2:2a4822c7c91a 44 float data[2];
jjohnle 2:2a4822c7c91a 45 float data2[2];
jjohnle 2:2a4822c7c91a 46 float meas = 0;
jjohnle 2:2a4822c7c91a 47 int n;
jjohnle 2:2a4822c7c91a 48 int dummy;
jjohnle 2:2a4822c7c91a 49 int alive;
jjohnle 2:2a4822c7c91a 50
jjohnle 2:2a4822c7c91a 51 int id = DC_BASE + DC_DRIVE;
jjohnle 2:2a4822c7c91a 52 int id2 = DC_BASE + DC_POWER;
jjohnle 2:2a4822c7c91a 53 int id3 = MC_BASE + DC_POWER;
jjohnle 2:2a4822c7c91a 54
jjohnle 2:2a4822c7c91a 55 CAN can1(PD_0, PD_1 /*, 125000*/); // can1 is car CAN (Rx, Tx, speed)
jjohnle 2:2a4822c7c91a 56 CAN can2(PB_5, PB_6 /*, 50000*/); // can2 is motor controller CAN (Rx, Tx, speed)
jjohnle 2:2a4822c7c91a 57 AnalogIn poop(PB_0);
jjohnle 0:c61d18b01f8c 58 Serial pc(USBTX, USBRX);
jjohnle 0:c61d18b01f8c 59
jjohnle 2:2a4822c7c91a 60 // https://stackoverflow.com/questions/24420246/c-function-to-convert-float-to-byte-array
jjohnle 2:2a4822c7c91a 61 void float2Bytes(float val, uint8_t *bytes_array)
jjohnle 1:863bdd011cf8 62 {
jjohnle 2:2a4822c7c91a 63 uint8_t temp;
jjohnle 2:2a4822c7c91a 64 // Create union of shared memory space
jjohnle 2:2a4822c7c91a 65 union {
jjohnle 2:2a4822c7c91a 66 float float_variable;
jjohnle 2:2a4822c7c91a 67 uint8_t temp_array[4];
jjohnle 2:2a4822c7c91a 68 } u;
jjohnle 2:2a4822c7c91a 69 // Overite bytes of union with float variable
jjohnle 2:2a4822c7c91a 70 u.float_variable = val;
jjohnle 2:2a4822c7c91a 71 // Assign bytes to input array
jjohnle 2:2a4822c7c91a 72 memcpy(bytes_array, u.temp_array, 4);
jjohnle 2:2a4822c7c91a 73 temp = bytes_array[3];
jjohnle 2:2a4822c7c91a 74 bytes_array[3] = bytes_array[0];
jjohnle 2:2a4822c7c91a 75 bytes_array[0] = temp;
jjohnle 2:2a4822c7c91a 76 temp = bytes_array[2];
jjohnle 2:2a4822c7c91a 77 bytes_array[2] = bytes_array[1];
jjohnle 2:2a4822c7c91a 78 bytes_array[1] = temp;
jjohnle 2:2a4822c7c91a 79 }
jjohnle 1:863bdd011cf8 80
jjohnle 2:2a4822c7c91a 81 float bytes2Float(uint8_t *bytes_array)
jjohnle 2:2a4822c7c91a 82 {
jjohnle 2:2a4822c7c91a 83 union {
jjohnle 2:2a4822c7c91a 84 float f;
jjohnle 2:2a4822c7c91a 85 uint8_t b[4];
jjohnle 2:2a4822c7c91a 86 } u;
jjohnle 2:2a4822c7c91a 87 u.b[3] = bytes_array[0];
jjohnle 2:2a4822c7c91a 88 u.b[2] = bytes_array[1];
jjohnle 2:2a4822c7c91a 89 u.b[1] = bytes_array[2];
jjohnle 2:2a4822c7c91a 90 u.b[0] = bytes_array[3];
jjohnle 2:2a4822c7c91a 91 return u.f;
jjohnle 2:2a4822c7c91a 92 }
jjohnle 1:863bdd011cf8 93
jjohnle 2:2a4822c7c91a 94 union {
jjohnle 2:2a4822c7c91a 95 char rcvdata[4];
jjohnle 2:2a4822c7c91a 96 float rxdata;
jjohnle 2:2a4822c7c91a 97 } urxdata;
jjohnle 1:863bdd011cf8 98
jjohnle 2:2a4822c7c91a 99 CANMessage msg;
jjohnle 2:2a4822c7c91a 100 char rdata[8];
jjohnle 2:2a4822c7c91a 101 char rdata2[8];
jjohnle 0:c61d18b01f8c 102
jjohnle 2:2a4822c7c91a 103 void pedal()
jjohnle 2:2a4822c7c91a 104 {
jjohnle 0:c61d18b01f8c 105 while (1)
jjohnle 0:c61d18b01f8c 106 {
jjohnle 0:c61d18b01f8c 107 n = 0;
jjohnle 0:c61d18b01f8c 108 avgval = 0.0;
jjohnle 0:c61d18b01f8c 109 while (n < 100)
jjohnle 0:c61d18b01f8c 110 {
jjohnle 2:2a4822c7c91a 111 meas = poop.read();
jjohnle 0:c61d18b01f8c 112 avgval = avgval + meas;
jjohnle 0:c61d18b01f8c 113 n++;
jjohnle 0:c61d18b01f8c 114 }
jjohnle 0:c61d18b01f8c 115 pedal_position = avgval / 100.0;
jjohnle 0:c61d18b01f8c 116
jjohnle 0:c61d18b01f8c 117 current = MAX_CURRENT * pedal_position;
jjohnle 0:c61d18b01f8c 118 velocity = 9.0;
jjohnle 0:c61d18b01f8c 119
jjohnle 0:c61d18b01f8c 120 data[1] = current; // Flipped because of endianness
jjohnle 0:c61d18b01f8c 121 data[0] = velocity;
jjohnle 0:c61d18b01f8c 122
jjohnle 0:c61d18b01f8c 123 if (!can2.write(CANMessage(id, (char *)data, 8))) // send current and velocity to Tritum
jjohnle 0:c61d18b01f8c 124 printf("Drive failed \n\r");
jjohnle 0:c61d18b01f8c 125
jjohnle 0:c61d18b01f8c 126 data2[1] = bus_current;
jjohnle 0:c61d18b01f8c 127 data2[0] = 0.0;
jjohnle 0:c61d18b01f8c 128 if (!can2.write(CANMessage(id2, (char *)data2, 8)))
jjohnle 0:c61d18b01f8c 129 dummy = 0;
jjohnle 0:c61d18b01f8c 130
jjohnle 0:c61d18b01f8c 131 wait_ms(10); // Need message every 250ms to maintain operation
jjohnle 2:2a4822c7c91a 132 }
jjohnle 2:2a4822c7c91a 133 }
jjohnle 0:c61d18b01f8c 134
jjohnle 2:2a4822c7c91a 135 void receiveCAN()
jjohnle 2:2a4822c7c91a 136 {
jjohnle 2:2a4822c7c91a 137 can1.frequency(125000);
jjohnle 2:2a4822c7c91a 138 can2.frequency(50000);
jjohnle 2:2a4822c7c91a 139 while (1)
jjohnle 2:2a4822c7c91a 140 {
jjohnle 2:2a4822c7c91a 141 pc.printf("inside thread \r\n");
jjohnle 2:2a4822c7c91a 142 if (can2.read(msg) && msg.id == (MC_BASE + MC_VEL))
jjohnle 2:2a4822c7c91a 143 {
jjohnle 0:c61d18b01f8c 144 for (int i = 0; i < msg.len; i++)
jjohnle 0:c61d18b01f8c 145 {
jjohnle 0:c61d18b01f8c 146 rdata[i] = msg.data[i];
jjohnle 0:c61d18b01f8c 147 }
jjohnle 0:c61d18b01f8c 148 urxdata.rcvdata[3] = rdata[7];
jjohnle 0:c61d18b01f8c 149 urxdata.rcvdata[2] = rdata[6];
jjohnle 0:c61d18b01f8c 150 urxdata.rcvdata[1] = rdata[5];
jjohnle 0:c61d18b01f8c 151 urxdata.rcvdata[0] = rdata[4];
jjohnle 2:2a4822c7c91a 152 //DCbuscur = urxdata.rxdata;
jjohnle 0:c61d18b01f8c 153 vehicleVel = urxdata.rxdata;
jjohnle 0:c61d18b01f8c 154 urxdata.rcvdata[3] = rdata[3];
jjohnle 0:c61d18b01f8c 155 urxdata.rcvdata[2] = rdata[2];
jjohnle 0:c61d18b01f8c 156 urxdata.rcvdata[1] = rdata[1];
jjohnle 0:c61d18b01f8c 157 urxdata.rcvdata[0] = rdata[0];
jjohnle 2:2a4822c7c91a 158 //DCbusvolt = urxdata.rxdata;
jjohnle 0:c61d18b01f8c 159 motorVel = urxdata.rxdata;
jjohnle 2:2a4822c7c91a 160 }
jjohnle 2:2a4822c7c91a 161 else if (can2.read(msg) && msg.id == (MC_BASE + MC_BUS))
jjohnle 2:2a4822c7c91a 162 {
jjohnle 2:2a4822c7c91a 163 for (int i = 0; i < msg.len; i++)
jjohnle 2:2a4822c7c91a 164 {
jjohnle 2:2a4822c7c91a 165 rdata2[i] = msg.data[i];
jjohnle 2:2a4822c7c91a 166 }
jjohnle 2:2a4822c7c91a 167
jjohnle 2:2a4822c7c91a 168 urxdata.rcvdata[3] = rdata2[7];
jjohnle 2:2a4822c7c91a 169 urxdata.rcvdata[2] = rdata2[6];
jjohnle 2:2a4822c7c91a 170 urxdata.rcvdata[1] = rdata2[5];
jjohnle 2:2a4822c7c91a 171 urxdata.rcvdata[0] = rdata2[4];
jjohnle 2:2a4822c7c91a 172 //vehicleVel = urxdata.rxdata;
jjohnle 2:2a4822c7c91a 173 DCbuscur = urxdata.rxdata;
jjohnle 2:2a4822c7c91a 174 urxdata.rcvdata[3] = rdata2[3];
jjohnle 2:2a4822c7c91a 175 urxdata.rcvdata[2] = rdata2[2];
jjohnle 2:2a4822c7c91a 176 urxdata.rcvdata[1] = rdata2[1];
jjohnle 2:2a4822c7c91a 177 urxdata.rcvdata[0] = rdata2[0];
jjohnle 2:2a4822c7c91a 178 //motorVel = urxdata.rxdata;
jjohnle 2:2a4822c7c91a 179 DCbusvolt = urxdata.rxdata;
jjohnle 0:c61d18b01f8c 180 }
jjohnle 0:c61d18b01f8c 181
jjohnle 0:c61d18b01f8c 182 // reading phase currents
jjohnle 0:c61d18b01f8c 183 else if (can2.read(msg) && msg.id == (MC_BASE + MC_PHCUR))
jjohnle 0:c61d18b01f8c 184 {
jjohnle 0:c61d18b01f8c 185 for (int i = 0; i < msg.len; i++)
jjohnle 0:c61d18b01f8c 186 {
jjohnle 0:c61d18b01f8c 187 rdata[i] = msg.data[i];
jjohnle 0:c61d18b01f8c 188 }
jjohnle 0:c61d18b01f8c 189 urxdata.rcvdata[3] = rdata[7];
jjohnle 0:c61d18b01f8c 190 urxdata.rcvdata[2] = rdata[6];
jjohnle 0:c61d18b01f8c 191 urxdata.rcvdata[1] = rdata[5];
jjohnle 0:c61d18b01f8c 192 urxdata.rcvdata[0] = rdata[4];
jjohnle 0:c61d18b01f8c 193 phaseCcurrent = urxdata.rxdata;
jjohnle 0:c61d18b01f8c 194 urxdata.rcvdata[3] = rdata[3];
jjohnle 0:c61d18b01f8c 195 urxdata.rcvdata[2] = rdata[2];
jjohnle 0:c61d18b01f8c 196 urxdata.rcvdata[1] = rdata[1];
jjohnle 0:c61d18b01f8c 197 urxdata.rcvdata[0] = rdata[0];
jjohnle 0:c61d18b01f8c 198 phaseBcurrent = urxdata.rxdata;
jjohnle 0:c61d18b01f8c 199 }
jjohnle 0:c61d18b01f8c 200
jjohnle 0:c61d18b01f8c 201 // reading motor voltage vector
jjohnle 0:c61d18b01f8c 202 else if (can2.read(msg) && msg.id == (MC_BASE + MC_VOVEC))
jjohnle 0:c61d18b01f8c 203 {
jjohnle 0:c61d18b01f8c 204 for (int i = 0; i < msg.len; i++)
jjohnle 0:c61d18b01f8c 205 {
jjohnle 0:c61d18b01f8c 206 rdata[i] = msg.data[i];
jjohnle 0:c61d18b01f8c 207 }
jjohnle 0:c61d18b01f8c 208 urxdata.rcvdata[3] = rdata[7];
jjohnle 0:c61d18b01f8c 209 urxdata.rcvdata[2] = rdata[6];
jjohnle 0:c61d18b01f8c 210 urxdata.rcvdata[1] = rdata[5];
jjohnle 0:c61d18b01f8c 211 urxdata.rcvdata[0] = rdata[4];
jjohnle 0:c61d18b01f8c 212 vd = urxdata.rxdata;
jjohnle 0:c61d18b01f8c 213 urxdata.rcvdata[3] = rdata[3];
jjohnle 0:c61d18b01f8c 214 urxdata.rcvdata[2] = rdata[2];
jjohnle 0:c61d18b01f8c 215 urxdata.rcvdata[1] = rdata[1];
jjohnle 0:c61d18b01f8c 216 urxdata.rcvdata[0] = rdata[0];
jjohnle 0:c61d18b01f8c 217 vq = urxdata.rxdata;
jjohnle 0:c61d18b01f8c 218 }
jjohnle 0:c61d18b01f8c 219
jjohnle 0:c61d18b01f8c 220 // reading current vector
jjohnle 0:c61d18b01f8c 221 else if (can2.read(msg) && msg.id == (MC_BASE + MC_CUVEC))
jjohnle 0:c61d18b01f8c 222 {
jjohnle 0:c61d18b01f8c 223 for (int i = 0; i < msg.len; i++)
jjohnle 0:c61d18b01f8c 224 {
jjohnle 0:c61d18b01f8c 225 rdata[i] = msg.data[i];
jjohnle 0:c61d18b01f8c 226 }
jjohnle 0:c61d18b01f8c 227 urxdata.rcvdata[3] = rdata[7];
jjohnle 0:c61d18b01f8c 228 urxdata.rcvdata[2] = rdata[6];
jjohnle 0:c61d18b01f8c 229 urxdata.rcvdata[1] = rdata[5];
jjohnle 0:c61d18b01f8c 230 urxdata.rcvdata[0] = rdata[4];
jjohnle 0:c61d18b01f8c 231 Id = urxdata.rxdata;
jjohnle 0:c61d18b01f8c 232 urxdata.rcvdata[3] = rdata[3];
jjohnle 0:c61d18b01f8c 233 urxdata.rcvdata[2] = rdata[2];
jjohnle 0:c61d18b01f8c 234 urxdata.rcvdata[1] = rdata[1];
jjohnle 0:c61d18b01f8c 235 urxdata.rcvdata[0] = rdata[0];
jjohnle 0:c61d18b01f8c 236 Iq = urxdata.rxdata;
jjohnle 0:c61d18b01f8c 237 }
jjohnle 0:c61d18b01f8c 238
jjohnle 0:c61d18b01f8c 239 // reading back emf
jjohnle 0:c61d18b01f8c 240 else if (can2.read(msg) && msg.id == (MC_BASE + MC_BEMF))
jjohnle 0:c61d18b01f8c 241 {
jjohnle 0:c61d18b01f8c 242 for (int i = 0; i < msg.len; i++)
jjohnle 0:c61d18b01f8c 243 {
jjohnle 0:c61d18b01f8c 244 rdata[i] = msg.data[i];
jjohnle 0:c61d18b01f8c 245 }
jjohnle 0:c61d18b01f8c 246 urxdata.rcvdata[3] = rdata[7];
jjohnle 0:c61d18b01f8c 247 urxdata.rcvdata[2] = rdata[6];
jjohnle 0:c61d18b01f8c 248 urxdata.rcvdata[1] = rdata[5];
jjohnle 0:c61d18b01f8c 249 urxdata.rcvdata[0] = rdata[4];
jjohnle 0:c61d18b01f8c 250 BEMFd = urxdata.rxdata;
jjohnle 0:c61d18b01f8c 251 urxdata.rcvdata[3] = rdata[3];
jjohnle 0:c61d18b01f8c 252 urxdata.rcvdata[2] = rdata[2];
jjohnle 0:c61d18b01f8c 253 urxdata.rcvdata[1] = rdata[1];
jjohnle 0:c61d18b01f8c 254 urxdata.rcvdata[0] = rdata[0];
jjohnle 0:c61d18b01f8c 255 BEMFq = urxdata.rxdata;
jjohnle 0:c61d18b01f8c 256 }
jjohnle 0:c61d18b01f8c 257
jjohnle 0:c61d18b01f8c 258 // reading heatsink and motor temp
jjohnle 0:c61d18b01f8c 259 else if (can2.read(msg) && msg.id == (MC_BASE + MC_TEMP))
jjohnle 0:c61d18b01f8c 260 {
jjohnle 0:c61d18b01f8c 261 for (int i = 0; i < msg.len; i++)
jjohnle 0:c61d18b01f8c 262 {
jjohnle 0:c61d18b01f8c 263 rdata[i] = msg.data[i];
jjohnle 0:c61d18b01f8c 264 }
jjohnle 0:c61d18b01f8c 265 urxdata.rcvdata[3] = rdata[7];
jjohnle 0:c61d18b01f8c 266 urxdata.rcvdata[2] = rdata[6];
jjohnle 0:c61d18b01f8c 267 urxdata.rcvdata[1] = rdata[5];
jjohnle 0:c61d18b01f8c 268 urxdata.rcvdata[0] = rdata[4];
jjohnle 0:c61d18b01f8c 269 heatSinkTemp = urxdata.rxdata;
jjohnle 0:c61d18b01f8c 270 urxdata.rcvdata[3] = rdata[3];
jjohnle 0:c61d18b01f8c 271 urxdata.rcvdata[2] = rdata[2];
jjohnle 0:c61d18b01f8c 272 urxdata.rcvdata[1] = rdata[1];
jjohnle 0:c61d18b01f8c 273 urxdata.rcvdata[0] = rdata[0];
jjohnle 0:c61d18b01f8c 274 motorTemp = urxdata.rxdata;
jjohnle 0:c61d18b01f8c 275 }
jjohnle 0:c61d18b01f8c 276
jjohnle 0:c61d18b01f8c 277 // reading odometer and bus amp ohours measuremeant
jjohnle 0:c61d18b01f8c 278 else if (can2.read(msg) && msg.id == (MC_BASE + MC_AMPH))
jjohnle 0:c61d18b01f8c 279 {
jjohnle 0:c61d18b01f8c 280 for (int i = 0; i < msg.len; i++)
jjohnle 0:c61d18b01f8c 281 {
jjohnle 0:c61d18b01f8c 282 rdata[i] = msg.data[i];
jjohnle 0:c61d18b01f8c 283 }
jjohnle 0:c61d18b01f8c 284 urxdata.rcvdata[3] = rdata[7];
jjohnle 0:c61d18b01f8c 285 urxdata.rcvdata[2] = rdata[6];
jjohnle 0:c61d18b01f8c 286 urxdata.rcvdata[1] = rdata[5];
jjohnle 0:c61d18b01f8c 287 urxdata.rcvdata[0] = rdata[4];
jjohnle 0:c61d18b01f8c 288 DCBusAmpHours = urxdata.rxdata;
jjohnle 0:c61d18b01f8c 289 urxdata.rcvdata[3] = rdata[3];
jjohnle 0:c61d18b01f8c 290 urxdata.rcvdata[2] = rdata[2];
jjohnle 0:c61d18b01f8c 291 urxdata.rcvdata[1] = rdata[1];
jjohnle 0:c61d18b01f8c 292 urxdata.rcvdata[0] = rdata[0];
jjohnle 0:c61d18b01f8c 293 odometerValue = urxdata.rxdata;
jjohnle 0:c61d18b01f8c 294 }
jjohnle 0:c61d18b01f8c 295
jjohnle 0:c61d18b01f8c 296 if (alive % 100 == 0)
jjohnle 0:c61d18b01f8c 297 {
jjohnle 2:2a4822c7c91a 298
jjohnle 2:2a4822c7c91a 299 printf("Motor board is running");
jjohnle 2:2a4822c7c91a 300 printf("\r\n");
jjohnle 2:2a4822c7c91a 301 //printf(" Requested Motor Current: %f\n\r", current);
jjohnle 2:2a4822c7c91a 302 //printf(" Requested Motor Velocity: %f\n\r", velocity);
jjohnle 0:c61d18b01f8c 303 printf(" DC Bus Current (A) = %f", DCbuscur);
jjohnle 0:c61d18b01f8c 304 printf("\r\n");
jjohnle 0:c61d18b01f8c 305 printf(" DC Bus Voltage (V) = %f", DCbusvolt);
jjohnle 0:c61d18b01f8c 306 printf("\r\n");
jjohnle 0:c61d18b01f8c 307
jjohnle 0:c61d18b01f8c 308 // Printing other values
jjohnle 0:c61d18b01f8c 309 printf(" Vehicle Velocity (RPM) = %f", vehicleVel);
jjohnle 0:c61d18b01f8c 310 printf("\r\n");
jjohnle 0:c61d18b01f8c 311 printf(" Motor Velocity (V) = %f", motorVel);
jjohnle 0:c61d18b01f8c 312 printf("\r\n");
jjohnle 0:c61d18b01f8c 313 printf(" Phase B Current (A-rms) = %f", phaseBcurrent);
jjohnle 0:c61d18b01f8c 314 printf("\r\n");
jjohnle 0:c61d18b01f8c 315 printf(" Phase C Current (A-rms) = %f", phaseCcurrent);
jjohnle 0:c61d18b01f8c 316 printf("\r\n");
jjohnle 0:c61d18b01f8c 317 printf(" Vd (V) = %f", vd);
jjohnle 0:c61d18b01f8c 318 printf("\r\n");
jjohnle 0:c61d18b01f8c 319 printf(" Vq (V) = %f", vq);
jjohnle 0:c61d18b01f8c 320 printf("\r\n");
jjohnle 0:c61d18b01f8c 321
jjohnle 0:c61d18b01f8c 322 printf(" Id (A) = %f", Id);
jjohnle 0:c61d18b01f8c 323 printf("\r\n");
jjohnle 0:c61d18b01f8c 324 printf(" Iq (A) = %f", Iq);
jjohnle 0:c61d18b01f8c 325 printf("\r\n");
jjohnle 0:c61d18b01f8c 326 printf(" BEMFd (V) = %f", BEMFd);
jjohnle 0:c61d18b01f8c 327 printf("\r\n");
jjohnle 0:c61d18b01f8c 328 printf(" BEMFq (V) = %f", BEMFq);
jjohnle 0:c61d18b01f8c 329 printf("\r\n");
jjohnle 0:c61d18b01f8c 330 printf(" Heat Sink Temperature (Celsius) = %f", heatSinkTemp);
jjohnle 0:c61d18b01f8c 331 printf("\r\n");
jjohnle 0:c61d18b01f8c 332 printf(" Motor Temperature (Celsius) = %f", motorTemp);
jjohnle 0:c61d18b01f8c 333 printf("\r\n");
jjohnle 0:c61d18b01f8c 334 printf(" DC Bus (Ah) = %f", DCBusAmpHours);
jjohnle 0:c61d18b01f8c 335 printf("\r\n");
jjohnle 0:c61d18b01f8c 336 printf(" Odometer (Distance) (m) = %f", odometerValue);
jjohnle 0:c61d18b01f8c 337 printf("\r\n");
jjohnle 2:2a4822c7c91a 338 }
jjohnle 2:2a4822c7c91a 339 }
jjohnle 2:2a4822c7c91a 340 }
jjohnle 0:c61d18b01f8c 341
jjohnle 2:2a4822c7c91a 342 int counter = 0;
jjohnle 2:2a4822c7c91a 343 int CAN_FLAG = 0;
jjohnle 2:2a4822c7c91a 344
jjohnle 2:2a4822c7c91a 345 void sendCAN()
jjohnle 2:2a4822c7c91a 346 {
jjohnle 2:2a4822c7c91a 347 while (1)
jjohnle 2:2a4822c7c91a 348 {
jjohnle 2:2a4822c7c91a 349 uint8_t bytes1[4];
jjohnle 2:2a4822c7c91a 350 float2Bytes(DCbuscur, &bytes1[0]);
jjohnle 2:2a4822c7c91a 351 if (can1.write(CANMessage(DC_BUS_CURRENT, (char *)(bytes1), 4)))
jjohnle 2:2a4822c7c91a 352 {
jjohnle 2:2a4822c7c91a 353 //pc.printf("Sent DC Bus Current");
jjohnle 2:2a4822c7c91a 354 }
jjohnle 2:2a4822c7c91a 355 else
jjohnle 2:2a4822c7c91a 356 {
jjohnle 2:2a4822c7c91a 357 // pc.printf("Cannot write to CAN\n");
jjohnle 2:2a4822c7c91a 358 }
jjohnle 2:2a4822c7c91a 359
jjohnle 2:2a4822c7c91a 360 uint8_t bytes2[4];
jjohnle 2:2a4822c7c91a 361 float2Bytes(DCbusvolt, &bytes2[0]);
jjohnle 2:2a4822c7c91a 362 if (can1.write(CANMessage(DC_BUS_VOLTAGE, (char *)(bytes2), 4)))
jjohnle 2:2a4822c7c91a 363 {
jjohnle 2:2a4822c7c91a 364 //pc.printf("Sent DC Bus Voltage");
jjohnle 2:2a4822c7c91a 365 }
jjohnle 2:2a4822c7c91a 366 else
jjohnle 2:2a4822c7c91a 367 {
jjohnle 2:2a4822c7c91a 368 //pc.printf("Cannot write to CAN\n");
jjohnle 2:2a4822c7c91a 369 }
jjohnle 2:2a4822c7c91a 370
jjohnle 2:2a4822c7c91a 371
jjohnle 2:2a4822c7c91a 372 uint8_t bytes3[4];
jjohnle 2:2a4822c7c91a 373 float2Bytes(vehicleVel, &bytes3[0]);
jjohnle 2:2a4822c7c91a 374 if (can1.write(CANMessage(VEHICLE_VELOCITY, (char *)(bytes3), 4)))
jjohnle 2:2a4822c7c91a 375 {
jjohnle 2:2a4822c7c91a 376 //pc.printf("Sent Vehicle Velocity (RPM)");
jjohnle 2:2a4822c7c91a 377 }
jjohnle 2:2a4822c7c91a 378 else
jjohnle 2:2a4822c7c91a 379 {
jjohnle 2:2a4822c7c91a 380 //pc.printf("Cannot write to CAN\n");
jjohnle 2:2a4822c7c91a 381 }
jjohnle 2:2a4822c7c91a 382
jjohnle 2:2a4822c7c91a 383
jjohnle 2:2a4822c7c91a 384 uint8_t bytes4[4];
jjohnle 2:2a4822c7c91a 385 float2Bytes(motorVel, &bytes4[0]);
jjohnle 2:2a4822c7c91a 386 if (can1.write(CANMessage(MOTOR_VELOCITY, (char *)(bytes4), 4)))
jjohnle 2:2a4822c7c91a 387 {
jjohnle 2:2a4822c7c91a 388 //pc.printf("Sent Motor Velocity (V)");
jjohnle 2:2a4822c7c91a 389 }
jjohnle 2:2a4822c7c91a 390 else
jjohnle 2:2a4822c7c91a 391 {
jjohnle 2:2a4822c7c91a 392 // pc.printf("Cannot write to CAN\n");
jjohnle 2:2a4822c7c91a 393 }
jjohnle 2:2a4822c7c91a 394
jjohnle 2:2a4822c7c91a 395
jjohnle 2:2a4822c7c91a 396 uint8_t bytes5[4];
jjohnle 2:2a4822c7c91a 397 float2Bytes(phaseBcurrent, &bytes5[0]);
jjohnle 2:2a4822c7c91a 398 if (can1.write(CANMessage(PHASE_B_CURRENT, (char *)(bytes5), 4)))
jjohnle 2:2a4822c7c91a 399 {
jjohnle 2:2a4822c7c91a 400 // pc.printf("Sent Phase B Current");
jjohnle 2:2a4822c7c91a 401 }
jjohnle 2:2a4822c7c91a 402 else
jjohnle 2:2a4822c7c91a 403 {
jjohnle 2:2a4822c7c91a 404 // pc.printf("Cannot write to CAN\n");
jjohnle 2:2a4822c7c91a 405 }
jjohnle 2:2a4822c7c91a 406
jjohnle 2:2a4822c7c91a 407 uint8_t bytes6[4];
jjohnle 2:2a4822c7c91a 408 float2Bytes(phaseCcurrent, &bytes6[0]);
jjohnle 2:2a4822c7c91a 409 if (can1.write(CANMessage(PHASE_C_CURRENT, (char *)(bytes6), 4)))
jjohnle 2:2a4822c7c91a 410 {
jjohnle 2:2a4822c7c91a 411 // pc.printf("Sent Phase C Current");
jjohnle 2:2a4822c7c91a 412 }
jjohnle 2:2a4822c7c91a 413 else
jjohnle 2:2a4822c7c91a 414 {
jjohnle 2:2a4822c7c91a 415 // pc.printf("Cannot write to CAN\n");
jjohnle 2:2a4822c7c91a 416 }
jjohnle 2:2a4822c7c91a 417
jjohnle 2:2a4822c7c91a 418 uint8_t bytes7[4];
jjohnle 2:2a4822c7c91a 419 float2Bytes(vd, &bytes7[0]);
jjohnle 2:2a4822c7c91a 420 if (can1.write(CANMessage(VD, (char *)(bytes7), 4)))
jjohnle 2:2a4822c7c91a 421 {
jjohnle 2:2a4822c7c91a 422 //pc.printf("Sent Vd (V)");
jjohnle 2:2a4822c7c91a 423 }
jjohnle 2:2a4822c7c91a 424 else
jjohnle 2:2a4822c7c91a 425 {
jjohnle 2:2a4822c7c91a 426 //pc.printf("Cannot write to CAN\n");
jjohnle 2:2a4822c7c91a 427 }
jjohnle 2:2a4822c7c91a 428
jjohnle 2:2a4822c7c91a 429 uint8_t bytes8[4];
jjohnle 2:2a4822c7c91a 430 float2Bytes(vq, &bytes8[0]);
jjohnle 2:2a4822c7c91a 431 if (can1.write(CANMessage(VQ, (char *)(bytes8), 4)))
jjohnle 2:2a4822c7c91a 432 {
jjohnle 2:2a4822c7c91a 433 // pc.printf("Sent Vq (V)");
jjohnle 2:2a4822c7c91a 434 }
jjohnle 2:2a4822c7c91a 435 else
jjohnle 2:2a4822c7c91a 436 {
jjohnle 2:2a4822c7c91a 437 // pc.printf("Cannot write to CAN\n");
jjohnle 2:2a4822c7c91a 438 }
jjohnle 2:2a4822c7c91a 439
jjohnle 2:2a4822c7c91a 440 uint8_t bytes9[4];
jjohnle 2:2a4822c7c91a 441 float2Bytes(Id, &bytes9[0]);
jjohnle 2:2a4822c7c91a 442 if (can1.write(CANMessage(ID, (char *)(bytes9), 4)))
jjohnle 2:2a4822c7c91a 443 {
jjohnle 2:2a4822c7c91a 444 //pc.printf("Sent Id (A)");
jjohnle 0:c61d18b01f8c 445 }
jjohnle 2:2a4822c7c91a 446 else
jjohnle 2:2a4822c7c91a 447 {
jjohnle 2:2a4822c7c91a 448 //pc.printf("Cannot write to CAN\n");
jjohnle 2:2a4822c7c91a 449 }
jjohnle 2:2a4822c7c91a 450
jjohnle 2:2a4822c7c91a 451 uint8_t bytes10[4];
jjohnle 2:2a4822c7c91a 452 float2Bytes(Iq, &bytes10[0]);
jjohnle 2:2a4822c7c91a 453 if (can1.write(CANMessage(IQ, (char *)(bytes10), 4)))
jjohnle 2:2a4822c7c91a 454 {
jjohnle 2:2a4822c7c91a 455 // pc.printf("Sent Iq (A)");
jjohnle 2:2a4822c7c91a 456 }
jjohnle 2:2a4822c7c91a 457 else
jjohnle 2:2a4822c7c91a 458 {
jjohnle 2:2a4822c7c91a 459 // pc.printf("Cannot write to CAN\n");
jjohnle 2:2a4822c7c91a 460 }
jjohnle 2:2a4822c7c91a 461
jjohnle 2:2a4822c7c91a 462 uint8_t bytes11[4];
jjohnle 2:2a4822c7c91a 463 float2Bytes(BEMFd, &bytes11[0]);
jjohnle 2:2a4822c7c91a 464 if (can1.write(CANMessage(BEMFD, (char *)(bytes11), 4)))
jjohnle 2:2a4822c7c91a 465 {
jjohnle 2:2a4822c7c91a 466 //pc.printf("Sent BEMFd");
jjohnle 2:2a4822c7c91a 467 }
jjohnle 2:2a4822c7c91a 468 else
jjohnle 2:2a4822c7c91a 469 {
jjohnle 2:2a4822c7c91a 470 //pc.printf("Cannot write to CAN\n");
jjohnle 2:2a4822c7c91a 471 }
jjohnle 2:2a4822c7c91a 472
jjohnle 2:2a4822c7c91a 473 uint8_t bytes12[4];
jjohnle 2:2a4822c7c91a 474 float2Bytes(BEMFq, &bytes12[0]);
jjohnle 2:2a4822c7c91a 475 if (can1.write(CANMessage(BEMFQ, (char *)(bytes12), 4)))
jjohnle 2:2a4822c7c91a 476 {
jjohnle 2:2a4822c7c91a 477 //pc.printf("Sent BEMFq");
jjohnle 2:2a4822c7c91a 478 }
jjohnle 2:2a4822c7c91a 479 else
jjohnle 2:2a4822c7c91a 480 {
jjohnle 2:2a4822c7c91a 481 //pc.printf("Cannot write to CAN\n");
jjohnle 2:2a4822c7c91a 482 }
jjohnle 2:2a4822c7c91a 483
jjohnle 2:2a4822c7c91a 484 uint8_t bytes13[4];
jjohnle 2:2a4822c7c91a 485 float2Bytes(heatSinkTemp, &bytes13[0]);
jjohnle 2:2a4822c7c91a 486 if (can1.write(CANMessage(HEAT_SINK_TEMPERATURE, (char *)(bytes13), 4)))
jjohnle 2:2a4822c7c91a 487 {
jjohnle 2:2a4822c7c91a 488 // pc.printf("Sent Heat Sink Temperature (Celsius)");
jjohnle 2:2a4822c7c91a 489 }
jjohnle 2:2a4822c7c91a 490 else
jjohnle 2:2a4822c7c91a 491 {
jjohnle 2:2a4822c7c91a 492 // pc.printf("Cannot write to CAN\n");
jjohnle 2:2a4822c7c91a 493 }
jjohnle 2:2a4822c7c91a 494
jjohnle 2:2a4822c7c91a 495 uint8_t bytes14[4];
jjohnle 2:2a4822c7c91a 496 float2Bytes(motorTemp, &bytes14[0]);
jjohnle 2:2a4822c7c91a 497 if (can1.write(CANMessage(MOTOR_TEMPERATURE, (char *)(bytes14), 4)))
jjohnle 2:2a4822c7c91a 498 {
jjohnle 2:2a4822c7c91a 499 // pc.printf("Sent Motor Temperature (Celsius)");
jjohnle 2:2a4822c7c91a 500 }
jjohnle 2:2a4822c7c91a 501 else
jjohnle 2:2a4822c7c91a 502 {
jjohnle 2:2a4822c7c91a 503 // pc.printf("Cannot write to CAN\n");
jjohnle 2:2a4822c7c91a 504 }
jjohnle 2:2a4822c7c91a 505
jjohnle 2:2a4822c7c91a 506 uint8_t bytes15[4];
jjohnle 2:2a4822c7c91a 507 float2Bytes(DCBusAmpHours, &bytes15[0]);
jjohnle 2:2a4822c7c91a 508 if (can1.write(CANMessage(DC_BUS_AMP_HOURS, (char *)(bytes15), 4)))
jjohnle 2:2a4822c7c91a 509 {
jjohnle 2:2a4822c7c91a 510 // pc.printf("Sent DC Bus (Ah)");
jjohnle 2:2a4822c7c91a 511 }
jjohnle 2:2a4822c7c91a 512 else
jjohnle 2:2a4822c7c91a 513 {
jjohnle 2:2a4822c7c91a 514 // pc.printf("Cannot write to CAN\n");
jjohnle 2:2a4822c7c91a 515 }
jjohnle 2:2a4822c7c91a 516
jjohnle 2:2a4822c7c91a 517 uint8_t bytes16[4];
jjohnle 2:2a4822c7c91a 518 float2Bytes(odometerValue, &bytes16[0]);
jjohnle 2:2a4822c7c91a 519 if (can1.write(CANMessage(ODOMETER, (char *)(bytes16), 4)))
jjohnle 2:2a4822c7c91a 520 {
jjohnle 2:2a4822c7c91a 521 // pc.printf("Sent Odometer (Distance) (m)");
jjohnle 2:2a4822c7c91a 522 }
jjohnle 2:2a4822c7c91a 523 else
jjohnle 2:2a4822c7c91a 524 {
jjohnle 2:2a4822c7c91a 525 // pc.printf("Cannot write to CAN\n");
jjohnle 2:2a4822c7c91a 526 }
jjohnle 0:c61d18b01f8c 527 }
jjohnle 2:2a4822c7c91a 528 }
jjohnle 2:2a4822c7c91a 529
jjohnle 2:2a4822c7c91a 530 int main()
jjohnle 2:2a4822c7c91a 531 {
jjohnle 2:2a4822c7c91a 532 Thread recc(receiveCAN);
jjohnle 2:2a4822c7c91a 533 wait(1);
jjohnle 2:2a4822c7c91a 534 Thread Indicators(pedal);
jjohnle 2:2a4822c7c91a 535 wait(2);
jjohnle 2:2a4822c7c91a 536 Thread send(sendCAN);
jjohnle 2:2a4822c7c91a 537
jjohnle 2:2a4822c7c91a 538 while (1)
jjohnle 2:2a4822c7c91a 539 {
jjohnle 2:2a4822c7c91a 540 //pc.printf("main\r\n");
jjohnle 2:2a4822c7c91a 541 /*
jjohnle 2:2a4822c7c91a 542 uint8_t bytes1[4];
jjohnle 2:2a4822c7c91a 543 float foo = 42.0;
jjohnle 2:2a4822c7c91a 544 //float2Bytes(foo, &bytes1[0]);
jjohnle 2:2a4822c7c91a 545 if (can1.write(CANMessage(MC_BASE + MC_VEL, (char*)rdata2, 8))) {
jjohnle 2:2a4822c7c91a 546 pc.printf("Sent Velocity = %f", foo);
jjohnle 2:2a4822c7c91a 547 }
jjohnle 2:2a4822c7c91a 548 else {
jjohnle 2:2a4822c7c91a 549 pc.printf("Cannot write to CAN\n");
jjohnle 2:2a4822c7c91a 550 }
jjohnle 2:2a4822c7c91a 551 wait(0.1);
jjohnle 2:2a4822c7c91a 552 */
jjohnle 2:2a4822c7c91a 553 }
jjohnle 2:2a4822c7c91a 554 }