
first commit
main.cpp@25:8bd029d58251, 2021-11-06 (annotated)
- Committer:
- aalawfi
- Date:
- Sat Nov 06 16:31:02 2021 +0000
- Revision:
- 25:8bd029d58251
- Parent:
- 24:7bf492bf50f4
- Child:
- 26:54ce9f642477
-
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
quarren42 | 19:65fecaa2a387 | 1 | #include "MMA8451Q.h" |
quarren42 | 0:0a6756c7e3ed | 2 | #include "mbed.h" |
aalawfi | 25:8bd029d58251 | 3 | #include "steering_header.h" |
quarren42 | 1:c324a2849500 | 4 | #include "driving.h" |
aalawfi | 2:c857935f928e | 5 | #include "steering_methods.h" |
aalawfi | 2:c857935f928e | 6 | #include "state_control.h" |
quarren42 | 19:65fecaa2a387 | 7 | |
quarren42 | 0:0a6756c7e3ed | 8 | #include <iostream> |
aalawfi | 3:25c6bf0abc00 | 9 | #include "bluetooth.h" |
quarren42 | 19:65fecaa2a387 | 10 | |
quarren42 | 1:c324a2849500 | 11 | |
quarren42 | 0:0a6756c7e3ed | 12 | Serial pc(USBTX, USBRX); // tx, rx |
aalawfi | 3:25c6bf0abc00 | 13 | |
quarren42 | 0:0a6756c7e3ed | 14 | |
quarren42 | 1:c324a2849500 | 15 | |
quarren42 | 1:c324a2849500 | 16 | Ticker motorLoop; |
aalawfi | 7:05ea1c004b49 | 17 | Ticker fault_detector; |
aalawfi | 9:5320c2dfb913 | 18 | Ticker controlUpdate; |
quarren42 | 1:c324a2849500 | 19 | |
aalawfi | 9:5320c2dfb913 | 20 | void PID (void) { |
aalawfi | 9:5320c2dfb913 | 21 | steering_control(); |
aalawfi | 9:5320c2dfb913 | 22 | PI(); |
aalawfi | 9:5320c2dfb913 | 23 | }; |
quarren42 | 18:831c1e03d83e | 24 | |
quarren42 | 19:65fecaa2a387 | 25 | |
aalawfi | 3:25c6bf0abc00 | 26 | int main() { |
quarren42 | 19:65fecaa2a387 | 27 | |
aalawfi | 2:c857935f928e | 28 | state_update(); |
aalawfi | 2:c857935f928e | 29 | //Delcare Onboard LED with blue color |
aalawfi | 2:c857935f928e | 30 | DigitalOut led_b(LED_BLUE); |
aalawfi | 2:c857935f928e | 31 | //Set the period of the servo motor control signal |
aalawfi | 2:c857935f928e | 32 | servo_motor_pwm.period(1/SERVO_MOTOR_FREQ); |
quarren42 | 6:d2bd68ba99c9 | 33 | motorLeft.period(1/freq); |
quarren42 | 6:d2bd68ba99c9 | 34 | motorRight.period(1/freq); |
aalawfi | 14:eb9c58c0f8cd | 35 | //controlUpdate.attach(&PID, TI); |
aalawfi | 2:c857935f928e | 36 | //Center the servo motor |
aalawfi | 14:eb9c58c0f8cd | 37 | servo_motor_pwm.write(CENTER_DUTY_CYCLE); |
quarren42 | 20:7dcdadbd7bc0 | 38 | // controlUpdate.attach(&PID, TI); |
aalawfi | 2:c857935f928e | 39 | //Start the control systm using a Ticker object |
aalawfi | 22:08d30ea47111 | 40 | steering_control_ticker.attach(&steering_control, TI_STEERING); |
aalawfi | 14:eb9c58c0f8cd | 41 | motorLoop.attach(&PI,TI); |
quarren42 | 18:831c1e03d83e | 42 | fault_detector.attach(&fault_check, 0.003); |
quarren42 | 6:d2bd68ba99c9 | 43 | //bt.attach(&btReceive); |
aalawfi | 2:c857935f928e | 44 | |
aalawfi | 2:c857935f928e | 45 | // call landmark_counter wjen a landmark is detected |
aalawfi | 2:c857935f928e | 46 | left_landmark_sensor.rise(&landmark_counter); |
aalawfi | 2:c857935f928e | 47 | right_landmark_sensor.rise(&landmark_counter); |
aalawfi | 2:c857935f928e | 48 | // update status when the button is pressed |
aalawfi | 2:c857935f928e | 49 | stop_button.rise(&state_update); |
aalawfi | 2:c857935f928e | 50 | run_button.rise(&state_update); |
aalawfi | 2:c857935f928e | 51 | wait_button.rise(&state_update); |
aalawfi | 2:c857935f928e | 52 | |
quarren42 | 0:0a6756c7e3ed | 53 | bt.baud(BLUETOOTHBAUDRATE); |
quarren42 | 0:0a6756c7e3ed | 54 | //Sets the communication rate of the micro-controller to the Bluetooth module. |
quarren42 | 0:0a6756c7e3ed | 55 | pc.printf("Hello World!\n"); |
quarren42 | 1:c324a2849500 | 56 | bt.printf("Hello World!\n"); |
aalawfi | 25:8bd029d58251 | 57 | Timer t; |
aalawfi | 25:8bd029d58251 | 58 | t.start(); |
quarren42 | 0:0a6756c7e3ed | 59 | while(1) { |
aalawfi | 25:8bd029d58251 | 60 | wait(0.2); |
aalawfi | 2:c857935f928e | 61 | pc.printf("\n\n"); |
quarren42 | 17:d2c98ebda90b | 62 | /* |
aalawfi | 9:5320c2dfb913 | 63 | bt.printf("\n\n"); |
aalawfi | 9:5320c2dfb913 | 64 | bt.printf("\n\rMotor enabled ? : %d ", motor_enabled ); |
aalawfi | 9:5320c2dfb913 | 65 | bt.printf("\n\rSteering enabled ? : %d ", steering_enabled ); |
aalawfi | 9:5320c2dfb913 | 66 | bt.printf("\n\rCurrent duty cycle : %f", current_duty_cycle); |
aalawfi | 13:0091da3021df | 67 | bt.printf("\n\Brake enabled ? : %d ", are_brakes_activated ); |
aalawfi | 9:5320c2dfb913 | 68 | bt.printf("\n\rCurrent duty cycle? : %f ", current_duty_cycle ); |
quarren42 | 17:d2c98ebda90b | 69 | */ |
quarren42 | 6:d2bd68ba99c9 | 70 | //wait(0.5); //commented out the wait bc it slows down the fault_check, and it breaks the analogIn readings for the driving input |
quarren42 | 6:d2bd68ba99c9 | 71 | //the driving input ticker is faster than the analog.read() function, so all analog.read() methods must be in the main loop |
quarren42 | 18:831c1e03d83e | 72 | switch(current_state){ |
aalawfi | 9:5320c2dfb913 | 73 | case STOP : bt.printf("\r\nCurrent state is stop"); break; |
aalawfi | 9:5320c2dfb913 | 74 | case RUN: bt.printf("\r\nCurrent state is RUN"); break; |
aalawfi | 9:5320c2dfb913 | 75 | case WAIT : bt.printf("\r\nCurrent state is WAIT"); break; |
aalawfi | 2:c857935f928e | 76 | }; |
aalawfi | 2:c857935f928e | 77 | switch(fault_type) { |
aalawfi | 9:5320c2dfb913 | 78 | case CLEAR : bt.printf("\r\nFault: CLEAR"); break; |
aalawfi | 9:5320c2dfb913 | 79 | case OFF_TRACK: bt.printf("\r\nFault: OFF TRACK"); break; |
quarren42 | 19:65fecaa2a387 | 80 | case COLLISION : bt.printf("\r\nFault: COLLISION"); break; |
aalawfi | 9:5320c2dfb913 | 81 | case LOW_VOLTAGE : bt.printf("\r\nFault: LOW VOLTAGE"); break; |
aalawfi | 2:c857935f928e | 82 | } |
aalawfi | 25:8bd029d58251 | 83 | bt.printf("\n\n----"); |
aalawfi | 25:8bd029d58251 | 84 | // bt.printf("%d", t.read_ms()); |
aalawfi | 25:8bd029d58251 | 85 | bt.printf("\n\rLeft distance sens (ADC) : %1.5f", left_sens); |
aalawfi | 25:8bd029d58251 | 86 | bt.printf("\n\rRight distance sens (ADC): %1.5f",right_sens); |
aalawfi | 25:8bd029d58251 | 87 | bt.printf("\n\rDifference (ADC): %1.5f",left_sens - right_sens); |
aalawfi | 25:8bd029d58251 | 88 | bt.printf("\n\rFeedback (m) : %1.5f", feedback); |
aalawfi | 25:8bd029d58251 | 89 | bt.printf("\n\rLap: %d", lap); |
aalawfi | 25:8bd029d58251 | 90 | // bt.printf("\n\ravgCellVolt;ag : %1.5f", avgCellVoltag); |
quarren42 | 18:831c1e03d83e | 91 | //pot1Voltage = pot1 * 3.3f; |
quarren42 | 0:0a6756c7e3ed | 92 | //dutyCycleLeft = (pot1 * fullBatScalar); |
quarren42 | 0:0a6756c7e3ed | 93 | //dutyCycleRight = (pot1 * fullBatScalar); |
quarren42 | 0:0a6756c7e3ed | 94 | |
quarren42 | 1:c324a2849500 | 95 | //speedLeft = (speedSensorLeft * speedSensorScalar); |
quarren42 | 1:c324a2849500 | 96 | //speedRight = (speedSensorRight * speedSensorScalar); |
quarren42 | 20:7dcdadbd7bc0 | 97 | /* |
quarren42 | 1:c324a2849500 | 98 | bt.printf("Duty Cycle = %1.2f ", dutyCycleLeft); |
quarren42 | 1:c324a2849500 | 99 | bt.printf("Speed (V) L,R = %1.2f", speedLeftVolt); |
quarren42 | 1:c324a2849500 | 100 | bt.printf(", %1.2f ", speedRightVolt); |
quarren42 | 1:c324a2849500 | 101 | |
quarren42 | 1:c324a2849500 | 102 | bt.printf("Error L,R: %5.2f", errorLeft); |
quarren42 | 1:c324a2849500 | 103 | bt.printf(", %5.2f ", errorRight); |
quarren42 | 1:c324a2849500 | 104 | |
quarren42 | 1:c324a2849500 | 105 | bt.printf("ErrorArea: %1.2f ", errorAreaLeft); |
quarren42 | 18:831c1e03d83e | 106 | pc.printf("Output: %1.2f ", controllerOutputLeft); |
quarren42 | 1:c324a2849500 | 107 | |
quarren42 | 18:831c1e03d83e | 108 | pc.printf("Batt Voltage: %1.2f \n",avgCellVoltage); |
quarren42 | 20:7dcdadbd7bc0 | 109 | */ |
quarren42 | 1:c324a2849500 | 110 | //setpointLeft = pot1; |
quarren42 | 1:c324a2849500 | 111 | //setpointRight = pot1; |
quarren42 | 0:0a6756c7e3ed | 112 | |
aalawfi | 9:5320c2dfb913 | 113 | /* |
quarren42 | 1:c324a2849500 | 114 | if (t.read() > 5){ |
quarren42 | 1:c324a2849500 | 115 | setpointLeft = 0.0; |
quarren42 | 1:c324a2849500 | 116 | setpointRight = 0.0; |
quarren42 | 1:c324a2849500 | 117 | } |
quarren42 | 1:c324a2849500 | 118 | |
quarren42 | 1:c324a2849500 | 119 | if (t.read_ms() > 5100){ |
quarren42 | 1:c324a2849500 | 120 | setpointLeft = 0.2; |
quarren42 | 1:c324a2849500 | 121 | setpointRight = 0.2; |
quarren42 | 1:c324a2849500 | 122 | } |
aalawfi | 9:5320c2dfb913 | 123 | */ |
quarren42 | 0:0a6756c7e3ed | 124 | |
quarren42 | 1:c324a2849500 | 125 | if (newData){ |
quarren42 | 1:c324a2849500 | 126 | newData = false; |
quarren42 | 1:c324a2849500 | 127 | // bt.printf("Got %c %3i\n",newInputChar, newInputInt); |
quarren42 | 1:c324a2849500 | 128 | |
quarren42 | 1:c324a2849500 | 129 | if (newInputChar == 'L') |
quarren42 | 1:c324a2849500 | 130 | setpointLeft = (newInputInt / 100.0f); |
quarren42 | 1:c324a2849500 | 131 | if (newInputChar == 'R') |
quarren42 | 1:c324a2849500 | 132 | setpointRight = (newInputInt / 100.0f); |
quarren42 | 1:c324a2849500 | 133 | |
quarren42 | 0:0a6756c7e3ed | 134 | |
quarren42 | 1:c324a2849500 | 135 | //wait(0.1); |
quarren42 | 1:c324a2849500 | 136 | } |
quarren42 | 1:c324a2849500 | 137 | } |
quarren42 | 1:c324a2849500 | 138 | } |
quarren42 | 1:c324a2849500 | 139 |