first commit

Dependencies:   mbed MMA8451Q

Committer:
aalawfi
Date:
Tue Oct 26 14:07:48 2021 +0000
Revision:
13:0091da3021df
Parent:
9:5320c2dfb913
Child:
14:eb9c58c0f8cd
- added Boolean to check if the brakes are on or off

Who changed what in which revision?

UserRevisionLine numberNew contents of line
quarren42 0:0a6756c7e3ed 1 #include "mbed.h"
quarren42 1:c324a2849500 2 #include "driving.h"
aalawfi 2:c857935f928e 3 #include "steering_header.h"
aalawfi 2:c857935f928e 4 #include "steering_methods.h"
aalawfi 2:c857935f928e 5 #include "state_control.h"
quarren42 1:c324a2849500 6 #include "MMA8451Q.h"
quarren42 0:0a6756c7e3ed 7 #include <iostream>
aalawfi 3:25c6bf0abc00 8 #include "bluetooth.h"
quarren42 1:c324a2849500 9 #define MMA8451_I2C_ADDRESS (0x1d<<1)
quarren42 1:c324a2849500 10
quarren42 0:0a6756c7e3ed 11 Serial pc(USBTX, USBRX); // tx, rx
aalawfi 3:25c6bf0abc00 12
quarren42 0:0a6756c7e3ed 13
quarren42 1:c324a2849500 14 PinName const SDA = PTE25;
quarren42 1:c324a2849500 15 PinName const SCL = PTE24;
quarren42 1:c324a2849500 16
quarren42 1:c324a2849500 17 MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS);
quarren42 1:c324a2849500 18 Ticker motorLoop;
aalawfi 7:05ea1c004b49 19 Ticker fault_detector;
quarren42 1:c324a2849500 20 Timer t;
aalawfi 9:5320c2dfb913 21 Ticker controlUpdate;
quarren42 1:c324a2849500 22
aalawfi 9:5320c2dfb913 23 void PID (void) {
aalawfi 9:5320c2dfb913 24 steering_control();
aalawfi 9:5320c2dfb913 25 PI();
aalawfi 9:5320c2dfb913 26 };
aalawfi 3:25c6bf0abc00 27 int main() {
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 9:5320c2dfb913 35 controlUpdate.attach(&PID, TI);
aalawfi 2:c857935f928e 36 //Center the servo motor
aalawfi 9:5320c2dfb913 37 //servo_motor_pwm.write(CENTER_DUTY_CYCLE);
aalawfi 9:5320c2dfb913 38 //controlUpdate.attach(&PID, TI);
aalawfi 2:c857935f928e 39 //Start the control systm using a Ticker object
aalawfi 9:5320c2dfb913 40 // steering_control_ticker.attach(&steering_control, TI);
aalawfi 9:5320c2dfb913 41 // motorLoop.attach(&PI,TI);
aalawfi 9:5320c2dfb913 42 // fault_detector.attach(&fault_check, 0.02);
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 1:c324a2849500 57
quarren42 1:c324a2849500 58 t.start();
quarren42 1:c324a2849500 59 float time = t.read();
quarren42 1:c324a2849500 60
quarren42 0:0a6756c7e3ed 61 //prints the string to the Tera-Term Console using the Bluetooth object ‘bt’.
quarren42 0:0a6756c7e3ed 62 while(1) {
aalawfi 2:c857935f928e 63
aalawfi 2:c857935f928e 64 pc.printf("\n\n");
aalawfi 2:c857935f928e 65
aalawfi 9:5320c2dfb913 66
aalawfi 9:5320c2dfb913 67 bt.printf("\n\n");
aalawfi 9:5320c2dfb913 68 bt.printf("\n\rMotor enabled ? : %d ", motor_enabled );
aalawfi 9:5320c2dfb913 69 bt.printf("\n\rSteering enabled ? : %d ", steering_enabled );
aalawfi 9:5320c2dfb913 70 bt.printf("\n\rCurrent duty cycle : %f", current_duty_cycle);
aalawfi 13:0091da3021df 71 bt.printf("\n\Brake enabled ? : %d ", are_brakes_activated );
aalawfi 9:5320c2dfb913 72 bt.printf("\n\rCurrent duty cycle? : %f ", current_duty_cycle );
quarren42 6:d2bd68ba99c9 73 //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 74 //the driving input ticker is faster than the analog.read() function, so all analog.read() methods must be in the main loop
aalawfi 2:c857935f928e 75 switch(current_state ){
aalawfi 9:5320c2dfb913 76 case STOP : bt.printf("\r\nCurrent state is stop"); break;
aalawfi 9:5320c2dfb913 77 case RUN: bt.printf("\r\nCurrent state is RUN"); break;
aalawfi 9:5320c2dfb913 78 case WAIT : bt.printf("\r\nCurrent state is WAIT"); break;
aalawfi 2:c857935f928e 79 };
aalawfi 2:c857935f928e 80 switch(fault_type) {
aalawfi 9:5320c2dfb913 81 case CLEAR : bt.printf("\r\nFault: CLEAR"); break;
aalawfi 9:5320c2dfb913 82 case OFF_TRACK: bt.printf("\r\nFault: OFF TRACK"); break;
aalawfi 9:5320c2dfb913 83 case BUMPER : bt.printf("\r\nFault: OBSTACLE"); break;
aalawfi 9:5320c2dfb913 84 case LOW_VOLTAGE : bt.printf("\r\nFault: LOW VOLTAGE"); break;
aalawfi 2:c857935f928e 85 }
aalawfi 2:c857935f928e 86
quarren42 1:c324a2849500 87 x = abs(acc.getAccX());
quarren42 0:0a6756c7e3ed 88 pot1Voltage = pot1 * 3.3f;
quarren42 0:0a6756c7e3ed 89 //dutyCycleLeft = (pot1 * fullBatScalar);
quarren42 0:0a6756c7e3ed 90 //dutyCycleRight = (pot1 * fullBatScalar);
quarren42 0:0a6756c7e3ed 91
quarren42 1:c324a2849500 92 //speedLeft = (speedSensorLeft * speedSensorScalar);
quarren42 1:c324a2849500 93 //speedRight = (speedSensorRight * speedSensorScalar);
quarren42 1:c324a2849500 94
quarren42 1:c324a2849500 95 speedLeftVolt = (speedSensorLeft * 3.3f);
quarren42 1:c324a2849500 96 speedRightVolt = (speedSensorRight * 3.3f);
aalawfi 9:5320c2dfb913 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 1:c324a2849500 106 bt.printf("Output: %1.2f ", controllerOutputLeft);
quarren42 1:c324a2849500 107
quarren42 1:c324a2849500 108 bt.printf("Batt Voltage: %1.2f \n",avgCellVoltage);
aalawfi 9:5320c2dfb913 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