![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
MBED Framework with communication to be tested
main.cpp@4:85bc76bb6fdb, 2017-07-23 (annotated)
- Committer:
- AndrewLouw
- Date:
- Sun Jul 23 20:29:38 2017 +0000
- Revision:
- 4:85bc76bb6fdb
- Parent:
- 3:5995028595d2
Republish MBED 145
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
AndrewLouw | 0:a32d085719df | 1 | #include "mbed.h" |
AndrewLouw | 0:a32d085719df | 2 | #include "PINNAMES.h" |
AndrewLouw | 0:a32d085719df | 3 | #include "SPICOMMANDS.h" |
AndrewLouw | 0:a32d085719df | 4 | #include "PROTOTYPES.h" |
AndrewLouw | 3:5995028595d2 | 5 | //#include "CAN_Data.h" |
AndrewLouw | 3:5995028595d2 | 6 | //#include "CAN_IDs.h" |
AndrewLouw | 0:a32d085719df | 7 | |
AndrewLouw | 0:a32d085719df | 8 | int main() { |
AndrewLouw | 0:a32d085719df | 9 | |
AndrewLouw | 0:a32d085719df | 10 | //main loop variables |
AndrewLouw | 3:5995028595d2 | 11 | bool Shutdown = false; |
AndrewLouw | 0:a32d085719df | 12 | OpModesym OpMode= Setup; //OpMode variable |
AndrewLouw | 0:a32d085719df | 13 | Errorsym ErrorFlag = False; //Error flag |
AndrewLouw | 0:a32d085719df | 14 | |
AndrewLouw | 0:a32d085719df | 15 | //angle limits |
AndrewLouw | 0:a32d085719df | 16 | const double Upperlimit_Can_on = 0; |
AndrewLouw | 0:a32d085719df | 17 | const double Upperlimit_Can_off = 0; |
AndrewLouw | 0:a32d085719df | 18 | const double Lowerlimit_Can_on = 100; |
AndrewLouw | 0:a32d085719df | 19 | const double Lowerlimit_Can_off = 100; |
AndrewLouw | 0:a32d085719df | 20 | |
AndrewLouw | 0:a32d085719df | 21 | //allocate memorry for data |
AndrewLouw | 0:a32d085719df | 22 | float LDR1; //LDR1's value |
AndrewLouw | 0:a32d085719df | 23 | float LDR2; //LDR2's value |
AndrewLouw | 1:e8c5a9654f5f | 24 | |
AndrewLouw | 3:5995028595d2 | 25 | //spi send and decoded recieve buffers |
AndrewLouw | 3:5995028595d2 | 26 | uint8_t write_buf [8]; |
AndrewLouw | 3:5995028595d2 | 27 | for (int i=0;i<8;i++){ |
AndrewLouw | 3:5995028595d2 | 28 | write_buf[i] = 0x00; |
AndrewLouw | 3:5995028595d2 | 29 | } |
AndrewLouw | 3:5995028595d2 | 30 | |
AndrewLouw | 3:5995028595d2 | 31 | uint8_t decoded_buf[7]; |
AndrewLouw | 3:5995028595d2 | 32 | |
AndrewLouw | 3:5995028595d2 | 33 | |
AndrewLouw | 3:5995028595d2 | 34 | |
AndrewLouw | 1:e8c5a9654f5f | 35 | float MPPT [5]; |
AndrewLouw | 1:e8c5a9654f5f | 36 | #define bat_volt MPPT[0] |
AndrewLouw | 1:e8c5a9654f5f | 37 | #define bat_current MPPT[1] |
AndrewLouw | 1:e8c5a9654f5f | 38 | #define bat_temp MPPT[2] |
AndrewLouw | 1:e8c5a9654f5f | 39 | #define bat_status MPPT[3] |
AndrewLouw | 1:e8c5a9654f5f | 40 | #define PV_volt MPPT[4] |
AndrewLouw | 1:e8c5a9654f5f | 41 | |
AndrewLouw | 2:64443528f6bb | 42 | uint8_t panel_locations [6]; //the array for actual/desired location of each panel |
AndrewLouw | 0:a32d085719df | 43 | |
AndrewLouw | 3:5995028595d2 | 44 | //setup SPI |
AndrewLouw | 3:5995028595d2 | 45 | printf("spi setup \r\n"); |
AndrewLouw | 3:5995028595d2 | 46 | const int spiBitrate=250000; |
AndrewLouw | 3:5995028595d2 | 47 | spi.format(8,3); |
AndrewLouw | 3:5995028595d2 | 48 | spi.frequency(spiBitrate); |
AndrewLouw | 3:5995028595d2 | 49 | SlaveSelect = 1; |
AndrewLouw | 3:5995028595d2 | 50 | |
AndrewLouw | 3:5995028595d2 | 51 | wait(2); |
AndrewLouw | 3:5995028595d2 | 52 | |
AndrewLouw | 0:a32d085719df | 53 | //main loop. |
AndrewLouw | 0:a32d085719df | 54 | while(1){ |
AndrewLouw | 0:a32d085719df | 55 | if(ErrorFlag==False){ |
AndrewLouw | 0:a32d085719df | 56 | switch (OpMode){ |
AndrewLouw | 3:5995028595d2 | 57 | case Setup: |
AndrewLouw | 3:5995028595d2 | 58 | printf("Setup mode \r\n"); |
AndrewLouw | 2:64443528f6bb | 59 | SlaveSelect = 0; |
AndrewLouw | 3:5995028595d2 | 60 | //start in correct mode |
AndrewLouw | 0:a32d085719df | 61 | |
AndrewLouw | 3:5995028595d2 | 62 | //test region |
AndrewLouw | 3:5995028595d2 | 63 | write_buf[0] = 0xAA; //message start, AA is a reserved message don't send at any other time |
AndrewLouw | 3:5995028595d2 | 64 | write_buf[1] = 0xF0; //chip id |
AndrewLouw | 3:5995028595d2 | 65 | write_buf[2] = 0xA0; //message verb |
AndrewLouw | 3:5995028595d2 | 66 | write_buf[3] = 0x01; //message noun |
AndrewLouw | 3:5995028595d2 | 67 | |
AndrewLouw | 3:5995028595d2 | 68 | for (int i=5;i<8;i++){ //spare nouns or verbs |
AndrewLouw | 3:5995028595d2 | 69 | write_buf[i] = 0xFF; |
AndrewLouw | 3:5995028595d2 | 70 | } |
AndrewLouw | 3:5995028595d2 | 71 | printf("begin send \r\n"); |
AndrewLouw | 3:5995028595d2 | 72 | spi_send(write_buf,decoded_buf); |
AndrewLouw | 3:5995028595d2 | 73 | printf("send exited \r\n"); |
AndrewLouw | 0:a32d085719df | 74 | StatusLED = 1; |
AndrewLouw | 3:5995028595d2 | 75 | wait_ms(1000); |
AndrewLouw | 3:5995028595d2 | 76 | StatusLED = 0; |
AndrewLouw | 3:5995028595d2 | 77 | wait_ms(1000); |
AndrewLouw | 3:5995028595d2 | 78 | printf("restarting \r\n"); |
AndrewLouw | 3:5995028595d2 | 79 | // end of test region |
AndrewLouw | 3:5995028595d2 | 80 | |
AndrewLouw | 3:5995028595d2 | 81 | |
AndrewLouw | 3:5995028595d2 | 82 | |
AndrewLouw | 3:5995028595d2 | 83 | //OpMode = readswitches(); |
AndrewLouw | 0:a32d085719df | 84 | break; |
AndrewLouw | 0:a32d085719df | 85 | |
AndrewLouw | 0:a32d085719df | 86 | case Canopy_on: |
AndrewLouw | 3:5995028595d2 | 87 | printf("Canopy_on mode \r\n"); |
AndrewLouw | 0:a32d085719df | 88 | checkLDR(LDR1,LDR2); |
AndrewLouw | 0:a32d085719df | 89 | checkMPPT(MPPT); |
AndrewLouw | 0:a32d085719df | 90 | if(shouldmove(MPPT,LDR1,LDR2,Upperlimit_Can_on,Lowerlimit_Can_on,panel_locations)){ |
AndrewLouw | 0:a32d085719df | 91 | sendlocations(panel_locations); |
AndrewLouw | 0:a32d085719df | 92 | } |
AndrewLouw | 2:64443528f6bb | 93 | else{ |
AndrewLouw | 1:e8c5a9654f5f | 94 | wait(120); |
AndrewLouw | 0:a32d085719df | 95 | } |
AndrewLouw | 0:a32d085719df | 96 | OpMode = readswitches(); |
AndrewLouw | 0:a32d085719df | 97 | break; |
AndrewLouw | 0:a32d085719df | 98 | |
AndrewLouw | 0:a32d085719df | 99 | case Canopy_off: |
AndrewLouw | 3:5995028595d2 | 100 | printf("Canopy_off mode \r\n"); |
AndrewLouw | 0:a32d085719df | 101 | checkLDR(LDR1,LDR2); |
AndrewLouw | 0:a32d085719df | 102 | checkMPPT(MPPT); |
AndrewLouw | 0:a32d085719df | 103 | if(shouldmove(MPPT,LDR1,LDR2,Upperlimit_Can_off,Lowerlimit_Can_off,panel_locations)){ |
AndrewLouw | 0:a32d085719df | 104 | sendlocations(panel_locations); |
AndrewLouw | 0:a32d085719df | 105 | } |
AndrewLouw | 0:a32d085719df | 106 | else{ |
AndrewLouw | 1:e8c5a9654f5f | 107 | wait(120); |
AndrewLouw | 0:a32d085719df | 108 | } |
AndrewLouw | 0:a32d085719df | 109 | OpMode = readswitches(); |
AndrewLouw | 0:a32d085719df | 110 | break; |
AndrewLouw | 0:a32d085719df | 111 | |
AndrewLouw | 0:a32d085719df | 112 | case Manual: |
AndrewLouw | 3:5995028595d2 | 113 | printf("Manual mode \r\n"); |
AndrewLouw | 2:64443528f6bb | 114 | //calibration mode - finds upper and lower limits for each panel for the current setup (canopy up or down) |
AndrewLouw | 1:e8c5a9654f5f | 115 | |
AndrewLouw | 1:e8c5a9654f5f | 116 | while(MANUAL_SW){//release the button |
AndrewLouw | 1:e8c5a9654f5f | 117 | } |
AndrewLouw | 1:e8c5a9654f5f | 118 | |
AndrewLouw | 1:e8c5a9654f5f | 119 | //panels should be moved to lower limit |
AndrewLouw | 1:e8c5a9654f5f | 120 | while(!MANUAL_SW){//then the switch should be pressed again |
AndrewLouw | 1:e8c5a9654f5f | 121 | } |
AndrewLouw | 1:e8c5a9654f5f | 122 | |
AndrewLouw | 1:e8c5a9654f5f | 123 | //TODO |
AndrewLouw | 1:e8c5a9654f5f | 124 | //panel locations will be read and values stored as lower limit |
AndrewLouw | 1:e8c5a9654f5f | 125 | |
AndrewLouw | 1:e8c5a9654f5f | 126 | |
AndrewLouw | 1:e8c5a9654f5f | 127 | while(MANUAL_SW){//release the button |
AndrewLouw | 1:e8c5a9654f5f | 128 | } |
AndrewLouw | 1:e8c5a9654f5f | 129 | |
AndrewLouw | 1:e8c5a9654f5f | 130 | //then panels should be moved to the upper limit |
AndrewLouw | 1:e8c5a9654f5f | 131 | while(!MANUAL_SW){//then the switch should be pressed again |
AndrewLouw | 1:e8c5a9654f5f | 132 | } |
AndrewLouw | 1:e8c5a9654f5f | 133 | |
AndrewLouw | 1:e8c5a9654f5f | 134 | //TODO |
AndrewLouw | 1:e8c5a9654f5f | 135 | //panel locations will be read and values stored as upper limit |
AndrewLouw | 1:e8c5a9654f5f | 136 | |
AndrewLouw | 1:e8c5a9654f5f | 137 | |
AndrewLouw | 1:e8c5a9654f5f | 138 | while(MANUAL_SW){//release the button |
AndrewLouw | 2:64443528f6bb | 139 | } |
AndrewLouw | 1:e8c5a9654f5f | 140 | |
AndrewLouw | 0:a32d085719df | 141 | break; |
AndrewLouw | 0:a32d085719df | 142 | |
AndrewLouw | 0:a32d085719df | 143 | |
AndrewLouw | 0:a32d085719df | 144 | case Error: |
AndrewLouw | 3:5995028595d2 | 145 | printf("Error mode \r\n"); |
AndrewLouw | 0:a32d085719df | 146 | ErrorFlag = Switch_Error; |
AndrewLouw | 0:a32d085719df | 147 | break; |
AndrewLouw | 0:a32d085719df | 148 | |
AndrewLouw | 0:a32d085719df | 149 | default: |
AndrewLouw | 3:5995028595d2 | 150 | printf("Default mode \r\n"); |
AndrewLouw | 0:a32d085719df | 151 | OpMode = Error; |
AndrewLouw | 0:a32d085719df | 152 | break; |
AndrewLouw | 0:a32d085719df | 153 | } |
AndrewLouw | 0:a32d085719df | 154 | |
AndrewLouw | 0:a32d085719df | 155 | } |
AndrewLouw | 0:a32d085719df | 156 | else{ //do this if shutting down |
AndrewLouw | 0:a32d085719df | 157 | switch (ErrorFlag){ |
AndrewLouw | 0:a32d085719df | 158 | case False: |
AndrewLouw | 0:a32d085719df | 159 | OpMode= Setup; |
AndrewLouw | 0:a32d085719df | 160 | break; |
AndrewLouw | 0:a32d085719df | 161 | |
AndrewLouw | 0:a32d085719df | 162 | case Switch_Error: |
AndrewLouw | 0:a32d085719df | 163 | OpMode = Canopy_on; |
AndrewLouw | 0:a32d085719df | 164 | break; |
AndrewLouw | 0:a32d085719df | 165 | |
AndrewLouw | 0:a32d085719df | 166 | default: |
AndrewLouw | 0:a32d085719df | 167 | Shutdown = true; |
AndrewLouw | 0:a32d085719df | 168 | break; |
AndrewLouw | 0:a32d085719df | 169 | } |
AndrewLouw | 0:a32d085719df | 170 | } |
AndrewLouw | 0:a32d085719df | 171 | if(Shutdown){ |
AndrewLouw | 0:a32d085719df | 172 | break; |
AndrewLouw | 0:a32d085719df | 173 | } |
AndrewLouw | 0:a32d085719df | 174 | } |
AndrewLouw | 3:5995028595d2 | 175 | |
AndrewLouw | 3:5995028595d2 | 176 | printf("Out of loop \r\n"); |
AndrewLouw | 3:5995028595d2 | 177 | |
AndrewLouw | 0:a32d085719df | 178 | } |
AndrewLouw | 0:a32d085719df | 179 | |
AndrewLouw | 0:a32d085719df | 180 | OpModesym readswitches(){ //read canopy switches |
AndrewLouw | 0:a32d085719df | 181 | if(MANUAL_SW){ |
AndrewLouw | 0:a32d085719df | 182 | return Manual; |
AndrewLouw | 0:a32d085719df | 183 | } |
AndrewLouw | 0:a32d085719df | 184 | else{ |
AndrewLouw | 0:a32d085719df | 185 | if(CANOPY_SW){ |
AndrewLouw | 0:a32d085719df | 186 | return Canopy_on; |
AndrewLouw | 0:a32d085719df | 187 | } |
AndrewLouw | 0:a32d085719df | 188 | else{ |
AndrewLouw | 0:a32d085719df | 189 | return Canopy_off; |
AndrewLouw | 0:a32d085719df | 190 | } |
AndrewLouw | 0:a32d085719df | 191 | } |
AndrewLouw | 0:a32d085719df | 192 | } |
AndrewLouw | 0:a32d085719df | 193 | void checkLDR(float &LDR1,float &LDR2){ //get the LDR values |
AndrewLouw | 0:a32d085719df | 194 | LDR1 = LDR_SENSE1; |
AndrewLouw | 0:a32d085719df | 195 | LDR2 = LDR_SENSE1; |
AndrewLouw | 0:a32d085719df | 196 | } |
AndrewLouw | 1:e8c5a9654f5f | 197 | |
AndrewLouw | 1:e8c5a9654f5f | 198 | void checkMPPT(float *MPPT){ //get the MPPT values |
AndrewLouw | 2:64443528f6bb | 199 | //TODO |
AndrewLouw | 0:a32d085719df | 200 | } |
AndrewLouw | 0:a32d085719df | 201 | |
AndrewLouw | 2:64443528f6bb | 202 | bool shouldmove(float *MPPT,float LDR1,float LDR2,double Upperlimit,double Lowelimit,uint8_t *panel_locations){ //checks if the motors should move based on current location and sensor data |
AndrewLouw | 0:a32d085719df | 203 | //TODO |
AndrewLouw | 1:e8c5a9654f5f | 204 | //need to use "getlocations" |
AndrewLouw | 2:64443528f6bb | 205 | return true; |
AndrewLouw | 0:a32d085719df | 206 | } |
AndrewLouw | 0:a32d085719df | 207 | |
AndrewLouw | 2:64443528f6bb | 208 | void getlocations(double Upperlimit, double Lowerlimit,uint8_t *panel_locations){ //update panel locations from actual to desired |
AndrewLouw | 0:a32d085719df | 209 | //TODO |
AndrewLouw | 1:e8c5a9654f5f | 210 | //update panellocations |
AndrewLouw | 0:a32d085719df | 211 | } |
AndrewLouw | 0:a32d085719df | 212 | |
AndrewLouw | 2:64443528f6bb | 213 | void sendlocations(uint8_t *panel_locations){ //transmit the desired locations |
AndrewLouw | 3:5995028595d2 | 214 | //TODO send useful information get output to be |
AndrewLouw | 0:a32d085719df | 215 | } |
AndrewLouw | 0:a32d085719df | 216 | |
AndrewLouw | 1:e8c5a9654f5f | 217 | uint8_t toEncoder(double angle){ //convert angle to encoder value |
AndrewLouw | 0:a32d085719df | 218 | //TODO |
AndrewLouw | 0:a32d085719df | 219 | } |
AndrewLouw | 0:a32d085719df | 220 | |
AndrewLouw | 3:5995028595d2 | 221 | void spi_send(uint8_t write_buf [8],uint8_t decoded_buf[7]){ |
AndrewLouw | 3:5995028595d2 | 222 | uint8_t read_buf [8]; |
AndrewLouw | 3:5995028595d2 | 223 | |
AndrewLouw | 3:5995028595d2 | 224 | SlaveSelect = 0; |
AndrewLouw | 3:5995028595d2 | 225 | spi.write(0b00000000); //Ensures isoSPI is in ready |
AndrewLouw | 3:5995028595d2 | 226 | SlaveSelect=1; |
AndrewLouw | 3:5995028595d2 | 227 | |
AndrewLouw | 3:5995028595d2 | 228 | wait_us(100); |
AndrewLouw | 3:5995028595d2 | 229 | SlaveSelect = 0; |
AndrewLouw | 3:5995028595d2 | 230 | wait_us(70); |
AndrewLouw | 3:5995028595d2 | 231 | for (int i = 0; i < 8; i++) { |
AndrewLouw | 3:5995028595d2 | 232 | spi.write(write_buf[i]); |
AndrewLouw | 3:5995028595d2 | 233 | wait_us(60); |
AndrewLouw | 3:5995028595d2 | 234 | } |
AndrewLouw | 3:5995028595d2 | 235 | |
AndrewLouw | 3:5995028595d2 | 236 | for (int i = 0; i < 8; i++) { |
AndrewLouw | 3:5995028595d2 | 237 | read_buf[i] = spi.write(0xFF); |
AndrewLouw | 3:5995028595d2 | 238 | wait_us(60); |
AndrewLouw | 3:5995028595d2 | 239 | } |
AndrewLouw | 3:5995028595d2 | 240 | SlaveSelect=1; |
AndrewLouw | 3:5995028595d2 | 241 | |
AndrewLouw | 3:5995028595d2 | 242 | uint8_t MSB_byte = read_buf[7]; |
AndrewLouw | 3:5995028595d2 | 243 | |
AndrewLouw | 3:5995028595d2 | 244 | for (int i = 0;i<7;i++){ |
AndrewLouw | 3:5995028595d2 | 245 | if(!(MSB_byte & (0b00000001<<(i)))){ |
AndrewLouw | 3:5995028595d2 | 246 | decoded_buf[i] = read_buf[i]&0b01111111; |
AndrewLouw | 3:5995028595d2 | 247 | } |
AndrewLouw | 3:5995028595d2 | 248 | else{ |
AndrewLouw | 3:5995028595d2 | 249 | decoded_buf[i] = read_buf[i]; |
AndrewLouw | 3:5995028595d2 | 250 | } |
AndrewLouw | 3:5995028595d2 | 251 | |
AndrewLouw | 3:5995028595d2 | 252 | |
AndrewLouw | 3:5995028595d2 | 253 | printf("decoded: %u \r\n",decoded_buf[i]); |
AndrewLouw | 3:5995028595d2 | 254 | } |
AndrewLouw | 3:5995028595d2 | 255 | for (int i = 0;i<8;i++){ |
AndrewLouw | 3:5995028595d2 | 256 | printf("read: %u \r\n",read_buf[i]); |
AndrewLouw | 3:5995028595d2 | 257 | } |
AndrewLouw | 3:5995028595d2 | 258 | |
AndrewLouw | 3:5995028595d2 | 259 | |
AndrewLouw | 3:5995028595d2 | 260 | wait_us(30); |
AndrewLouw | 3:5995028595d2 | 261 | printf("msg sent \r\n"); |
AndrewLouw | 3:5995028595d2 | 262 | return; |
AndrewLouw | 3:5995028595d2 | 263 | } |