Andrew Louw
/
TrackingMBED
MBED Framework with communication to be tested
main.cpp@2:64443528f6bb, 2017-07-16 (annotated)
- Committer:
- AndrewLouw
- Date:
- Sun Jul 16 18:43:28 2017 +0000
- Revision:
- 2:64443528f6bb
- Parent:
- 1:e8c5a9654f5f
- Child:
- 3:5995028595d2
Framework or MBED with communication to be tested;
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 | 0:a32d085719df | 5 | #include "CAN_Data.h" |
AndrewLouw | 0:a32d085719df | 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 | 0:a32d085719df | 11 | bool Shutdown; |
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 | 1:e8c5a9654f5f | 25 | float MPPT [5]; |
AndrewLouw | 1:e8c5a9654f5f | 26 | #define bat_volt MPPT[0] |
AndrewLouw | 1:e8c5a9654f5f | 27 | #define bat_current MPPT[1] |
AndrewLouw | 1:e8c5a9654f5f | 28 | #define bat_temp MPPT[2] |
AndrewLouw | 1:e8c5a9654f5f | 29 | #define bat_status MPPT[3] |
AndrewLouw | 1:e8c5a9654f5f | 30 | #define PV_volt MPPT[4] |
AndrewLouw | 1:e8c5a9654f5f | 31 | |
AndrewLouw | 2:64443528f6bb | 32 | uint8_t panel_locations [6]; //the array for actual/desired location of each panel |
AndrewLouw | 0:a32d085719df | 33 | |
AndrewLouw | 0:a32d085719df | 34 | //main loop. |
AndrewLouw | 0:a32d085719df | 35 | while(1){ |
AndrewLouw | 0:a32d085719df | 36 | if(ErrorFlag==False){ |
AndrewLouw | 0:a32d085719df | 37 | switch (OpMode){ |
AndrewLouw | 0:a32d085719df | 38 | case Setup: |
AndrewLouw | 0:a32d085719df | 39 | //setup SPI |
AndrewLouw | 2:64443528f6bb | 40 | const int spiBitrate=250000; |
AndrewLouw | 2:64443528f6bb | 41 | |
AndrewLouw | 0:a32d085719df | 42 | spi.format(8,3); |
AndrewLouw | 2:64443528f6bb | 43 | spi.frequency(spiBitrate); |
AndrewLouw | 2:64443528f6bb | 44 | SlaveSelect = 0; |
AndrewLouw | 0:a32d085719df | 45 | |
AndrewLouw | 0:a32d085719df | 46 | //start in correct mode |
AndrewLouw | 0:a32d085719df | 47 | OpMode = readswitches(); |
AndrewLouw | 0:a32d085719df | 48 | StatusLED = 1; |
AndrewLouw | 0:a32d085719df | 49 | break; |
AndrewLouw | 0:a32d085719df | 50 | |
AndrewLouw | 0:a32d085719df | 51 | case Canopy_on: |
AndrewLouw | 0:a32d085719df | 52 | checkLDR(LDR1,LDR2); |
AndrewLouw | 0:a32d085719df | 53 | checkMPPT(MPPT); |
AndrewLouw | 0:a32d085719df | 54 | if(shouldmove(MPPT,LDR1,LDR2,Upperlimit_Can_on,Lowerlimit_Can_on,panel_locations)){ |
AndrewLouw | 0:a32d085719df | 55 | sendlocations(panel_locations); |
AndrewLouw | 0:a32d085719df | 56 | } |
AndrewLouw | 2:64443528f6bb | 57 | else{ |
AndrewLouw | 1:e8c5a9654f5f | 58 | wait(120); |
AndrewLouw | 0:a32d085719df | 59 | } |
AndrewLouw | 0:a32d085719df | 60 | OpMode = readswitches(); |
AndrewLouw | 0:a32d085719df | 61 | break; |
AndrewLouw | 0:a32d085719df | 62 | |
AndrewLouw | 0:a32d085719df | 63 | case Canopy_off: |
AndrewLouw | 0:a32d085719df | 64 | checkLDR(LDR1,LDR2); |
AndrewLouw | 0:a32d085719df | 65 | checkMPPT(MPPT); |
AndrewLouw | 0:a32d085719df | 66 | if(shouldmove(MPPT,LDR1,LDR2,Upperlimit_Can_off,Lowerlimit_Can_off,panel_locations)){ |
AndrewLouw | 0:a32d085719df | 67 | sendlocations(panel_locations); |
AndrewLouw | 0:a32d085719df | 68 | } |
AndrewLouw | 0:a32d085719df | 69 | else{ |
AndrewLouw | 1:e8c5a9654f5f | 70 | wait(120); |
AndrewLouw | 0:a32d085719df | 71 | } |
AndrewLouw | 0:a32d085719df | 72 | OpMode = readswitches(); |
AndrewLouw | 0:a32d085719df | 73 | break; |
AndrewLouw | 0:a32d085719df | 74 | |
AndrewLouw | 0:a32d085719df | 75 | case Manual: |
AndrewLouw | 2:64443528f6bb | 76 | //calibration mode - finds upper and lower limits for each panel for the current setup (canopy up or down) |
AndrewLouw | 1:e8c5a9654f5f | 77 | |
AndrewLouw | 1:e8c5a9654f5f | 78 | while(MANUAL_SW){//release the button |
AndrewLouw | 1:e8c5a9654f5f | 79 | } |
AndrewLouw | 1:e8c5a9654f5f | 80 | |
AndrewLouw | 1:e8c5a9654f5f | 81 | //panels should be moved to lower limit |
AndrewLouw | 1:e8c5a9654f5f | 82 | while(!MANUAL_SW){//then the switch should be pressed again |
AndrewLouw | 1:e8c5a9654f5f | 83 | } |
AndrewLouw | 1:e8c5a9654f5f | 84 | |
AndrewLouw | 1:e8c5a9654f5f | 85 | //TODO |
AndrewLouw | 1:e8c5a9654f5f | 86 | //panel locations will be read and values stored as lower limit |
AndrewLouw | 1:e8c5a9654f5f | 87 | |
AndrewLouw | 1:e8c5a9654f5f | 88 | |
AndrewLouw | 1:e8c5a9654f5f | 89 | while(MANUAL_SW){//release the button |
AndrewLouw | 1:e8c5a9654f5f | 90 | } |
AndrewLouw | 1:e8c5a9654f5f | 91 | |
AndrewLouw | 1:e8c5a9654f5f | 92 | //then panels should be moved to the upper limit |
AndrewLouw | 1:e8c5a9654f5f | 93 | while(!MANUAL_SW){//then the switch should be pressed again |
AndrewLouw | 1:e8c5a9654f5f | 94 | } |
AndrewLouw | 1:e8c5a9654f5f | 95 | |
AndrewLouw | 1:e8c5a9654f5f | 96 | //TODO |
AndrewLouw | 1:e8c5a9654f5f | 97 | //panel locations will be read and values stored as upper limit |
AndrewLouw | 1:e8c5a9654f5f | 98 | |
AndrewLouw | 1:e8c5a9654f5f | 99 | |
AndrewLouw | 1:e8c5a9654f5f | 100 | while(MANUAL_SW){//release the button |
AndrewLouw | 2:64443528f6bb | 101 | } |
AndrewLouw | 1:e8c5a9654f5f | 102 | |
AndrewLouw | 0:a32d085719df | 103 | break; |
AndrewLouw | 0:a32d085719df | 104 | |
AndrewLouw | 0:a32d085719df | 105 | |
AndrewLouw | 0:a32d085719df | 106 | case Error: |
AndrewLouw | 0:a32d085719df | 107 | ErrorFlag = Switch_Error; |
AndrewLouw | 0:a32d085719df | 108 | break; |
AndrewLouw | 0:a32d085719df | 109 | |
AndrewLouw | 0:a32d085719df | 110 | default: |
AndrewLouw | 0:a32d085719df | 111 | OpMode = Error; |
AndrewLouw | 0:a32d085719df | 112 | break; |
AndrewLouw | 0:a32d085719df | 113 | } |
AndrewLouw | 0:a32d085719df | 114 | |
AndrewLouw | 0:a32d085719df | 115 | } |
AndrewLouw | 0:a32d085719df | 116 | else{ //do this if shutting down |
AndrewLouw | 0:a32d085719df | 117 | switch (ErrorFlag){ |
AndrewLouw | 0:a32d085719df | 118 | case False: |
AndrewLouw | 0:a32d085719df | 119 | OpMode= Setup; |
AndrewLouw | 0:a32d085719df | 120 | break; |
AndrewLouw | 0:a32d085719df | 121 | |
AndrewLouw | 0:a32d085719df | 122 | case Switch_Error: |
AndrewLouw | 0:a32d085719df | 123 | OpMode = Canopy_on; |
AndrewLouw | 0:a32d085719df | 124 | break; |
AndrewLouw | 0:a32d085719df | 125 | |
AndrewLouw | 0:a32d085719df | 126 | default: |
AndrewLouw | 0:a32d085719df | 127 | Shutdown = true; |
AndrewLouw | 0:a32d085719df | 128 | break; |
AndrewLouw | 0:a32d085719df | 129 | } |
AndrewLouw | 0:a32d085719df | 130 | } |
AndrewLouw | 0:a32d085719df | 131 | if(Shutdown){ |
AndrewLouw | 0:a32d085719df | 132 | break; |
AndrewLouw | 0:a32d085719df | 133 | } |
AndrewLouw | 0:a32d085719df | 134 | } |
AndrewLouw | 0:a32d085719df | 135 | } |
AndrewLouw | 0:a32d085719df | 136 | |
AndrewLouw | 0:a32d085719df | 137 | OpModesym readswitches(){ //read canopy switches |
AndrewLouw | 0:a32d085719df | 138 | if(MANUAL_SW){ |
AndrewLouw | 0:a32d085719df | 139 | return Manual; |
AndrewLouw | 0:a32d085719df | 140 | } |
AndrewLouw | 0:a32d085719df | 141 | else{ |
AndrewLouw | 0:a32d085719df | 142 | if(CANOPY_SW){ |
AndrewLouw | 0:a32d085719df | 143 | return Canopy_on; |
AndrewLouw | 0:a32d085719df | 144 | } |
AndrewLouw | 0:a32d085719df | 145 | else{ |
AndrewLouw | 0:a32d085719df | 146 | return Canopy_off; |
AndrewLouw | 0:a32d085719df | 147 | } |
AndrewLouw | 0:a32d085719df | 148 | } |
AndrewLouw | 0:a32d085719df | 149 | } |
AndrewLouw | 0:a32d085719df | 150 | void checkLDR(float &LDR1,float &LDR2){ //get the LDR values |
AndrewLouw | 0:a32d085719df | 151 | LDR1 = LDR_SENSE1; |
AndrewLouw | 0:a32d085719df | 152 | LDR2 = LDR_SENSE1; |
AndrewLouw | 0:a32d085719df | 153 | } |
AndrewLouw | 1:e8c5a9654f5f | 154 | |
AndrewLouw | 1:e8c5a9654f5f | 155 | void checkMPPT(float *MPPT){ //get the MPPT values |
AndrewLouw | 2:64443528f6bb | 156 | //TODO |
AndrewLouw | 0:a32d085719df | 157 | } |
AndrewLouw | 0:a32d085719df | 158 | |
AndrewLouw | 2:64443528f6bb | 159 | 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 | 160 | //TODO |
AndrewLouw | 1:e8c5a9654f5f | 161 | //need to use "getlocations" |
AndrewLouw | 2:64443528f6bb | 162 | return true; |
AndrewLouw | 0:a32d085719df | 163 | } |
AndrewLouw | 0:a32d085719df | 164 | |
AndrewLouw | 2:64443528f6bb | 165 | void getlocations(double Upperlimit, double Lowerlimit,uint8_t *panel_locations){ //update panel locations from actual to desired |
AndrewLouw | 0:a32d085719df | 166 | //TODO |
AndrewLouw | 1:e8c5a9654f5f | 167 | //update panellocations |
AndrewLouw | 0:a32d085719df | 168 | } |
AndrewLouw | 0:a32d085719df | 169 | |
AndrewLouw | 2:64443528f6bb | 170 | void sendlocations(uint8_t *panel_locations){ //transmit the desired locations |
AndrewLouw | 2:64443528f6bb | 171 | //TODO send useful information get output to be useful - break down the bytes into variables |
AndrewLouw | 2:64443528f6bb | 172 | uint8_t write_buf [8]; |
AndrewLouw | 2:64443528f6bb | 173 | uint8_t read_buf [8]; |
AndrewLouw | 2:64443528f6bb | 174 | uint8_t MSB_byte = read_buf[7]-128; |
AndrewLouw | 2:64443528f6bb | 175 | uint8_t decoded_buf[7]; |
AndrewLouw | 2:64443528f6bb | 176 | |
AndrewLouw | 2:64443528f6bb | 177 | SlaveSelect = 0; |
AndrewLouw | 2:64443528f6bb | 178 | spi.write(0b00000000); //Ensures isoSPI is in ready |
AndrewLouw | 2:64443528f6bb | 179 | SlaveSelect=1; |
AndrewLouw | 2:64443528f6bb | 180 | |
AndrewLouw | 2:64443528f6bb | 181 | wait_us(200); |
AndrewLouw | 2:64443528f6bb | 182 | |
AndrewLouw | 2:64443528f6bb | 183 | SlaveSelect = 0; |
AndrewLouw | 2:64443528f6bb | 184 | wait_us(70); |
AndrewLouw | 2:64443528f6bb | 185 | for (int i = 0; i < 8; ++i) { |
AndrewLouw | 2:64443528f6bb | 186 | spi.write(write_buf[i]); |
AndrewLouw | 1:e8c5a9654f5f | 187 | } |
AndrewLouw | 2:64443528f6bb | 188 | wait_us(100); |
AndrewLouw | 2:64443528f6bb | 189 | |
AndrewLouw | 2:64443528f6bb | 190 | for (int i = 0; i < 8; ++i) { |
AndrewLouw | 2:64443528f6bb | 191 | read_buf[i] = spi.write(0xFF); |
AndrewLouw | 2:64443528f6bb | 192 | //if(read_buf[i] == 160) printf("this is 160 \r\n"); |
AndrewLouw | 2:64443528f6bb | 193 | //wait_us(30); |
AndrewLouw | 2:64443528f6bb | 194 | } |
AndrewLouw | 2:64443528f6bb | 195 | |
AndrewLouw | 2:64443528f6bb | 196 | for (int i = 0;i<7;i++){ |
AndrewLouw | 2:64443528f6bb | 197 | if(MSB_byte & 1<<i) decoded_buf[i] =read_buf[i]+128; |
AndrewLouw | 2:64443528f6bb | 198 | } |
AndrewLouw | 2:64443528f6bb | 199 | |
AndrewLouw | 2:64443528f6bb | 200 | SlaveSelect=1; |
AndrewLouw | 2:64443528f6bb | 201 | |
AndrewLouw | 2:64443528f6bb | 202 | wait(1); |
AndrewLouw | 2:64443528f6bb | 203 | |
AndrewLouw | 0:a32d085719df | 204 | } |
AndrewLouw | 0:a32d085719df | 205 | |
AndrewLouw | 1:e8c5a9654f5f | 206 | uint8_t toEncoder(double angle){ //convert angle to encoder value |
AndrewLouw | 0:a32d085719df | 207 | //TODO |
AndrewLouw | 0:a32d085719df | 208 | } |
AndrewLouw | 0:a32d085719df | 209 |