first commit

Dependencies:   mbed MMA8451Q

Committer:
quarren42
Date:
Fri Nov 19 22:53:54 2021 +0000
Revision:
30:ab358e8a9e6a
Parent:
29:17bec543a437
Child:
31:d570f957e083
go zoom zoom on straightaway

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 30:ab358e8a9e6a 22
quarren42 18:831c1e03d83e 23
quarren42 19:65fecaa2a387 24
aalawfi 3:25c6bf0abc00 25 int main() {
aalawfi 26:54ce9f642477 26
aalawfi 2:c857935f928e 27 state_update();
aalawfi 26:54ce9f642477 28 wait(2.5);
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 14:eb9c58c0f8cd 40 motorLoop.attach(&PI,TI);
aalawfi 26:54ce9f642477 41 steering_control_ticker.attach(&steering_control, TI_STEERING);
aalawfi 26:54ce9f642477 42 fault_detector.attach(&fault_check, 0.008);
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");
quarren42 30:ab358e8a9e6a 57 //Timer t;
quarren42 30:ab358e8a9e6a 58 //t.start();
quarren42 30:ab358e8a9e6a 59
quarren42 0:0a6756c7e3ed 60 while(1) {
aalawfi 25:8bd029d58251 61 wait(0.2);
aalawfi 2:c857935f928e 62 pc.printf("\n\n");
quarren42 17:d2c98ebda90b 63 /*
aalawfi 9:5320c2dfb913 64 bt.printf("\n\n");
aalawfi 9:5320c2dfb913 65 bt.printf("\n\rMotor enabled ? : %d ", motor_enabled );
aalawfi 9:5320c2dfb913 66 bt.printf("\n\rSteering enabled ? : %d ", steering_enabled );
aalawfi 9:5320c2dfb913 67 bt.printf("\n\rCurrent duty cycle : %f", current_duty_cycle);
aalawfi 13:0091da3021df 68 bt.printf("\n\Brake enabled ? : %d ", are_brakes_activated );
aalawfi 9:5320c2dfb913 69 bt.printf("\n\rCurrent duty cycle? : %f ", current_duty_cycle );
quarren42 17:d2c98ebda90b 70 */
quarren42 6:d2bd68ba99c9 71 //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 72 //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 73 switch(current_state){
aalawfi 9:5320c2dfb913 74 case STOP : bt.printf("\r\nCurrent state is stop"); break;
aalawfi 9:5320c2dfb913 75 case RUN: bt.printf("\r\nCurrent state is RUN"); break;
aalawfi 9:5320c2dfb913 76 case WAIT : bt.printf("\r\nCurrent state is WAIT"); break;
aalawfi 2:c857935f928e 77 };
aalawfi 2:c857935f928e 78 switch(fault_type) {
aalawfi 9:5320c2dfb913 79 case CLEAR : bt.printf("\r\nFault: CLEAR"); break;
aalawfi 9:5320c2dfb913 80 case OFF_TRACK: bt.printf("\r\nFault: OFF TRACK"); break;
quarren42 19:65fecaa2a387 81 case COLLISION : bt.printf("\r\nFault: COLLISION"); break;
aalawfi 9:5320c2dfb913 82 case LOW_VOLTAGE : bt.printf("\r\nFault: LOW VOLTAGE"); break;
aalawfi 2:c857935f928e 83 }
aalawfi 29:17bec543a437 84
aalawfi 27:0fa9d61c5fc6 85 // bt.printf("\n\n----");
aalawfi 25:8bd029d58251 86 // bt.printf("%d", t.read_ms());
aalawfi 27:0fa9d61c5fc6 87 // bt.printf("\n\rLeft distance sens (ADC) : %1.5f", left_sens);
aalawfi 27:0fa9d61c5fc6 88 // bt.printf("\n\rRight distance sens (ADC): %1.5f",right_sens);
aalawfi 27:0fa9d61c5fc6 89 // bt.printf("\n\rDifference (ADC): %1.5f",left_sens - right_sens);
aalawfi 27:0fa9d61c5fc6 90 bt.printf("\n%1.5f", feedback);
aalawfi 27:0fa9d61c5fc6 91 // bt.printf("\n\rLap: %d", lap);
aalawfi 25:8bd029d58251 92 // bt.printf("\n\ravgCellVolt;ag : %1.5f", avgCellVoltag);
quarren42 18:831c1e03d83e 93 //pot1Voltage = pot1 * 3.3f;
quarren42 0:0a6756c7e3ed 94 //dutyCycleLeft = (pot1 * fullBatScalar);
quarren42 0:0a6756c7e3ed 95 //dutyCycleRight = (pot1 * fullBatScalar);
quarren42 0:0a6756c7e3ed 96
quarren42 1:c324a2849500 97 //speedLeft = (speedSensorLeft * speedSensorScalar);
quarren42 1:c324a2849500 98 //speedRight = (speedSensorRight * speedSensorScalar);
quarren42 20:7dcdadbd7bc0 99 /*
quarren42 1:c324a2849500 100 bt.printf("Duty Cycle = %1.2f ", dutyCycleLeft);
quarren42 1:c324a2849500 101 bt.printf("Speed (V) L,R = %1.2f", speedLeftVolt);
quarren42 1:c324a2849500 102 bt.printf(", %1.2f ", speedRightVolt);
quarren42 1:c324a2849500 103
quarren42 1:c324a2849500 104 bt.printf("Error L,R: %5.2f", errorLeft);
quarren42 1:c324a2849500 105 bt.printf(", %5.2f ", errorRight);
quarren42 1:c324a2849500 106
quarren42 1:c324a2849500 107 bt.printf("ErrorArea: %1.2f ", errorAreaLeft);
quarren42 18:831c1e03d83e 108 pc.printf("Output: %1.2f ", controllerOutputLeft);
quarren42 1:c324a2849500 109
quarren42 18:831c1e03d83e 110 pc.printf("Batt Voltage: %1.2f \n",avgCellVoltage);
quarren42 20:7dcdadbd7bc0 111 */
quarren42 1:c324a2849500 112 //setpointLeft = pot1;
quarren42 1:c324a2849500 113 //setpointRight = pot1;
quarren42 0:0a6756c7e3ed 114
aalawfi 9:5320c2dfb913 115 /*
quarren42 1:c324a2849500 116 if (t.read() > 5){
quarren42 1:c324a2849500 117 setpointLeft = 0.0;
quarren42 1:c324a2849500 118 setpointRight = 0.0;
quarren42 1:c324a2849500 119 }
quarren42 1:c324a2849500 120
quarren42 1:c324a2849500 121 if (t.read_ms() > 5100){
quarren42 1:c324a2849500 122 setpointLeft = 0.2;
quarren42 1:c324a2849500 123 setpointRight = 0.2;
quarren42 1:c324a2849500 124 }
aalawfi 9:5320c2dfb913 125 */
quarren42 0:0a6756c7e3ed 126
quarren42 1:c324a2849500 127 if (newData){
quarren42 1:c324a2849500 128 newData = false;
quarren42 1:c324a2849500 129 // bt.printf("Got %c %3i\n",newInputChar, newInputInt);
quarren42 1:c324a2849500 130
quarren42 1:c324a2849500 131 if (newInputChar == 'L')
quarren42 1:c324a2849500 132 setpointLeft = (newInputInt / 100.0f);
quarren42 1:c324a2849500 133 if (newInputChar == 'R')
quarren42 1:c324a2849500 134 setpointRight = (newInputInt / 100.0f);
quarren42 1:c324a2849500 135
quarren42 0:0a6756c7e3ed 136
quarren42 1:c324a2849500 137 //wait(0.1);
quarren42 1:c324a2849500 138 }
quarren42 1:c324a2849500 139 }
quarren42 1:c324a2849500 140 }
quarren42 1:c324a2849500 141