
first commit
main.cpp@21:9e6ddb590103, 2021-10-29 (annotated)
- Committer:
- quarren42
- Date:
- Fri Oct 29 18:42:13 2021 +0000
- Revision:
- 21:9e6ddb590103
- Parent:
- 20:7dcdadbd7bc0
- Child:
- 22:08d30ea47111
latest commit
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" |
quarren42 | 1:c324a2849500 | 3 | #include "driving.h" |
aalawfi | 2:c857935f928e | 4 | #include "steering_header.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; |
quarren42 | 1:c324a2849500 | 18 | Timer t; |
aalawfi | 9:5320c2dfb913 | 19 | Ticker controlUpdate; |
quarren42 | 1:c324a2849500 | 20 | |
aalawfi | 9:5320c2dfb913 | 21 | void PID (void) { |
aalawfi | 9:5320c2dfb913 | 22 | steering_control(); |
aalawfi | 9:5320c2dfb913 | 23 | PI(); |
aalawfi | 9:5320c2dfb913 | 24 | }; |
quarren42 | 18:831c1e03d83e | 25 | |
quarren42 | 19:65fecaa2a387 | 26 | |
aalawfi | 3:25c6bf0abc00 | 27 | int main() { |
quarren42 | 19:65fecaa2a387 | 28 | |
aalawfi | 2:c857935f928e | 29 | state_update(); |
aalawfi | 2:c857935f928e | 30 | //Delcare Onboard LED with blue color |
aalawfi | 2:c857935f928e | 31 | DigitalOut led_b(LED_BLUE); |
aalawfi | 2:c857935f928e | 32 | //Set the period of the servo motor control signal |
aalawfi | 2:c857935f928e | 33 | servo_motor_pwm.period(1/SERVO_MOTOR_FREQ); |
quarren42 | 6:d2bd68ba99c9 | 34 | motorLeft.period(1/freq); |
quarren42 | 6:d2bd68ba99c9 | 35 | motorRight.period(1/freq); |
aalawfi | 14:eb9c58c0f8cd | 36 | //controlUpdate.attach(&PID, TI); |
aalawfi | 2:c857935f928e | 37 | //Center the servo motor |
aalawfi | 14:eb9c58c0f8cd | 38 | servo_motor_pwm.write(CENTER_DUTY_CYCLE); |
quarren42 | 20:7dcdadbd7bc0 | 39 | // controlUpdate.attach(&PID, TI); |
aalawfi | 2:c857935f928e | 40 | //Start the control systm using a Ticker object |
aalawfi | 14:eb9c58c0f8cd | 41 | steering_control_ticker.attach(&steering_control, 0.02); |
aalawfi | 14:eb9c58c0f8cd | 42 | motorLoop.attach(&PI,TI); |
quarren42 | 18:831c1e03d83e | 43 | fault_detector.attach(&fault_check, 0.003); |
quarren42 | 6:d2bd68ba99c9 | 44 | //bt.attach(&btReceive); |
aalawfi | 2:c857935f928e | 45 | |
aalawfi | 2:c857935f928e | 46 | // call landmark_counter wjen a landmark is detected |
aalawfi | 2:c857935f928e | 47 | left_landmark_sensor.rise(&landmark_counter); |
aalawfi | 2:c857935f928e | 48 | right_landmark_sensor.rise(&landmark_counter); |
aalawfi | 2:c857935f928e | 49 | // update status when the button is pressed |
aalawfi | 2:c857935f928e | 50 | stop_button.rise(&state_update); |
aalawfi | 2:c857935f928e | 51 | run_button.rise(&state_update); |
aalawfi | 2:c857935f928e | 52 | wait_button.rise(&state_update); |
aalawfi | 2:c857935f928e | 53 | |
quarren42 | 0:0a6756c7e3ed | 54 | bt.baud(BLUETOOTHBAUDRATE); |
quarren42 | 0:0a6756c7e3ed | 55 | //Sets the communication rate of the micro-controller to the Bluetooth module. |
quarren42 | 0:0a6756c7e3ed | 56 | pc.printf("Hello World!\n"); |
quarren42 | 1:c324a2849500 | 57 | bt.printf("Hello World!\n"); |
quarren42 | 1:c324a2849500 | 58 | |
quarren42 | 17:d2c98ebda90b | 59 | //t.start(); |
quarren42 | 17:d2c98ebda90b | 60 | //float time = t.read(); |
quarren42 | 1:c324a2849500 | 61 | |
quarren42 | 0:0a6756c7e3ed | 62 | //prints the string to the Tera-Term Console using the Bluetooth object ‘bt’. |
quarren42 | 0:0a6756c7e3ed | 63 | while(1) { |
aalawfi | 2:c857935f928e | 64 | |
aalawfi | 2:c857935f928e | 65 | pc.printf("\n\n"); |
quarren42 | 20:7dcdadbd7bc0 | 66 | wait(1); |
quarren42 | 17:d2c98ebda90b | 67 | /* |
aalawfi | 9:5320c2dfb913 | 68 | bt.printf("\n\n"); |
aalawfi | 9:5320c2dfb913 | 69 | bt.printf("\n\rMotor enabled ? : %d ", motor_enabled ); |
aalawfi | 9:5320c2dfb913 | 70 | bt.printf("\n\rSteering enabled ? : %d ", steering_enabled ); |
aalawfi | 9:5320c2dfb913 | 71 | bt.printf("\n\rCurrent duty cycle : %f", current_duty_cycle); |
aalawfi | 13:0091da3021df | 72 | bt.printf("\n\Brake enabled ? : %d ", are_brakes_activated ); |
aalawfi | 9:5320c2dfb913 | 73 | bt.printf("\n\rCurrent duty cycle? : %f ", current_duty_cycle ); |
quarren42 | 17:d2c98ebda90b | 74 | */ |
quarren42 | 6:d2bd68ba99c9 | 75 | //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 | 76 | //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 | 77 | switch(current_state){ |
aalawfi | 9:5320c2dfb913 | 78 | case STOP : bt.printf("\r\nCurrent state is stop"); break; |
aalawfi | 9:5320c2dfb913 | 79 | case RUN: bt.printf("\r\nCurrent state is RUN"); break; |
aalawfi | 9:5320c2dfb913 | 80 | case WAIT : bt.printf("\r\nCurrent state is WAIT"); break; |
aalawfi | 2:c857935f928e | 81 | }; |
aalawfi | 2:c857935f928e | 82 | switch(fault_type) { |
aalawfi | 9:5320c2dfb913 | 83 | case CLEAR : bt.printf("\r\nFault: CLEAR"); break; |
aalawfi | 9:5320c2dfb913 | 84 | case OFF_TRACK: bt.printf("\r\nFault: OFF TRACK"); break; |
quarren42 | 19:65fecaa2a387 | 85 | case COLLISION : bt.printf("\r\nFault: COLLISION"); break; |
aalawfi | 9:5320c2dfb913 | 86 | case LOW_VOLTAGE : bt.printf("\r\nFault: LOW VOLTAGE"); break; |
aalawfi | 2:c857935f928e | 87 | } |
quarren42 | 21:9e6ddb590103 | 88 | /* bt.printf("\n\n----"); |
quarren42 | 20:7dcdadbd7bc0 | 89 | bt.printf("\n\rLandmark counter : %d", counter); |
quarren42 | 20:7dcdadbd7bc0 | 90 | bt.printf("\n\rLandmark restarted : %d", restarted); |
quarren42 | 20:7dcdadbd7bc0 | 91 | bt.printf("\n\rlandmark triggered : %d", landmark_triggered); |
quarren42 | 20:7dcdadbd7bc0 | 92 | bt.printf("\n\rleft distance sensor : %1.6f", left_distance_sensor.read()); |
quarren42 | 20:7dcdadbd7bc0 | 93 | bt.printf("\n\rRight distance sensor : %1.6f", right_distance_sensor.read()*1.4); |
quarren42 | 20:7dcdadbd7bc0 | 94 | bt.printf("\n\rCurrent duty cycle : %f", current_duty_cycle); |
quarren42 | 19:65fecaa2a387 | 95 | |
quarren42 | 18:831c1e03d83e | 96 | //pot1Voltage = pot1 * 3.3f; |
quarren42 | 0:0a6756c7e3ed | 97 | //dutyCycleLeft = (pot1 * fullBatScalar); |
quarren42 | 0:0a6756c7e3ed | 98 | //dutyCycleRight = (pot1 * fullBatScalar); |
quarren42 | 0:0a6756c7e3ed | 99 | |
quarren42 | 1:c324a2849500 | 100 | //speedLeft = (speedSensorLeft * speedSensorScalar); |
quarren42 | 1:c324a2849500 | 101 | //speedRight = (speedSensorRight * speedSensorScalar); |
quarren42 | 20:7dcdadbd7bc0 | 102 | /* |
quarren42 | 1:c324a2849500 | 103 | bt.printf("Duty Cycle = %1.2f ", dutyCycleLeft); |
quarren42 | 1:c324a2849500 | 104 | bt.printf("Speed (V) L,R = %1.2f", speedLeftVolt); |
quarren42 | 1:c324a2849500 | 105 | bt.printf(", %1.2f ", speedRightVolt); |
quarren42 | 1:c324a2849500 | 106 | |
quarren42 | 1:c324a2849500 | 107 | bt.printf("Error L,R: %5.2f", errorLeft); |
quarren42 | 1:c324a2849500 | 108 | bt.printf(", %5.2f ", errorRight); |
quarren42 | 1:c324a2849500 | 109 | |
quarren42 | 1:c324a2849500 | 110 | bt.printf("ErrorArea: %1.2f ", errorAreaLeft); |
quarren42 | 18:831c1e03d83e | 111 | pc.printf("Output: %1.2f ", controllerOutputLeft); |
quarren42 | 1:c324a2849500 | 112 | |
quarren42 | 18:831c1e03d83e | 113 | pc.printf("Batt Voltage: %1.2f \n",avgCellVoltage); |
quarren42 | 20:7dcdadbd7bc0 | 114 | */ |
quarren42 | 1:c324a2849500 | 115 | //setpointLeft = pot1; |
quarren42 | 1:c324a2849500 | 116 | //setpointRight = pot1; |
quarren42 | 0:0a6756c7e3ed | 117 | |
aalawfi | 9:5320c2dfb913 | 118 | /* |
quarren42 | 1:c324a2849500 | 119 | if (t.read() > 5){ |
quarren42 | 1:c324a2849500 | 120 | setpointLeft = 0.0; |
quarren42 | 1:c324a2849500 | 121 | setpointRight = 0.0; |
quarren42 | 1:c324a2849500 | 122 | } |
quarren42 | 1:c324a2849500 | 123 | |
quarren42 | 1:c324a2849500 | 124 | if (t.read_ms() > 5100){ |
quarren42 | 1:c324a2849500 | 125 | setpointLeft = 0.2; |
quarren42 | 1:c324a2849500 | 126 | setpointRight = 0.2; |
quarren42 | 1:c324a2849500 | 127 | } |
aalawfi | 9:5320c2dfb913 | 128 | */ |
quarren42 | 0:0a6756c7e3ed | 129 | |
quarren42 | 1:c324a2849500 | 130 | if (newData){ |
quarren42 | 1:c324a2849500 | 131 | newData = false; |
quarren42 | 1:c324a2849500 | 132 | // bt.printf("Got %c %3i\n",newInputChar, newInputInt); |
quarren42 | 1:c324a2849500 | 133 | |
quarren42 | 1:c324a2849500 | 134 | if (newInputChar == 'L') |
quarren42 | 1:c324a2849500 | 135 | setpointLeft = (newInputInt / 100.0f); |
quarren42 | 1:c324a2849500 | 136 | if (newInputChar == 'R') |
quarren42 | 1:c324a2849500 | 137 | setpointRight = (newInputInt / 100.0f); |
quarren42 | 1:c324a2849500 | 138 | |
quarren42 | 0:0a6756c7e3ed | 139 | |
quarren42 | 1:c324a2849500 | 140 | //wait(0.1); |
quarren42 | 1:c324a2849500 | 141 | } |
quarren42 | 1:c324a2849500 | 142 | } |
quarren42 | 1:c324a2849500 | 143 | } |
quarren42 | 1:c324a2849500 | 144 |