Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp@36:5c61710813b3, 2023-06-21 (annotated)
- Committer:
- as96
- Date:
- Wed Jun 21 16:58:23 2023 +0000
- Revision:
- 36:5c61710813b3
- Parent:
- 35:2f476d981896
- Child:
- 38:1c93a9501929
Updated code with LCD SPI
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| rwcjoliver | 0:4788e1df7b55 | 1 | #include <mbed.h> |
| rwcjoliver | 0:4788e1df7b55 | 2 | #include "millis.h" |
| cdevarakonda | 28:1086791972d0 | 3 | #include "brakes.h" |
| rwcjoliver | 0:4788e1df7b55 | 4 | #include "definitions.h" |
| rwcjoliver | 0:4788e1df7b55 | 5 | #include "remoteControl.h" |
| rwcjoliver | 0:4788e1df7b55 | 6 | #include "dashboard.h" |
| rwcjoliver | 0:4788e1df7b55 | 7 | #include "rtc.h" |
| rwcjoliver | 0:4788e1df7b55 | 8 | #include "motor.h" |
| rwcjoliver | 0:4788e1df7b55 | 9 | #include "challenge.h" |
| jamesmcildowietfl | 3:32e951e05a5b | 10 | #include <sstream> |
| as96 | 36:5c61710813b3 | 11 | #include "lcd.h" |
| jamesmcildowietfl | 3:32e951e05a5b | 12 | using std::string; |
| rwcjoliver | 0:4788e1df7b55 | 13 | |
| rwcjoliver | 0:4788e1df7b55 | 14 | // SET UP REMOTE CONTROL COMMS |
| rwcjoliver | 0:4788e1df7b55 | 15 | SPI remoteControl(PE_14, PE_13, PE_12); // (SPI_MOSI, SPI_MISO, SPI_SCK) |
| rwcjoliver | 0:4788e1df7b55 | 16 | DigitalOut remoteControlCS(PE_11); // (SPI_SS) |
| rwcjoliver | 0:4788e1df7b55 | 17 | |
| as96 | 36:5c61710813b3 | 18 | // SET UP lcd COMMS |
| as96 | 36:5c61710813b3 | 19 | SPI lcd(PF_9, PF_8, PF_7); // (SPI_MOSI, SPI_MISO, SPI_SCK) |
| as96 | 36:5c61710813b3 | 20 | DigitalOut lcdCS(PF_6); // (SPI_SS) |
| as96 | 36:5c61710813b3 | 21 | |
| as96 | 36:5c61710813b3 | 22 | |
| rwcjoliver | 0:4788e1df7b55 | 23 | // CREATE OBJECTS |
| rwcjoliver | 0:4788e1df7b55 | 24 | Remote remote(remoteControl, remoteControlCS); |
| as96 | 36:5c61710813b3 | 25 | Lcd lcd1(lcd,lcdCS); |
| rwcjoliver | 0:4788e1df7b55 | 26 | Dashboard dashboard(hallSensor); |
| rwcjoliver | 0:4788e1df7b55 | 27 | RoundTrainCircuit rtc(rtc_1, rtc_2, rtc_3, rtc_4, rtc_5, rtc_6, rtc_7, rtc_override); |
| rwcjoliver | 0:4788e1df7b55 | 28 | Motor motor1(motorAccelerator, motorBrake, keySwitchM1, directionFwd, directionRev, footswitchM1, seatM1, inchFwdM1, speedLimit2M1, speedLimit3M1); |
| rwcjoliver | 0:4788e1df7b55 | 29 | ChallengeMode challenge(autoStopTrigger, dashboard, remote, motor1); |
| cdevarakonda | 30:c65bf90e8f47 | 30 | Brakes brakes; |
| rwcjoliver | 0:4788e1df7b55 | 31 | |
| cdevarakonda | 31:54292fdb96b3 | 32 | int driveMode = 2;// Drive mode - fwd(0), rev(1), park(2) |
| cdevarakonda | 31:54292fdb96b3 | 33 | bool emergencyStopActive = false; //default state for emergency stop being active |
| rwcjoliver | 0:4788e1df7b55 | 34 | |
| cdevarakonda | 31:54292fdb96b3 | 35 | ////////FUNCTIONS |
| rwcjoliver | 0:4788e1df7b55 | 36 | |
| jamesmcildowietfl | 3:32e951e05a5b | 37 | //Display Function for data logger |
| as96 | 36:5c61710813b3 | 38 | void DisplaySerial() |
| as96 | 36:5c61710813b3 | 39 | { |
| jamesmcildowietfl | 18:d28d458824d4 | 40 | std::stringstream displayline; |
| as96 | 36:5c61710813b3 | 41 | displayline << "Blackbox# " << " Motor Accelerator: " << motorAccelerator << " Brake 3/2: " << frontBrake<< " Brake 2/2: " << backBrake << " Speed: " << dashboard.currentSpeed << " Distance: " << dashboard.currentDistance << " Drive Mode: " << driveMode << "\n"; |
| jamesmcildowietfl | 20:0add93a69df5 | 42 | string disp = displayline.str(); |
| jamesmcildowietfl | 20:0add93a69df5 | 43 | pc.printf("%s \n", disp.c_str()); |
| as96 | 36:5c61710813b3 | 44 | } |
| rwcjoliver | 0:4788e1df7b55 | 45 | |
| jamesmcildowietfl | 18:d28d458824d4 | 46 | //Brake code |
| as96 | 36:5c61710813b3 | 47 | void brakeControl(int brakeRate) |
| as96 | 36:5c61710813b3 | 48 | { //set brake rate to float for regen |
| as96 | 36:5c61710813b3 | 49 | if (driveMode == 2) |
| as96 | 36:5c61710813b3 | 50 | { // PARK MODE |
| as96 | 36:5c61710813b3 | 51 | // All Mechanical brakes applied |
| as96 | 36:5c61710813b3 | 52 | brakes.ParkMode(motor1); |
| as96 | 36:5c61710813b3 | 53 | } |
| as96 | 36:5c61710813b3 | 54 | //REGEN BRAKING |
| cdevarakonda | 30:c65bf90e8f47 | 55 | |
| as96 | 36:5c61710813b3 | 56 | if (challenge.regenBrakingActive == true) |
| as96 | 36:5c61710813b3 | 57 | { // REGEN BRAKING WITH OVERVOLTAGE SAFETY CHECK |
| as96 | 36:5c61710813b3 | 58 | brakes.RegenControl(brakeRate,motor1); |
| as96 | 36:5c61710813b3 | 59 | |
| as96 | 36:5c61710813b3 | 60 | } |
| cdevarakonda | 27:5f801d4ca461 | 61 | |
| as96 | 36:5c61710813b3 | 62 | else |
| as96 | 36:5c61710813b3 | 63 | { // MECHANICAL BRAKING |
| as96 | 36:5c61710813b3 | 64 | brakes.MechanicalBraking(brakeRate,motor1); // calls mechanical braking |
| jamesmcildowietfl | 8:0fe9f7bde2f9 | 65 | } |
| as96 | 36:5c61710813b3 | 66 | |
| as96 | 36:5c61710813b3 | 67 | return; |
| rwcjoliver | 0:4788e1df7b55 | 68 | } |
| cdevarakonda | 30:c65bf90e8f47 | 69 | void emergencyStop() |
| cdevarakonda | 30:c65bf90e8f47 | 70 | { |
| cdevarakonda | 30:c65bf90e8f47 | 71 | brakes.EmergencyStop(motor1,rtc,emergencyStopActive); //invokes emergency stop code from brakes class |
| cdevarakonda | 30:c65bf90e8f47 | 72 | } |
| jamesmcildowietfl | 8:0fe9f7bde2f9 | 73 | //Motor code |
| as96 | 36:5c61710813b3 | 74 | void speedControl(int commandedSpeed) |
| as96 | 36:5c61710813b3 | 75 | { |
| as96 | 36:5c61710813b3 | 76 | switch (commandedSpeed) |
| as96 | 36:5c61710813b3 | 77 | { |
| jamesmcildowietfl | 8:0fe9f7bde2f9 | 78 | |
| jamesmcildowietfl | 8:0fe9f7bde2f9 | 79 | default: |
| jamesmcildowietfl | 8:0fe9f7bde2f9 | 80 | break; |
| jamesmcildowietfl | 8:0fe9f7bde2f9 | 81 | |
| jamesmcildowietfl | 8:0fe9f7bde2f9 | 82 | case 0: |
| jamesmcildowietfl | 8:0fe9f7bde2f9 | 83 | motor1.throttle(0.0f); |
| jamesmcildowietfl | 8:0fe9f7bde2f9 | 84 | break; |
| jamesmcildowietfl | 8:0fe9f7bde2f9 | 85 | |
| jamesmcildowietfl | 8:0fe9f7bde2f9 | 86 | case 1 ... 2: |
| jamesmcildowietfl | 8:0fe9f7bde2f9 | 87 | motor1.throttle(0.1f); |
| jamesmcildowietfl | 8:0fe9f7bde2f9 | 88 | break; |
| jamesmcildowietfl | 8:0fe9f7bde2f9 | 89 | |
| jamesmcildowietfl | 8:0fe9f7bde2f9 | 90 | case 3 ... 4: |
| jamesmcildowietfl | 8:0fe9f7bde2f9 | 91 | motor1.throttle(0.2f); |
| jamesmcildowietfl | 8:0fe9f7bde2f9 | 92 | break; |
| jamesmcildowietfl | 8:0fe9f7bde2f9 | 93 | |
| jamesmcildowietfl | 8:0fe9f7bde2f9 | 94 | case 5 ... 6: |
| jamesmcildowietfl | 8:0fe9f7bde2f9 | 95 | motor1.throttle(0.3f); |
| jamesmcildowietfl | 8:0fe9f7bde2f9 | 96 | break; |
| jamesmcildowietfl | 8:0fe9f7bde2f9 | 97 | |
| jamesmcildowietfl | 8:0fe9f7bde2f9 | 98 | case 7 ... 8: |
| jamesmcildowietfl | 8:0fe9f7bde2f9 | 99 | motor1.throttle(0.4f); |
| jamesmcildowietfl | 8:0fe9f7bde2f9 | 100 | break; |
| jamesmcildowietfl | 8:0fe9f7bde2f9 | 101 | |
| jamesmcildowietfl | 8:0fe9f7bde2f9 | 102 | case 9 ... 10: |
| jamesmcildowietfl | 8:0fe9f7bde2f9 | 103 | motor1.throttle(0.5f); |
| jamesmcildowietfl | 8:0fe9f7bde2f9 | 104 | break; |
| jamesmcildowietfl | 8:0fe9f7bde2f9 | 105 | |
| jamesmcildowietfl | 8:0fe9f7bde2f9 | 106 | case 11: |
| jamesmcildowietfl | 8:0fe9f7bde2f9 | 107 | motor1.throttle(0.6f); |
| jamesmcildowietfl | 8:0fe9f7bde2f9 | 108 | break; |
| jamesmcildowietfl | 8:0fe9f7bde2f9 | 109 | |
| jamesmcildowietfl | 8:0fe9f7bde2f9 | 110 | case 12: |
| jamesmcildowietfl | 8:0fe9f7bde2f9 | 111 | motor1.throttle(0.7f); |
| jamesmcildowietfl | 8:0fe9f7bde2f9 | 112 | break; |
| jamesmcildowietfl | 8:0fe9f7bde2f9 | 113 | |
| jamesmcildowietfl | 8:0fe9f7bde2f9 | 114 | case 13: |
| jamesmcildowietfl | 8:0fe9f7bde2f9 | 115 | motor1.throttle(0.8f); |
| jamesmcildowietfl | 8:0fe9f7bde2f9 | 116 | break; |
| jamesmcildowietfl | 8:0fe9f7bde2f9 | 117 | |
| jamesmcildowietfl | 8:0fe9f7bde2f9 | 118 | case 14: |
| jamesmcildowietfl | 8:0fe9f7bde2f9 | 119 | motor1.throttle(0.9f); |
| jamesmcildowietfl | 8:0fe9f7bde2f9 | 120 | break; |
| jamesmcildowietfl | 8:0fe9f7bde2f9 | 121 | |
| jamesmcildowietfl | 8:0fe9f7bde2f9 | 122 | case 15: |
| jamesmcildowietfl | 8:0fe9f7bde2f9 | 123 | motor1.throttle(1.0f); |
| jamesmcildowietfl | 8:0fe9f7bde2f9 | 124 | break; |
| jamesmcildowietfl | 8:0fe9f7bde2f9 | 125 | } |
| rwcjoliver | 0:4788e1df7b55 | 126 | } |
| cdevarakonda | 32:771f6d99a77b | 127 | ///Energy Storage |
| cdevarakonda | 32:771f6d99a77b | 128 | //energy storage display code |
| as96 | 36:5c61710813b3 | 129 | |
| cdevarakonda | 32:771f6d99a77b | 130 | void EnergyStorage() |
| cdevarakonda | 32:771f6d99a77b | 131 | { |
| cdevarakonda | 32:771f6d99a77b | 132 | float current_powercab=((2*vout_powercab)-vref_powercab)*250; //voltage change to current conversion |
| cdevarakonda | 32:771f6d99a77b | 133 | float current_supercap=((2*vout_supercap)-vref_supercap)*250; |
| cdevarakonda | 32:771f6d99a77b | 134 | int t=1; //1 second interval |
| cdevarakonda | 32:771f6d99a77b | 135 | int C= 250; //Capacitance Value |
| cdevarakonda | 32:771f6d99a77b | 136 | float energy_supercap = (1/2) * (current_supercap * current_supercap) * (t*t) / C; |
| cdevarakonda | 32:771f6d99a77b | 137 | float energy_powercab = (1/2) * (current_powercab * current_powercab) * (t*t) / C; |
| cdevarakonda | 32:771f6d99a77b | 138 | int scap = static_cast<int>(energy_supercap); |
| cdevarakonda | 33:9198b292a8eb | 139 | int pcab = static_cast<int>(current_powercab);//(current_powercab); |
| as96 | 36:5c61710813b3 | 140 | //remote.sendData(9,scap); |
| as96 | 36:5c61710813b3 | 141 | //remote.sendData(10,pcab); |
| as96 | 36:5c61710813b3 | 142 | //printf("Power Cab Current = %2f\n",current_powercab); |
| as96 | 36:5c61710813b3 | 143 | //printf("Super Cap Current = %2f\n",current_supercap); |
| as96 | 36:5c61710813b3 | 144 | remote.sendData(9,current_powercab); |
| as96 | 36:5c61710813b3 | 145 | remote.sendData(10,current_supercap); |
| cdevarakonda | 32:771f6d99a77b | 146 | |
| cdevarakonda | 35:2f476d981896 | 147 | } |
| as96 | 36:5c61710813b3 | 148 | |
| cdevarakonda | 32:771f6d99a77b | 149 | |
| rwcjoliver | 0:4788e1df7b55 | 150 | |
| as96 | 36:5c61710813b3 | 151 | int main() |
| as96 | 36:5c61710813b3 | 152 | { |
| jamesmcildowietfl | 8:0fe9f7bde2f9 | 153 | pc.baud(115200); |
| as96 | 36:5c61710813b3 | 154 | |
| as96 | 36:5c61710813b3 | 155 | |
| as96 | 36:5c61710813b3 | 156 | |
| jamesmcildowietfl | 8:0fe9f7bde2f9 | 157 | |
| jamesmcildowietfl | 8:0fe9f7bde2f9 | 158 | // CONFIGURE INTERRUPTS |
| jamesmcildowietfl | 8:0fe9f7bde2f9 | 159 | rtc_output.rise(&emergencyStop); |
| jamesmcildowietfl | 8:0fe9f7bde2f9 | 160 | |
| jamesmcildowietfl | 8:0fe9f7bde2f9 | 161 | millisStart(); |
| jamesmcildowietfl | 8:0fe9f7bde2f9 | 162 | |
| jamesmcildowietfl | 8:0fe9f7bde2f9 | 163 | rtc_Trigger = 1; |
| jamesmcildowietfl | 8:0fe9f7bde2f9 | 164 | |
| jamesmcildowietfl | 8:0fe9f7bde2f9 | 165 | // LOCAL VARIABLES |
| jamesmcildowietfl | 8:0fe9f7bde2f9 | 166 | bool systemOn = false; // On/Off status of loco |
| jamesmcildowietfl | 8:0fe9f7bde2f9 | 167 | int lastKnownDirection = 2; |
| jamesmcildowietfl | 8:0fe9f7bde2f9 | 168 | bool inParkMode = false; |
| jamesmcildowietfl | 8:0fe9f7bde2f9 | 169 | |
| as96 | 36:5c61710813b3 | 170 | |
| jamesmcildowietfl | 8:0fe9f7bde2f9 | 171 | // Record last time error was sent |
| jamesmcildowietfl | 8:0fe9f7bde2f9 | 172 | unsigned long lastErrorMillis = 0; |
| jamesmcildowietfl | 8:0fe9f7bde2f9 | 173 | |
| jamesmcildowietfl | 8:0fe9f7bde2f9 | 174 | //MainLoop |
| as96 | 36:5c61710813b3 | 175 | while (1) |
| as96 | 36:5c61710813b3 | 176 | { |
| jamesmcildowietfl | 8:0fe9f7bde2f9 | 177 | |
| as96 | 36:5c61710813b3 | 178 | while (remote.commsGood == true) |
| as96 | 36:5c61710813b3 | 179 | { |
| jamesmcildowietfl | 8:0fe9f7bde2f9 | 180 | /////Start Up/////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| jamesmcildowietfl | 8:0fe9f7bde2f9 | 181 | |
| jamesmcildowietfl | 8:0fe9f7bde2f9 | 182 | /////Checking Modes from controller///////////////////////////////////////////////////////////////////////////////// |
| jamesmcildowietfl | 8:0fe9f7bde2f9 | 183 | // PING |
| jamesmcildowietfl | 8:0fe9f7bde2f9 | 184 | remote.commsCheck(); |
| jamesmcildowietfl | 8:0fe9f7bde2f9 | 185 | |
| jamesmcildowietfl | 8:0fe9f7bde2f9 | 186 | // GET SWITCH STATES |
| jamesmcildowietfl | 8:0fe9f7bde2f9 | 187 | remote.getSwitchStates(); |
| jamesmcildowietfl | 8:0fe9f7bde2f9 | 188 | |
| jamesmcildowietfl | 8:0fe9f7bde2f9 | 189 | // ALLOW BRAKES TO BE OPERATED |
| jamesmcildowietfl | 8:0fe9f7bde2f9 | 190 | brakeControl(remote.braking); |
| cdevarakonda | 32:771f6d99a77b | 191 | |
| cdevarakonda | 32:771f6d99a77b | 192 | //Energy Storage Display |
| as96 | 36:5c61710813b3 | 193 | EnergyStorage(); |
| jamesmcildowietfl | 8:0fe9f7bde2f9 | 194 | |
| jamesmcildowietfl | 8:0fe9f7bde2f9 | 195 | // SOUND WHISTLE IF WHISTLE BUTTON PRESSED |
| as96 | 36:5c61710813b3 | 196 | remote.whistle == 0 ? whistle = 1 : whistle = 0; |
| jamesmcildowietfl | 8:0fe9f7bde2f9 | 197 | |
| jamesmcildowietfl | 8:0fe9f7bde2f9 | 198 | // GET AND DISPLAY SPEED |
| jamesmcildowietfl | 8:0fe9f7bde2f9 | 199 | dashboard.getCurrentSpeed(); |
| jamesmcildowietfl | 8:0fe9f7bde2f9 | 200 | remote.sendData(2, dashboard.currentSpeed); // Send speed to remote |
| as96 | 36:5c61710813b3 | 201 | //sprintf(dataToSend, "%f", dashboard.currentSpeed); |
| as96 | 36:5c61710813b3 | 202 | lcd1.sendData(2,dashboard.currentSpeed); |
| as96 | 36:5c61710813b3 | 203 | |
| jamesmcildowietfl | 8:0fe9f7bde2f9 | 204 | |
| jamesmcildowietfl | 8:0fe9f7bde2f9 | 205 | // TOGGLE COMPRESSOR |
| jamesmcildowietfl | 8:0fe9f7bde2f9 | 206 | remote.compressor == 0 ? contactCompressor = 1 : contactCompressor = 0; |
| jamesmcildowietfl | 8:0fe9f7bde2f9 | 207 | |
| jamesmcildowietfl | 8:0fe9f7bde2f9 | 208 | // TOGGLE MOTOR CONTROLLER DEADMAN (SEAT SWITCH AND FOOTBRAKE) |
| as96 | 36:5c61710813b3 | 209 | if (rtc.deadman == 0) |
| as96 | 36:5c61710813b3 | 210 | { // IF DEADMAN PRESSED |
| jamesmcildowietfl | 8:0fe9f7bde2f9 | 211 | motor1.closeDeadman(); |
| jamesmcildowietfl | 8:0fe9f7bde2f9 | 212 | } |
| as96 | 36:5c61710813b3 | 213 | else |
| as96 | 36:5c61710813b3 | 214 | { |
| jamesmcildowietfl | 8:0fe9f7bde2f9 | 215 | motor1.releaseDeadman(); |
| jamesmcildowietfl | 8:0fe9f7bde2f9 | 216 | } |
| jamesmcildowietfl | 8:0fe9f7bde2f9 | 217 | |
| jamesmcildowietfl | 8:0fe9f7bde2f9 | 218 | // TOGGLE REGEN THROTTLING |
| as96 | 36:5c61710813b3 | 219 | if (challenge.regenThrottleActive == false) |
| as96 | 36:5c61710813b3 | 220 | { |
| as96 | 36:5c61710813b3 | 221 | if (remote.regenThrottle == 0 && remote.start == 0) |
| as96 | 36:5c61710813b3 | 222 | { // TURN OFF IF ON |
| as96 | 36:5c61710813b3 | 223 | challenge.regenThrottleOn(); |
| as96 | 36:5c61710813b3 | 224 | } |
| jamesmcildowietfl | 8:0fe9f7bde2f9 | 225 | } |
| as96 | 36:5c61710813b3 | 226 | else |
| as96 | 36:5c61710813b3 | 227 | { |
| as96 | 36:5c61710813b3 | 228 | remote.sendError('B'); // Send error to remote |
| as96 | 36:5c61710813b3 | 229 | //lcd1.sendError('B'); |
| as96 | 36:5c61710813b3 | 230 | if (remote.regenThrottle == 1) |
| as96 | 36:5c61710813b3 | 231 | { // TURN ON IF OFF |
| as96 | 36:5c61710813b3 | 232 | challenge.regenThrottleOff(); |
| as96 | 36:5c61710813b3 | 233 | } |
| jamesmcildowietfl | 8:0fe9f7bde2f9 | 234 | } |
| rwcjoliver | 0:4788e1df7b55 | 235 | |
| jamesmcildowietfl | 8:0fe9f7bde2f9 | 236 | // TOGGLE REGEN BRAKING |
| as96 | 36:5c61710813b3 | 237 | if (challenge.regenBrakingActive == false) |
| as96 | 36:5c61710813b3 | 238 | { |
| as96 | 36:5c61710813b3 | 239 | if (remote.regenBrake == 0 && remote.start == 0) |
| as96 | 36:5c61710813b3 | 240 | { // TURN OFF IF ON |
| as96 | 36:5c61710813b3 | 241 | if (challenge.regenBrakingOn() == 0) |
| as96 | 36:5c61710813b3 | 242 | { |
| as96 | 36:5c61710813b3 | 243 | remote.sendError('I'); // Send error to remote |
| as96 | 36:5c61710813b3 | 244 | |
| as96 | 36:5c61710813b3 | 245 | //pc.printf("Regen Braking Off - SuperCaps are full\r\n"); |
| as96 | 36:5c61710813b3 | 246 | } |
| as96 | 36:5c61710813b3 | 247 | } |
| jamesmcildowietfl | 8:0fe9f7bde2f9 | 248 | } |
| as96 | 36:5c61710813b3 | 249 | else |
| as96 | 36:5c61710813b3 | 250 | { |
| as96 | 36:5c61710813b3 | 251 | remote.sendError('C'); // Send error to remote |
| as96 | 36:5c61710813b3 | 252 | //lcd1.sendError('C'); |
| as96 | 36:5c61710813b3 | 253 | if (remote.regenBrake == 1) |
| as96 | 36:5c61710813b3 | 254 | { // TURN OFF |
| as96 | 36:5c61710813b3 | 255 | challenge.regenBrakingOff(); |
| as96 | 36:5c61710813b3 | 256 | if (superCapVoltage == 1) |
| as96 | 36:5c61710813b3 | 257 | { |
| as96 | 36:5c61710813b3 | 258 | //pc.printf("Regen Braking Off - SuperCaps are full\r\n"); |
| as96 | 36:5c61710813b3 | 259 | remote.sendError('I'); // Send error to remote |
| as96 | 36:5c61710813b3 | 260 | } |
| as96 | 36:5c61710813b3 | 261 | } |
| jamesmcildowietfl | 8:0fe9f7bde2f9 | 262 | } |
| rwcjoliver | 0:4788e1df7b55 | 263 | |
| jamesmcildowietfl | 8:0fe9f7bde2f9 | 264 | // TOGGLE AUTOSTOP |
| as96 | 36:5c61710813b3 | 265 | if (challenge.autoStopActive == 0) |
| as96 | 36:5c61710813b3 | 266 | { |
| as96 | 36:5c61710813b3 | 267 | if (remote.autoStop == 0 && remote.start == 0) |
| as96 | 36:5c61710813b3 | 268 | { // TURN OFF IF ON |
| as96 | 36:5c61710813b3 | 269 | challenge.autoStopOn(); |
| as96 | 36:5c61710813b3 | 270 | } |
| jamesmcildowietfl | 8:0fe9f7bde2f9 | 271 | } |
| as96 | 36:5c61710813b3 | 272 | else |
| as96 | 36:5c61710813b3 | 273 | { |
| as96 | 36:5c61710813b3 | 274 | remote.sendError('D'); // Send error to remote |
| as96 | 36:5c61710813b3 | 275 | //lcd1.sendError('D'); |
| as96 | 36:5c61710813b3 | 276 | if (remote.autoStop == 1) |
| as96 | 36:5c61710813b3 | 277 | { // TURN ON IF OFF |
| as96 | 36:5c61710813b3 | 278 | challenge.autoStopOff(); |
| as96 | 36:5c61710813b3 | 279 | } |
| jamesmcildowietfl | 8:0fe9f7bde2f9 | 280 | } |
| rwcjoliver | 0:4788e1df7b55 | 281 | |
| jamesmcildowietfl | 8:0fe9f7bde2f9 | 282 | /////END OF TOGGLE CHECKS////////////////////////////////////////////////////////////////////////////////////////////// |
| rwcjoliver | 0:4788e1df7b55 | 283 | |
| jamesmcildowietfl | 8:0fe9f7bde2f9 | 284 | /////Control/////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| jamesmcildowietfl | 8:0fe9f7bde2f9 | 285 | //Is a Toggle check, but it is the train start swtich A |
| as96 | 36:5c61710813b3 | 286 | if (systemOn == false) |
| as96 | 36:5c61710813b3 | 287 | { |
| as96 | 36:5c61710813b3 | 288 | if (remote.start == 1) |
| as96 | 36:5c61710813b3 | 289 | { |
| as96 | 36:5c61710813b3 | 290 | if (millis() - lastErrorMillis > 500) |
| as96 | 36:5c61710813b3 | 291 | { |
| as96 | 36:5c61710813b3 | 292 | remote.sendError('A'); // SEND ERROR MESSAGE 'A' TO REMOTE |
| as96 | 36:5c61710813b3 | 293 | lcd1.sendError('A'); |
| as96 | 36:5c61710813b3 | 294 | lastErrorMillis = millis(); |
| as96 | 36:5c61710813b3 | 295 | } |
| as96 | 36:5c61710813b3 | 296 | motor1.turnOff(); |
| as96 | 36:5c61710813b3 | 297 | } |
| as96 | 36:5c61710813b3 | 298 | else |
| as96 | 36:5c61710813b3 | 299 | { |
| as96 | 36:5c61710813b3 | 300 | systemOn = true; |
| as96 | 36:5c61710813b3 | 301 | motor1.turnOn(); // Turn on motors |
| as96 | 36:5c61710813b3 | 302 | } |
| jamesmcildowietfl | 8:0fe9f7bde2f9 | 303 | } // END IF SYSTEMON = FALSE |
| jamesmcildowietfl | 8:0fe9f7bde2f9 | 304 | //If train is switched on and in start do this start/////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| as96 | 36:5c61710813b3 | 305 | else |
| as96 | 36:5c61710813b3 | 306 | { // IF SYSTEMON == TRUE |
| as96 | 36:5c61710813b3 | 307 | if (remote.start == 1) |
| as96 | 36:5c61710813b3 | 308 | { |
| as96 | 36:5c61710813b3 | 309 | systemOn = false; // WILL STOP ABOVE HERE NEXT LOOP |
| as96 | 36:5c61710813b3 | 310 | } |
| jamesmcildowietfl | 15:4976d145fbd9 | 311 | |
| as96 | 36:5c61710813b3 | 312 | //Set foward |
| as96 | 36:5c61710813b3 | 313 | if (driveMode != 0 && remote.forward == 0) |
| as96 | 36:5c61710813b3 | 314 | { |
| as96 | 36:5c61710813b3 | 315 | driveMode = 0; |
| as96 | 36:5c61710813b3 | 316 | motor1.setForward(); |
| as96 | 36:5c61710813b3 | 317 | frontLight=1; |
| as96 | 36:5c61710813b3 | 318 | backLight=0; |
| as96 | 36:5c61710813b3 | 319 | } |
| as96 | 36:5c61710813b3 | 320 | //Set reverse |
| as96 | 36:5c61710813b3 | 321 | if (driveMode != 1 && remote.reverse == 0) |
| as96 | 36:5c61710813b3 | 322 | { |
| as96 | 36:5c61710813b3 | 323 | driveMode = 1; |
| as96 | 36:5c61710813b3 | 324 | motor1.setReverse(); |
| as96 | 36:5c61710813b3 | 325 | backLight=1; |
| as96 | 36:5c61710813b3 | 326 | frontLight=0; |
| as96 | 36:5c61710813b3 | 327 | } |
| as96 | 36:5c61710813b3 | 328 | //Set park |
| as96 | 36:5c61710813b3 | 329 | if (driveMode != 2 && remote.park == 0) |
| as96 | 36:5c61710813b3 | 330 | { |
| as96 | 36:5c61710813b3 | 331 | driveMode = 2; |
| as96 | 36:5c61710813b3 | 332 | motor1.setPark(); |
| as96 | 36:5c61710813b3 | 333 | motor1.throttle(0); |
| as96 | 36:5c61710813b3 | 334 | } |
| as96 | 36:5c61710813b3 | 335 | ////Park Mode |
| as96 | 36:5c61710813b3 | 336 | if (driveMode == 2) |
| as96 | 36:5c61710813b3 | 337 | { |
| as96 | 36:5c61710813b3 | 338 | brakes.ParkMode(motor1); //place in park mode if selected by driver |
| as96 | 36:5c61710813b3 | 339 | backLight=0; |
| as96 | 36:5c61710813b3 | 340 | frontLight=0; |
| jamesmcildowietfl | 8:0fe9f7bde2f9 | 341 | |
| as96 | 36:5c61710813b3 | 342 | if (emergencyStopActive == true && rtc_output.read() == 0) |
| as96 | 36:5c61710813b3 | 343 | { // Clear emergency stop flag |
| as96 | 36:5c61710813b3 | 344 | emergencyStopActive = false; |
| as96 | 36:5c61710813b3 | 345 | } |
| jamesmcildowietfl | 8:0fe9f7bde2f9 | 346 | |
| as96 | 36:5c61710813b3 | 347 | led_parkMode = 1; |
| as96 | 36:5c61710813b3 | 348 | inParkMode = true; |
| as96 | 36:5c61710813b3 | 349 | motor1.setPark(); |
| rwcjoliver | 0:4788e1df7b55 | 350 | } |
| rwcjoliver | 0:4788e1df7b55 | 351 | |
| as96 | 36:5c61710813b3 | 352 | ////Drive |
| as96 | 36:5c61710813b3 | 353 | else |
| as96 | 36:5c61710813b3 | 354 | { //else i.e if selected drive mode is forward or reverse |
| as96 | 36:5c61710813b3 | 355 | ////////////////// Start of check for error G RTC clear |
| as96 | 36:5c61710813b3 | 356 | if (emergencyStopActive == false && rtc_output.read() == 0) |
| as96 | 36:5c61710813b3 | 357 | { // IF RTC FLAGGED AS SAFE |
| as96 | 36:5c61710813b3 | 358 | |
| as96 | 36:5c61710813b3 | 359 | led_parkMode = 0; |
| as96 | 36:5c61710813b3 | 360 | inParkMode = false; |
| as96 | 36:5c61710813b3 | 361 | |
| as96 | 36:5c61710813b3 | 362 | if (driveMode != lastKnownDirection) |
| as96 | 36:5c61710813b3 | 363 | { |
| as96 | 36:5c61710813b3 | 364 | //pc.printf("Train enabled for direction %d\r\n", driveMode); |
| as96 | 36:5c61710813b3 | 365 | |
| as96 | 36:5c61710813b3 | 366 | lastKnownDirection = driveMode; |
| as96 | 36:5c61710813b3 | 367 | } |
| jamesmcildowietfl | 8:0fe9f7bde2f9 | 368 | |
| as96 | 36:5c61710813b3 | 369 | ////Call autostop challenge |
| as96 | 36:5c61710813b3 | 370 | if (challenge.autoStopInProgress == true) |
| as96 | 36:5c61710813b3 | 371 | { // IF AUTOSTOPPING, PASS THROTTLE CONTROL TO FUNCTION |
| as96 | 36:5c61710813b3 | 372 | challenge.autoStopControl(); |
| as96 | 36:5c61710813b3 | 373 | //pc.printf("Autostop in Control\r\n"); |
| as96 | 36:5c61710813b3 | 374 | } |
| as96 | 36:5c61710813b3 | 375 | //Use controls from remote |
| as96 | 36:5c61710813b3 | 376 | else |
| as96 | 36:5c61710813b3 | 377 | { // OTHERWISE INPUT THROTTLE FROM REMOTE |
| as96 | 36:5c61710813b3 | 378 | if (remote.throttle > 0) |
| as96 | 36:5c61710813b3 | 379 | { // If joystick pushed upwards = throttle |
| as96 | 36:5c61710813b3 | 380 | //normal throttle control |
| as96 | 36:5c61710813b3 | 381 | speedControl(remote.throttle); |
| as96 | 36:5c61710813b3 | 382 | //pc.printf("Throttling: %d\r\n", remote.throttle); |
| as96 | 36:5c61710813b3 | 383 | |
| as96 | 36:5c61710813b3 | 384 | } // remote.throttle |
| as96 | 36:5c61710813b3 | 385 | ///if nothing set to 0 |
| as96 | 36:5c61710813b3 | 386 | else |
| as96 | 36:5c61710813b3 | 387 | { |
| as96 | 36:5c61710813b3 | 388 | speedControl(0); |
| as96 | 36:5c61710813b3 | 389 | } |
| as96 | 36:5c61710813b3 | 390 | } |
| as96 | 36:5c61710813b3 | 391 | |
| rwcjoliver | 0:4788e1df7b55 | 392 | } |
| as96 | 36:5c61710813b3 | 393 | ////////////////// End of check for error G RTC clear |
| as96 | 36:5c61710813b3 | 394 | else |
| as96 | 36:5c61710813b3 | 395 | { |
| as96 | 36:5c61710813b3 | 396 | //pc.printf("Cannot exit park mode until RTC is cleared\r\n"); |
| as96 | 36:5c61710813b3 | 397 | inParkMode = false; |
| as96 | 36:5c61710813b3 | 398 | remote.sendError('G'); // Send error to remote |
| jamesmcildowietfl | 8:0fe9f7bde2f9 | 399 | } |
| jamesmcildowietfl | 8:0fe9f7bde2f9 | 400 | } |
| jamesmcildowietfl | 8:0fe9f7bde2f9 | 401 | //Datalogger Chai Funciton |
| as96 | 36:5c61710813b3 | 402 | //DisplaySerial(); // Enable if you want to know what is happening with the peripherals |
| jamesmcildowietfl | 8:0fe9f7bde2f9 | 403 | } // END IF (SYSTEMON == TRUE) |
| jamesmcildowietfl | 8:0fe9f7bde2f9 | 404 | //If train is switched on and in start do this end/////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| rwcjoliver | 0:4788e1df7b55 | 405 | |
| jamesmcildowietfl | 19:a4afd3a6bdfb | 406 | wait_ms(25); // SLOW DOWN THE SYSTEM (REMOTE CANT KEEP UP) |
| jamesmcildowietfl | 8:0fe9f7bde2f9 | 407 | } // END WHILE(COMMSGOOD) |
| jamesmcildowietfl | 8:0fe9f7bde2f9 | 408 | pc.printf("Main Loop Skipped Due To Emergency Status\r\n"); |
| jamesmcildowietfl | 8:0fe9f7bde2f9 | 409 | emergencyStop(); // Emergency stop if comms lost with remote controller |
| rwcjoliver | 0:4788e1df7b55 | 410 | |
| rwcjoliver | 0:4788e1df7b55 | 411 | |
| jamesmcildowietfl | 8:0fe9f7bde2f9 | 412 | } //END WHILE(1) |
| jamesmcildowietfl | 8:0fe9f7bde2f9 | 413 | } |