![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
IGGE Power board
Dependencies: mbed ADS1015 USBDevice MCP4725
main.cpp@4:d29a7b77b1c6, 2014-04-07 (annotated)
- Committer:
- jamesdem1123
- Date:
- Mon Apr 07 04:32:52 2014 +0000
- Revision:
- 4:d29a7b77b1c6
- Parent:
- 3:a01ea06ca9ab
- Child:
- 5:6b90ed7f3eb1
working 1230
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jamesdem1123 | 1:dd6f8dc1e54e | 1 | |
jamesdem1123 | 0:54d5ccfcb14c | 2 | #include "mbed.h" |
jamesdem1123 | 1:dd6f8dc1e54e | 3 | #include "Adafruit_ADS1015.h" |
jamesdem1123 | 4:d29a7b77b1c6 | 4 | //#include "USBSerial.h" |
jamesdem1123 | 1:dd6f8dc1e54e | 5 | |
jamesdem1123 | 4:d29a7b77b1c6 | 6 | #include <iostream> |
jamesdem1123 | 4:d29a7b77b1c6 | 7 | #include <map> |
jamesdem1123 | 4:d29a7b77b1c6 | 8 | #include <string> |
jamesdem1123 | 4:d29a7b77b1c6 | 9 | #include <stdio.h> |
jamesdem1123 | 4:d29a7b77b1c6 | 10 | |
jamesdem1123 | 4:d29a7b77b1c6 | 11 | //#define SERIAL_BAUD_RATE 9600 |
jamesdem1123 | 1:dd6f8dc1e54e | 12 | |
jamesdem1123 | 1:dd6f8dc1e54e | 13 | //library function setup |
jamesdem1123 | 1:dd6f8dc1e54e | 14 | I2C i2c(p28, p27); |
jamesdem1123 | 1:dd6f8dc1e54e | 15 | Adafruit_ADS1015 ads(&i2c); |
jamesdem1123 | 4:d29a7b77b1c6 | 16 | //USBSerial pc; // USB CDC serial port |
jamesdem1123 | 1:dd6f8dc1e54e | 17 | CAN cantbus(p30, p29); |
jamesdem1123 | 2:321c8783ea30 | 18 | Ticker graph; |
jamesdem1123 | 1:dd6f8dc1e54e | 19 | |
jamesdem1123 | 4:d29a7b77b1c6 | 20 | |
jamesdem1123 | 4:d29a7b77b1c6 | 21 | //can stuff |
jamesdem1123 | 4:d29a7b77b1c6 | 22 | Ticker ticker; |
jamesdem1123 | 4:d29a7b77b1c6 | 23 | CAN can1(p9, p10); |
jamesdem1123 | 4:d29a7b77b1c6 | 24 | CAN can2(p30, p29); |
jamesdem1123 | 4:d29a7b77b1c6 | 25 | char counter[8] = {1,1,1,1,1,1,1,1}; |
jamesdem1123 | 4:d29a7b77b1c6 | 26 | int test=0x0; |
jamesdem1123 | 4:d29a7b77b1c6 | 27 | char counter2=0; |
jamesdem1123 | 4:d29a7b77b1c6 | 28 | |
jamesdem1123 | 4:d29a7b77b1c6 | 29 | |
jamesdem1123 | 4:d29a7b77b1c6 | 30 | |
jamesdem1123 | 1:dd6f8dc1e54e | 31 | //leds |
jamesdem1123 | 1:dd6f8dc1e54e | 32 | DigitalOut led0(LED1); |
jamesdem1123 | 1:dd6f8dc1e54e | 33 | DigitalOut led1(LED2); |
jamesdem1123 | 1:dd6f8dc1e54e | 34 | DigitalOut led2(LED3); |
jamesdem1123 | 1:dd6f8dc1e54e | 35 | DigitalOut led3(LED4); |
jamesdem1123 | 0:54d5ccfcb14c | 36 | |
jamesdem1123 | 1:dd6f8dc1e54e | 37 | //current readings |
jamesdem1123 | 4:d29a7b77b1c6 | 38 | AnalogIn iBat(p15); //75a PROTO |
jamesdem1123 | 1:dd6f8dc1e54e | 39 | AnalogIn iTwelve(p17); //31a |
jamesdem1123 | 4:d29a7b77b1c6 | 40 | AnalogIn iFive(p18);//31a PROTO |
jamesdem1123 | 1:dd6f8dc1e54e | 41 | AnalogIn iMot(p16);//75a |
jamesdem1123 | 1:dd6f8dc1e54e | 42 | AnalogIn iCpu(p20);//31a |
jamesdem1123 | 1:dd6f8dc1e54e | 43 | AnalogIn iPeri(p19);//31a |
jamesdem1123 | 1:dd6f8dc1e54e | 44 | |
jamesdem1123 | 1:dd6f8dc1e54e | 45 | //voltage scalers |
jamesdem1123 | 1:dd6f8dc1e54e | 46 | #define batScal 12.2 |
jamesdem1123 | 1:dd6f8dc1e54e | 47 | #define motScal 12.2 |
jamesdem1123 | 1:dd6f8dc1e54e | 48 | #define twelveScal 12.2 |
jamesdem1123 | 1:dd6f8dc1e54e | 49 | #define fiveScal 12.2 |
jamesdem1123 | 1:dd6f8dc1e54e | 50 | |
jamesdem1123 | 1:dd6f8dc1e54e | 51 | //Relays |
jamesdem1123 | 4:d29a7b77b1c6 | 52 | DigitalOut relSens(p21); // PROTO |
jamesdem1123 | 1:dd6f8dc1e54e | 53 | DigitalOut relUsr(p22); |
jamesdem1123 | 1:dd6f8dc1e54e | 54 | DigitalOut relNet(p23); |
jamesdem1123 | 3:a01ea06ca9ab | 55 | DigitalOut relCpuRst(p24); |
jamesdem1123 | 1:dd6f8dc1e54e | 56 | DigitalOut relCpuPower(p25); |
jamesdem1123 | 1:dd6f8dc1e54e | 57 | |
jamesdem1123 | 0:54d5ccfcb14c | 58 | |
jamesdem1123 | 1:dd6f8dc1e54e | 59 | //30 minute graph arrays |
jamesdem1123 | 4:d29a7b77b1c6 | 60 | uint16_t powerArr [10]; //{iBat,vBat,iTwelve, vTwelve, iFive, vFive, iMot, vMot, iCpu, iPeri} |
jamesdem1123 | 4:d29a7b77b1c6 | 61 | |
jamesdem1123 | 4:d29a7b77b1c6 | 62 | |
jamesdem1123 | 4:d29a7b77b1c6 | 63 | float ibat =0; |
jamesdem1123 | 4:d29a7b77b1c6 | 64 | float ifive = 0; |
jamesdem1123 | 1:dd6f8dc1e54e | 65 | |
jamesdem1123 | 1:dd6f8dc1e54e | 66 | |
jamesdem1123 | 1:dd6f8dc1e54e | 67 | //volt |
jamesdem1123 | 1:dd6f8dc1e54e | 68 | uint16_t vBat, vTwelve,vFive,vMot ; |
jamesdem1123 | 1:dd6f8dc1e54e | 69 | |
jamesdem1123 | 2:321c8783ea30 | 70 | |
jamesdem1123 | 4:d29a7b77b1c6 | 71 | |
jamesdem1123 | 4:d29a7b77b1c6 | 72 | void sending_values(long int int1,long int int2,long int int3,long int int4, int ID ) |
jamesdem1123 | 4:d29a7b77b1c6 | 73 | { |
jamesdem1123 | 4:d29a7b77b1c6 | 74 | //val(); |
jamesdem1123 | 4:d29a7b77b1c6 | 75 | counter[0]=int1 >> 8; |
jamesdem1123 | 4:d29a7b77b1c6 | 76 | counter[1]=int1 & 0xFF; |
jamesdem1123 | 4:d29a7b77b1c6 | 77 | counter[2]=int2 >> 8; |
jamesdem1123 | 4:d29a7b77b1c6 | 78 | counter[3]=int2 & 0xFF; |
jamesdem1123 | 4:d29a7b77b1c6 | 79 | counter[4]=int3 >> 8; |
jamesdem1123 | 4:d29a7b77b1c6 | 80 | counter[5]=int3 & 0xFF; |
jamesdem1123 | 4:d29a7b77b1c6 | 81 | counter[6]=int4 >> 8; |
jamesdem1123 | 4:d29a7b77b1c6 | 82 | counter[7]=int4 & 0xFF; |
jamesdem1123 | 4:d29a7b77b1c6 | 83 | |
jamesdem1123 | 4:d29a7b77b1c6 | 84 | if(can1.write(CANMessage(ID, counter, 8))) { |
jamesdem1123 | 4:d29a7b77b1c6 | 85 | |
jamesdem1123 | 4:d29a7b77b1c6 | 86 | printf("Message sent: %x%x\r\n", counter[1],counter[0]); |
jamesdem1123 | 4:d29a7b77b1c6 | 87 | printf("Should be: %d, %d, %d, %d\r\n", int1,int2,int3,int4); |
jamesdem1123 | 4:d29a7b77b1c6 | 88 | } |
jamesdem1123 | 4:d29a7b77b1c6 | 89 | } |
jamesdem1123 | 4:d29a7b77b1c6 | 90 | |
jamesdem1123 | 4:d29a7b77b1c6 | 91 | |
jamesdem1123 | 4:d29a7b77b1c6 | 92 | |
jamesdem1123 | 4:d29a7b77b1c6 | 93 | |
jamesdem1123 | 4:d29a7b77b1c6 | 94 | |
jamesdem1123 | 4:d29a7b77b1c6 | 95 | |
jamesdem1123 | 4:d29a7b77b1c6 | 96 | void Can_main(bool send,long int val1,long int val2,long int val3,long int val4, int the_ID) { |
jamesdem1123 | 4:d29a7b77b1c6 | 97 | |
jamesdem1123 | 4:d29a7b77b1c6 | 98 | |
jamesdem1123 | 4:d29a7b77b1c6 | 99 | |
jamesdem1123 | 4:d29a7b77b1c6 | 100 | std::map <int,string> CanIDMap; |
jamesdem1123 | 4:d29a7b77b1c6 | 101 | |
jamesdem1123 | 4:d29a7b77b1c6 | 102 | //CanIDMap[0]="Motor Controller String"; |
jamesdem1123 | 4:d29a7b77b1c6 | 103 | CanIDMap[1]="Motor Controller Ints"; |
jamesdem1123 | 4:d29a7b77b1c6 | 104 | //CanIDMap[2]="Power Controller Voltage_Battery"; |
jamesdem1123 | 4:d29a7b77b1c6 | 105 | CanIDMap[3]="Power Controller Voltage_5V"; |
jamesdem1123 | 4:d29a7b77b1c6 | 106 | //CanIDMap[4]="Power Controller Voltage_12V"; |
jamesdem1123 | 4:d29a7b77b1c6 | 107 | CanIDMap[5]="Power Controller Voltage_Motor"; |
jamesdem1123 | 4:d29a7b77b1c6 | 108 | CanIDMap[6]="Power Controller Current_Battery"; |
jamesdem1123 | 4:d29a7b77b1c6 | 109 | CanIDMap[7]="Power Controller Current_5V"; |
jamesdem1123 | 4:d29a7b77b1c6 | 110 | CanIDMap[8]="Power Controller Current_12V"; |
jamesdem1123 | 4:d29a7b77b1c6 | 111 | CanIDMap[9]="Power Controller Current_Motor"; |
jamesdem1123 | 4:d29a7b77b1c6 | 112 | CanIDMap[10]="LCD Controller String"; |
jamesdem1123 | 4:d29a7b77b1c6 | 113 | CanIDMap[11]="LCD Controller Ints"; |
jamesdem1123 | 4:d29a7b77b1c6 | 114 | CanIDMap[12]="Zac is the Best"; |
jamesdem1123 | 4:d29a7b77b1c6 | 115 | |
jamesdem1123 | 4:d29a7b77b1c6 | 116 | |
jamesdem1123 | 4:d29a7b77b1c6 | 117 | |
jamesdem1123 | 4:d29a7b77b1c6 | 118 | //ticker.attach(&sending_values,1); |
jamesdem1123 | 4:d29a7b77b1c6 | 119 | CANMessage msg; |
jamesdem1123 | 4:d29a7b77b1c6 | 120 | int fixer1=0; |
jamesdem1123 | 4:d29a7b77b1c6 | 121 | int fixer2=0; |
jamesdem1123 | 4:d29a7b77b1c6 | 122 | int fixer3=0; |
jamesdem1123 | 4:d29a7b77b1c6 | 123 | int fixer4=0; |
jamesdem1123 | 4:d29a7b77b1c6 | 124 | while(1) { |
jamesdem1123 | 4:d29a7b77b1c6 | 125 | if (send==true){ |
jamesdem1123 | 4:d29a7b77b1c6 | 126 | sending_values(val1,val2,val3,val4,the_ID); |
jamesdem1123 | 4:d29a7b77b1c6 | 127 | } |
jamesdem1123 | 4:d29a7b77b1c6 | 128 | if(can2.read(msg)) { |
jamesdem1123 | 4:d29a7b77b1c6 | 129 | bool tester= CanIDMap.find(msg.id) != CanIDMap.end(); |
jamesdem1123 | 4:d29a7b77b1c6 | 130 | if (tester){ |
jamesdem1123 | 4:d29a7b77b1c6 | 131 | printf("Message received: %x %x \r\n", msg.data[0], msg.data[1]); |
jamesdem1123 | 4:d29a7b77b1c6 | 132 | |
jamesdem1123 | 4:d29a7b77b1c6 | 133 | //printf("ID of message: %s \r\n", CanIDMap[msg.id]); |
jamesdem1123 | 4:d29a7b77b1c6 | 134 | |
jamesdem1123 | 4:d29a7b77b1c6 | 135 | std::string str=CanIDMap[msg.id]; |
jamesdem1123 | 4:d29a7b77b1c6 | 136 | unsigned pos = str.find("_"); // position of "_" in str |
jamesdem1123 | 4:d29a7b77b1c6 | 137 | std::string str3 = str.substr (pos+1); // get from past"_" to the end |
jamesdem1123 | 4:d29a7b77b1c6 | 138 | // printf("Dealing with: %s \r\n",str3); |
jamesdem1123 | 4:d29a7b77b1c6 | 139 | fixer1=((msg.data[0]<<8) | (msg.data[1])); |
jamesdem1123 | 4:d29a7b77b1c6 | 140 | fixer2=((msg.data[2]<<8) | (msg.data[3])); |
jamesdem1123 | 4:d29a7b77b1c6 | 141 | fixer3=((msg.data[4]<<8) | (msg.data[5])); |
jamesdem1123 | 4:d29a7b77b1c6 | 142 | fixer4=((msg.data[6]<<8) | (msg.data[7])); |
jamesdem1123 | 4:d29a7b77b1c6 | 143 | led2 = !led2; |
jamesdem1123 | 4:d29a7b77b1c6 | 144 | |
jamesdem1123 | 4:d29a7b77b1c6 | 145 | printf("Resulting Answer back is: %d, %d, %d, %d\r\n\r\n\r\n",fixer1,fixer2,fixer3,fixer4 ); |
jamesdem1123 | 4:d29a7b77b1c6 | 146 | break; |
jamesdem1123 | 4:d29a7b77b1c6 | 147 | } |
jamesdem1123 | 4:d29a7b77b1c6 | 148 | |
jamesdem1123 | 4:d29a7b77b1c6 | 149 | else{ |
jamesdem1123 | 4:d29a7b77b1c6 | 150 | |
jamesdem1123 | 4:d29a7b77b1c6 | 151 | printf("Can Message, not related to this controller\r\n\r\n"); |
jamesdem1123 | 4:d29a7b77b1c6 | 152 | |
jamesdem1123 | 4:d29a7b77b1c6 | 153 | } |
jamesdem1123 | 4:d29a7b77b1c6 | 154 | |
jamesdem1123 | 4:d29a7b77b1c6 | 155 | wait(0.2); |
jamesdem1123 | 4:d29a7b77b1c6 | 156 | } |
jamesdem1123 | 4:d29a7b77b1c6 | 157 | |
jamesdem1123 | 4:d29a7b77b1c6 | 158 | |
jamesdem1123 | 4:d29a7b77b1c6 | 159 | } |
jamesdem1123 | 4:d29a7b77b1c6 | 160 | |
jamesdem1123 | 4:d29a7b77b1c6 | 161 | |
jamesdem1123 | 4:d29a7b77b1c6 | 162 | } |
jamesdem1123 | 4:d29a7b77b1c6 | 163 | |
jamesdem1123 | 4:d29a7b77b1c6 | 164 | |
jamesdem1123 | 4:d29a7b77b1c6 | 165 | |
jamesdem1123 | 4:d29a7b77b1c6 | 166 | |
jamesdem1123 | 2:321c8783ea30 | 167 | void updateArray (void) |
jamesdem1123 | 2:321c8783ea30 | 168 | { |
jamesdem1123 | 4:d29a7b77b1c6 | 169 | |
jamesdem1123 | 4:d29a7b77b1c6 | 170 | printf("reading Vbat: %d\r\n", vBat); // print reading |
jamesdem1123 | 4:d29a7b77b1c6 | 171 | printf("reading Battery Current: %2.8f\r\n", ibat); // print reading |
jamesdem1123 | 4:d29a7b77b1c6 | 172 | printf("reading Five Volt Current: %2.8f\r\n", ifive); // print reading |
jamesdem1123 | 4:d29a7b77b1c6 | 173 | Can_main(true,15,84,1254,3254,1); |
jamesdem1123 | 4:d29a7b77b1c6 | 174 | // printf("reading: %d\r\n", vMot); // print reading |
jamesdem1123 | 4:d29a7b77b1c6 | 175 | /* |
jamesdem1123 | 4:d29a7b77b1c6 | 176 | |
jamesdem1123 | 4:d29a7b77b1c6 | 177 | vBat = ads.readADC_SingleEnded(0); // read channel 0 PROTO |
jamesdem1123 | 2:321c8783ea30 | 178 | vTwelve = ads.readADC_SingleEnded(1); // read channel 0 |
jamesdem1123 | 2:321c8783ea30 | 179 | vFive = ads.readADC_SingleEnded(2); // read channel 0 |
jamesdem1123 | 2:321c8783ea30 | 180 | vMot = ads.readADC_SingleEnded(3); // read channel 0 |
jamesdem1123 | 2:321c8783ea30 | 181 | |
jamesdem1123 | 2:321c8783ea30 | 182 | |
jamesdem1123 | 4:d29a7b77b1c6 | 183 | |
jamesdem1123 | 4:d29a7b77b1c6 | 184 | */ |
jamesdem1123 | 2:321c8783ea30 | 185 | |
jamesdem1123 | 2:321c8783ea30 | 186 | } |
jamesdem1123 | 2:321c8783ea30 | 187 | |
jamesdem1123 | 1:dd6f8dc1e54e | 188 | void setup(void) |
jamesdem1123 | 1:dd6f8dc1e54e | 189 | { |
jamesdem1123 | 4:d29a7b77b1c6 | 190 | graph.attach(&updateArray, 10); //update array every 10 seconds |
jamesdem1123 | 4:d29a7b77b1c6 | 191 | // cantbus.frequency(500000); |
jamesdem1123 | 4:d29a7b77b1c6 | 192 | // CANMessage can_MsgRx; |
jamesdem1123 | 3:a01ea06ca9ab | 193 | |
jamesdem1123 | 1:dd6f8dc1e54e | 194 | //initialize relays |
jamesdem1123 | 1:dd6f8dc1e54e | 195 | relSens= 0; |
jamesdem1123 | 1:dd6f8dc1e54e | 196 | relNet =0; |
jamesdem1123 | 1:dd6f8dc1e54e | 197 | relCpuPower=0; |
jamesdem1123 | 1:dd6f8dc1e54e | 198 | relCpuRst =0; |
jamesdem1123 | 1:dd6f8dc1e54e | 199 | relUsr=0; |
jamesdem1123 | 1:dd6f8dc1e54e | 200 | |
jamesdem1123 | 1:dd6f8dc1e54e | 201 | } |
jamesdem1123 | 1:dd6f8dc1e54e | 202 | |
jamesdem1123 | 1:dd6f8dc1e54e | 203 | |
jamesdem1123 | 2:321c8783ea30 | 204 | |
jamesdem1123 | 1:dd6f8dc1e54e | 205 | int main() |
jamesdem1123 | 1:dd6f8dc1e54e | 206 | { |
jamesdem1123 | 1:dd6f8dc1e54e | 207 | setup(); |
jamesdem1123 | 3:a01ea06ca9ab | 208 | |
jamesdem1123 | 4:d29a7b77b1c6 | 209 | graph.attach(updateArray, 2); //update array every 10 seconds |
jamesdem1123 | 4:d29a7b77b1c6 | 210 | //cantbus.frequency(500000); |
jamesdem1123 | 4:d29a7b77b1c6 | 211 | // CANMessage can_MsgRx; |
jamesdem1123 | 3:a01ea06ca9ab | 212 | |
jamesdem1123 | 3:a01ea06ca9ab | 213 | //initialize relays |
jamesdem1123 | 3:a01ea06ca9ab | 214 | relSens= 0; |
jamesdem1123 | 3:a01ea06ca9ab | 215 | relNet =0; |
jamesdem1123 | 3:a01ea06ca9ab | 216 | relCpuPower=0; |
jamesdem1123 | 3:a01ea06ca9ab | 217 | relCpuRst =0; |
jamesdem1123 | 3:a01ea06ca9ab | 218 | relUsr=0; |
jamesdem1123 | 1:dd6f8dc1e54e | 219 | while (1) { |
jamesdem1123 | 4:d29a7b77b1c6 | 220 | vBat = ads.readADC_SingleEnded(0); // read channel 0 |
jamesdem1123 | 4:d29a7b77b1c6 | 221 | ibat = (iBat.read())*3.3; |
jamesdem1123 | 4:d29a7b77b1c6 | 222 | ifive = (iFive.read())*3.3; |
jamesdem1123 | 2:321c8783ea30 | 223 | |
jamesdem1123 | 1:dd6f8dc1e54e | 224 | |
jamesdem1123 | 1:dd6f8dc1e54e | 225 | |
jamesdem1123 | 4:d29a7b77b1c6 | 226 | led0=!led0; |
jamesdem1123 | 4:d29a7b77b1c6 | 227 | |
jamesdem1123 | 0:54d5ccfcb14c | 228 | } |
jamesdem1123 | 0:54d5ccfcb14c | 229 | } |
jamesdem1123 | 1:dd6f8dc1e54e | 230 | |
jamesdem1123 | 1:dd6f8dc1e54e | 231 | |
jamesdem1123 | 1:dd6f8dc1e54e | 232 | |
jamesdem1123 | 1:dd6f8dc1e54e | 233 | |
jamesdem1123 | 1:dd6f8dc1e54e | 234 | |
jamesdem1123 | 1:dd6f8dc1e54e | 235 | |
jamesdem1123 | 1:dd6f8dc1e54e | 236 | |
jamesdem1123 | 1:dd6f8dc1e54e | 237 | |
jamesdem1123 | 1:dd6f8dc1e54e | 238 | |
jamesdem1123 | 1:dd6f8dc1e54e | 239 | |
jamesdem1123 | 1:dd6f8dc1e54e | 240 | |
jamesdem1123 | 1:dd6f8dc1e54e | 241 | |
jamesdem1123 | 1:dd6f8dc1e54e | 242 | |
jamesdem1123 | 4:d29a7b77b1c6 | 243 | |
jamesdem1123 | 4:d29a7b77b1c6 | 244 | |
jamesdem1123 | 4:d29a7b77b1c6 | 245 | |
jamesdem1123 | 4:d29a7b77b1c6 | 246 | |
jamesdem1123 | 4:d29a7b77b1c6 | 247 | |
jamesdem1123 | 4:d29a7b77b1c6 | 248 | |
jamesdem1123 | 4:d29a7b77b1c6 | 249 |