first commit

Dependencies:   mbed MMA8451Q

Committer:
aalawfi
Date:
Fri Nov 19 20:27:56 2021 +0000
Revision:
29:17bec543a437
Parent:
27:0fa9d61c5fc6
Child:
30:ab358e8a9e6a
-v

Who changed what in which revision?

UserRevisionLine numberNew 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"
aalawfi 26:54ce9f642477 4 #include "steering_methods.h"
quarren42 1:c324a2849500 5 #include "driving.h"
aalawfi 26:54ce9f642477 6
aalawfi 2:c857935f928e 7 #include "state_control.h"
quarren42 19:65fecaa2a387 8
quarren42 0:0a6756c7e3ed 9 #include <iostream>
aalawfi 3:25c6bf0abc00 10 #include "bluetooth.h"
quarren42 19:65fecaa2a387 11
quarren42 1:c324a2849500 12
quarren42 0:0a6756c7e3ed 13 Serial pc(USBTX, USBRX); // tx, rx
aalawfi 3:25c6bf0abc00 14
quarren42 0:0a6756c7e3ed 15
quarren42 1:c324a2849500 16
quarren42 1:c324a2849500 17 Ticker motorLoop;
aalawfi 7:05ea1c004b49 18 Ticker fault_detector;
aalawfi 9:5320c2dfb913 19 Ticker controlUpdate;
quarren42 1:c324a2849500 20
aalawfi 26:54ce9f642477 21
quarren42 18:831c1e03d83e 22
quarren42 19:65fecaa2a387 23
aalawfi 3:25c6bf0abc00 24 int main() {
aalawfi 26:54ce9f642477 25
aalawfi 2:c857935f928e 26 state_update();
aalawfi 26:54ce9f642477 27 wait(2.5);
aalawfi 2:c857935f928e 28 //Delcare Onboard LED with blue color
aalawfi 2:c857935f928e 29 DigitalOut led_b(LED_BLUE);
aalawfi 2:c857935f928e 30 //Set the period of the servo motor control signal
aalawfi 2:c857935f928e 31 servo_motor_pwm.period(1/SERVO_MOTOR_FREQ);
quarren42 6:d2bd68ba99c9 32 motorLeft.period(1/freq);
quarren42 6:d2bd68ba99c9 33 motorRight.period(1/freq);
aalawfi 14:eb9c58c0f8cd 34 //controlUpdate.attach(&PID, TI);
aalawfi 2:c857935f928e 35 //Center the servo motor
aalawfi 14:eb9c58c0f8cd 36 servo_motor_pwm.write(CENTER_DUTY_CYCLE);
quarren42 20:7dcdadbd7bc0 37 // controlUpdate.attach(&PID, TI);
aalawfi 2:c857935f928e 38 //Start the control systm using a Ticker object
aalawfi 14:eb9c58c0f8cd 39 motorLoop.attach(&PI,TI);
aalawfi 26:54ce9f642477 40 steering_control_ticker.attach(&steering_control, TI_STEERING);
aalawfi 26:54ce9f642477 41 fault_detector.attach(&fault_check, 0.008);
quarren42 6:d2bd68ba99c9 42 //bt.attach(&btReceive);
aalawfi 2:c857935f928e 43
aalawfi 2:c857935f928e 44 // call landmark_counter wjen a landmark is detected
aalawfi 2:c857935f928e 45 left_landmark_sensor.rise(&landmark_counter);
aalawfi 2:c857935f928e 46 right_landmark_sensor.rise(&landmark_counter);
aalawfi 2:c857935f928e 47 // update status when the button is pressed
aalawfi 2:c857935f928e 48 stop_button.rise(&state_update);
aalawfi 2:c857935f928e 49 run_button.rise(&state_update);
aalawfi 2:c857935f928e 50 wait_button.rise(&state_update);
aalawfi 2:c857935f928e 51
quarren42 0:0a6756c7e3ed 52 bt.baud(BLUETOOTHBAUDRATE);
quarren42 0:0a6756c7e3ed 53 //Sets the communication rate of the micro-controller to the Bluetooth module.
quarren42 0:0a6756c7e3ed 54 pc.printf("Hello World!\n");
quarren42 1:c324a2849500 55 bt.printf("Hello World!\n");
aalawfi 25:8bd029d58251 56 Timer t;
aalawfi 25:8bd029d58251 57 t.start();
quarren42 0:0a6756c7e3ed 58 while(1) {
aalawfi 25:8bd029d58251 59 wait(0.2);
aalawfi 2:c857935f928e 60 pc.printf("\n\n");
quarren42 17:d2c98ebda90b 61 /*
aalawfi 9:5320c2dfb913 62 bt.printf("\n\n");
aalawfi 9:5320c2dfb913 63 bt.printf("\n\rMotor enabled ? : %d ", motor_enabled );
aalawfi 9:5320c2dfb913 64 bt.printf("\n\rSteering enabled ? : %d ", steering_enabled );
aalawfi 9:5320c2dfb913 65 bt.printf("\n\rCurrent duty cycle : %f", current_duty_cycle);
aalawfi 13:0091da3021df 66 bt.printf("\n\Brake enabled ? : %d ", are_brakes_activated );
aalawfi 9:5320c2dfb913 67 bt.printf("\n\rCurrent duty cycle? : %f ", current_duty_cycle );
quarren42 17:d2c98ebda90b 68 */
quarren42 6:d2bd68ba99c9 69 //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 70 //the driving input ticker is faster than the analog.read() function, so all analog.read() methods must be in the main loop
aalawfi 29:17bec543a437 71 switch(current_state){
aalawfi 9:5320c2dfb913 72 case STOP : bt.printf("\r\nCurrent state is stop"); break;
aalawfi 9:5320c2dfb913 73 case RUN: bt.printf("\r\nCurrent state is RUN"); break;
aalawfi 9:5320c2dfb913 74 case WAIT : bt.printf("\r\nCurrent state is WAIT"); break;
aalawfi 2:c857935f928e 75 };
aalawfi 2:c857935f928e 76 switch(fault_type) {
aalawfi 9:5320c2dfb913 77 case CLEAR : bt.printf("\r\nFault: CLEAR"); break;
aalawfi 9:5320c2dfb913 78 case OFF_TRACK: bt.printf("\r\nFault: OFF TRACK"); break;
quarren42 19:65fecaa2a387 79 case COLLISION : bt.printf("\r\nFault: COLLISION"); break;
aalawfi 9:5320c2dfb913 80 case LOW_VOLTAGE : bt.printf("\r\nFault: LOW VOLTAGE"); break;
aalawfi 2:c857935f928e 81 }
aalawfi 29:17bec543a437 82
aalawfi 27:0fa9d61c5fc6 83 // bt.printf("\n\n----");
aalawfi 25:8bd029d58251 84 // bt.printf("%d", t.read_ms());
aalawfi 27:0fa9d61c5fc6 85 // bt.printf("\n\rLeft distance sens (ADC) : %1.5f", left_sens);
aalawfi 27:0fa9d61c5fc6 86 // bt.printf("\n\rRight distance sens (ADC): %1.5f",right_sens);
aalawfi 27:0fa9d61c5fc6 87 // bt.printf("\n\rDifference (ADC): %1.5f",left_sens - right_sens);
aalawfi 27:0fa9d61c5fc6 88 bt.printf("\n%1.5f", feedback);
aalawfi 27:0fa9d61c5fc6 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