Railway Challenge
/
challenge
Uncommenting of part that allow supercaps to charge up from the batteries
motor.cpp@0:4788e1df7b55, 2020-03-13 (annotated)
- Committer:
- rwcjoliver
- Date:
- Fri Mar 13 11:48:04 2020 +0000
- Revision:
- 0:4788e1df7b55
Uncommented RegenBRaking lines to allow capacitors to charge up from the batteries
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 "definitions.h" |
rwcjoliver | 0:4788e1df7b55 | 3 | #include "motor.h" |
rwcjoliver | 0:4788e1df7b55 | 4 | |
rwcjoliver | 0:4788e1df7b55 | 5 | Motor::Motor (AnalogOut& motorAccelerator, |
rwcjoliver | 0:4788e1df7b55 | 6 | AnalogOut& motorBrake, |
rwcjoliver | 0:4788e1df7b55 | 7 | DigitalOut& keySwitch, |
rwcjoliver | 0:4788e1df7b55 | 8 | DigitalOut& directionFwd, |
rwcjoliver | 0:4788e1df7b55 | 9 | DigitalOut& directionRev, |
rwcjoliver | 0:4788e1df7b55 | 10 | DigitalOut& footSwitch, |
rwcjoliver | 0:4788e1df7b55 | 11 | DigitalOut& seatSwitch, |
rwcjoliver | 0:4788e1df7b55 | 12 | DigitalOut& inchFwd, |
rwcjoliver | 0:4788e1df7b55 | 13 | DigitalOut& speedLimit2, |
rwcjoliver | 0:4788e1df7b55 | 14 | DigitalOut& speedLimit3) : |
rwcjoliver | 0:4788e1df7b55 | 15 | |
rwcjoliver | 0:4788e1df7b55 | 16 | _motorAccelerator(motorAccelerator), |
rwcjoliver | 0:4788e1df7b55 | 17 | _motorBrake(motorBrake), |
rwcjoliver | 0:4788e1df7b55 | 18 | _keySwitch(keySwitch), |
rwcjoliver | 0:4788e1df7b55 | 19 | _directionFwd(directionFwd), |
rwcjoliver | 0:4788e1df7b55 | 20 | _directionRev(directionRev), |
rwcjoliver | 0:4788e1df7b55 | 21 | _footSwitch(footSwitch), |
rwcjoliver | 0:4788e1df7b55 | 22 | _seatSwitch(seatSwitch), |
rwcjoliver | 0:4788e1df7b55 | 23 | _inchFwd(inchFwd), |
rwcjoliver | 0:4788e1df7b55 | 24 | _speedLimit2(speedLimit2), |
rwcjoliver | 0:4788e1df7b55 | 25 | _speedLimit3(speedLimit3) |
rwcjoliver | 0:4788e1df7b55 | 26 | { |
rwcjoliver | 0:4788e1df7b55 | 27 | _motorAccelerator = 0; |
rwcjoliver | 0:4788e1df7b55 | 28 | _motorBrake = 0; |
rwcjoliver | 0:4788e1df7b55 | 29 | // _contactMtr = 0; |
rwcjoliver | 0:4788e1df7b55 | 30 | _keySwitch = 0; |
rwcjoliver | 0:4788e1df7b55 | 31 | _directionFwd = 0; |
rwcjoliver | 0:4788e1df7b55 | 32 | _directionRev = 0; |
rwcjoliver | 0:4788e1df7b55 | 33 | _footSwitch = 0; |
rwcjoliver | 0:4788e1df7b55 | 34 | _seatSwitch = 0; |
rwcjoliver | 0:4788e1df7b55 | 35 | _inchFwd = 1; |
rwcjoliver | 0:4788e1df7b55 | 36 | _speedLimit2 = 1; |
rwcjoliver | 0:4788e1df7b55 | 37 | _speedLimit3 = 0; // SET MAX SPEED MODE BY DEFAULT |
rwcjoliver | 0:4788e1df7b55 | 38 | } |
rwcjoliver | 0:4788e1df7b55 | 39 | |
rwcjoliver | 0:4788e1df7b55 | 40 | |
rwcjoliver | 0:4788e1df7b55 | 41 | void Motor::turnOn() { |
rwcjoliver | 0:4788e1df7b55 | 42 | _keySwitch = 1; |
rwcjoliver | 0:4788e1df7b55 | 43 | // pc.printf("Motor Switched On\r\n"); |
rwcjoliver | 0:4788e1df7b55 | 44 | } |
rwcjoliver | 0:4788e1df7b55 | 45 | |
rwcjoliver | 0:4788e1df7b55 | 46 | void Motor::turnOff() { |
rwcjoliver | 0:4788e1df7b55 | 47 | _keySwitch = 0; |
rwcjoliver | 0:4788e1df7b55 | 48 | // pc.printf("Motor Switched Off\r\n"); |
rwcjoliver | 0:4788e1df7b55 | 49 | } |
rwcjoliver | 0:4788e1df7b55 | 50 | |
rwcjoliver | 0:4788e1df7b55 | 51 | void Motor::closeDeadman() { |
rwcjoliver | 0:4788e1df7b55 | 52 | _footSwitch = 1; |
rwcjoliver | 0:4788e1df7b55 | 53 | _seatSwitch = 1; |
rwcjoliver | 0:4788e1df7b55 | 54 | } |
rwcjoliver | 0:4788e1df7b55 | 55 | |
rwcjoliver | 0:4788e1df7b55 | 56 | void Motor::releaseDeadman() { |
rwcjoliver | 0:4788e1df7b55 | 57 | _footSwitch = 0; |
rwcjoliver | 0:4788e1df7b55 | 58 | _seatSwitch = 0; |
rwcjoliver | 0:4788e1df7b55 | 59 | } |
rwcjoliver | 0:4788e1df7b55 | 60 | |
rwcjoliver | 0:4788e1df7b55 | 61 | void Motor::setForward() { |
rwcjoliver | 0:4788e1df7b55 | 62 | _directionFwd = 1; |
rwcjoliver | 0:4788e1df7b55 | 63 | _directionRev = 0; |
rwcjoliver | 0:4788e1df7b55 | 64 | // pc.printf("Motor Set to Forward\r\n"); |
rwcjoliver | 0:4788e1df7b55 | 65 | } |
rwcjoliver | 0:4788e1df7b55 | 66 | |
rwcjoliver | 0:4788e1df7b55 | 67 | void Motor::setPark() { |
rwcjoliver | 0:4788e1df7b55 | 68 | _directionFwd = 0; |
rwcjoliver | 0:4788e1df7b55 | 69 | _directionRev = 0; |
rwcjoliver | 0:4788e1df7b55 | 70 | |
rwcjoliver | 0:4788e1df7b55 | 71 | // pc.printf("Motor Set to Park\r\n"); |
rwcjoliver | 0:4788e1df7b55 | 72 | } |
rwcjoliver | 0:4788e1df7b55 | 73 | |
rwcjoliver | 0:4788e1df7b55 | 74 | void Motor::setReverse() { |
rwcjoliver | 0:4788e1df7b55 | 75 | _directionFwd = 0; |
rwcjoliver | 0:4788e1df7b55 | 76 | _directionRev = 1; |
rwcjoliver | 0:4788e1df7b55 | 77 | // pc.printf("Motor Set to Reverse\r\n"); |
rwcjoliver | 0:4788e1df7b55 | 78 | } |
rwcjoliver | 0:4788e1df7b55 | 79 | |
rwcjoliver | 0:4788e1df7b55 | 80 | void Motor::engage() { |
rwcjoliver | 0:4788e1df7b55 | 81 | // _contactMtr = 1; |
rwcjoliver | 0:4788e1df7b55 | 82 | // pc.printf("Motor Contactor Engaged\r\n"); |
rwcjoliver | 0:4788e1df7b55 | 83 | } |
rwcjoliver | 0:4788e1df7b55 | 84 | |
rwcjoliver | 0:4788e1df7b55 | 85 | void Motor::disengage() { |
rwcjoliver | 0:4788e1df7b55 | 86 | // _contactMtr = 0; |
rwcjoliver | 0:4788e1df7b55 | 87 | |
rwcjoliver | 0:4788e1df7b55 | 88 | setPark(); |
rwcjoliver | 0:4788e1df7b55 | 89 | |
rwcjoliver | 0:4788e1df7b55 | 90 | // pc.printf("Motor Disengaged\r\n"); |
rwcjoliver | 0:4788e1df7b55 | 91 | } |
rwcjoliver | 0:4788e1df7b55 | 92 | |
rwcjoliver | 0:4788e1df7b55 | 93 | void Motor::setSpeedMode(int speed) { |
rwcjoliver | 0:4788e1df7b55 | 94 | switch (speed) { |
rwcjoliver | 0:4788e1df7b55 | 95 | case 0: // Clear limits |
rwcjoliver | 0:4788e1df7b55 | 96 | |
rwcjoliver | 0:4788e1df7b55 | 97 | _inchFwd = 0; |
rwcjoliver | 0:4788e1df7b55 | 98 | _speedLimit2 = 0; |
rwcjoliver | 0:4788e1df7b55 | 99 | _speedLimit3 = 0; |
rwcjoliver | 0:4788e1df7b55 | 100 | |
rwcjoliver | 0:4788e1df7b55 | 101 | // pc.printf("Motor Speed Cleared\r\n"); |
rwcjoliver | 0:4788e1df7b55 | 102 | break; |
rwcjoliver | 0:4788e1df7b55 | 103 | |
rwcjoliver | 0:4788e1df7b55 | 104 | case 1: // Inch Forward |
rwcjoliver | 0:4788e1df7b55 | 105 | |
rwcjoliver | 0:4788e1df7b55 | 106 | _inchFwd = 1; |
rwcjoliver | 0:4788e1df7b55 | 107 | _speedLimit2 = 0; |
rwcjoliver | 0:4788e1df7b55 | 108 | _speedLimit3 = 0; |
rwcjoliver | 0:4788e1df7b55 | 109 | |
rwcjoliver | 0:4788e1df7b55 | 110 | // pc.printf("Motor Set to Inch Forward\r\n"); |
rwcjoliver | 0:4788e1df7b55 | 111 | break; |
rwcjoliver | 0:4788e1df7b55 | 112 | |
rwcjoliver | 0:4788e1df7b55 | 113 | case 2: // Speed 2 |
rwcjoliver | 0:4788e1df7b55 | 114 | |
rwcjoliver | 0:4788e1df7b55 | 115 | _inchFwd = 0; |
rwcjoliver | 0:4788e1df7b55 | 116 | _speedLimit2 = 1; |
rwcjoliver | 0:4788e1df7b55 | 117 | _speedLimit3 = 0; |
rwcjoliver | 0:4788e1df7b55 | 118 | |
rwcjoliver | 0:4788e1df7b55 | 119 | // pc.printf("Motor Set to Speed Limit 2\r\n"); |
rwcjoliver | 0:4788e1df7b55 | 120 | break; |
rwcjoliver | 0:4788e1df7b55 | 121 | |
rwcjoliver | 0:4788e1df7b55 | 122 | case 3: // Speed 3 |
rwcjoliver | 0:4788e1df7b55 | 123 | |
rwcjoliver | 0:4788e1df7b55 | 124 | _inchFwd = 0; |
rwcjoliver | 0:4788e1df7b55 | 125 | _speedLimit2 = 0; |
rwcjoliver | 0:4788e1df7b55 | 126 | _speedLimit3 = 1; |
rwcjoliver | 0:4788e1df7b55 | 127 | |
rwcjoliver | 0:4788e1df7b55 | 128 | // pc.printf("Motor Set to Speed Limit 3\r\n"); |
rwcjoliver | 0:4788e1df7b55 | 129 | break; |
rwcjoliver | 0:4788e1df7b55 | 130 | } |
rwcjoliver | 0:4788e1df7b55 | 131 | } |
rwcjoliver | 0:4788e1df7b55 | 132 | |
rwcjoliver | 0:4788e1df7b55 | 133 | void Motor::throttle(float throttleRate) { |
rwcjoliver | 0:4788e1df7b55 | 134 | // set the output voltage of the analog output pin specified as a percentage of Vcc (3.3V) |
rwcjoliver | 0:4788e1df7b55 | 135 | _motorAccelerator = throttleRate; // Analog value between 0.0f and 1.0f |
rwcjoliver | 0:4788e1df7b55 | 136 | // pc.printf("Throttling\r\n"); |
rwcjoliver | 0:4788e1df7b55 | 137 | |
rwcjoliver | 0:4788e1df7b55 | 138 | } |
rwcjoliver | 0:4788e1df7b55 | 139 | |
rwcjoliver | 0:4788e1df7b55 | 140 | void Motor::brake(float brakeRate) { |
rwcjoliver | 0:4788e1df7b55 | 141 | _motorBrake = brakeRate; // Analog value between 0.0f and 1.0f |
rwcjoliver | 0:4788e1df7b55 | 142 | // pc.printf("Motor Braking\r\n"); |
rwcjoliver | 0:4788e1df7b55 | 143 | } |