- This code combines steering and driving in one ticker - Fault check is in a ticker instead of while loop

Dependencies:   mbed MMA8451Q

main.cpp

Committer:
aalawfi
Date:
2021-10-25
Revision:
9:5f032ca6c9b5
Parent:
8:e4a147850ba4

File content as of revision 9:5f032ca6c9b5:

#include "mbed.h"
#include "driving.h"
#include "steering_header.h"
#include "steering_methods.h"
#include "state_control.h"
#include "MMA8451Q.h"
#include <iostream>
#include "bluetooth.h"
#define MMA8451_I2C_ADDRESS (0x1d<<1)

Serial pc(USBTX, USBRX); // tx, rx


PinName const SDA = PTE25;
PinName const SCL = PTE24;

MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS);
Ticker motorLoop;
Ticker fault_detector; 
Ticker controlUpdate;
Timer t;
void PID (void) {
    PI();
    steering_control();
    };
int main() { 
    state_update();
    controlUpdate.attach(&PID, TI);
    fault_detector.attach(&fault_check, 0.02);
    //Delcare Onboard LED with blue color
    DigitalOut led_b(LED_BLUE);
    //Set the period of the servo motor control signal
    servo_motor_pwm.period(1/SERVO_MOTOR_FREQ);
    motorLeft.period(1/freq);
    motorRight.period(1/freq);
    //Center the servo motor
    servo_motor_pwm.write(CENTER_DUTY_CYCLE);

    //Start the control systm using a Ticker object
     // steering_control_ticker.attach(&steering_control, TI);
    //   motorLoop.attach(&PI,TI);
     
      //bt.attach(&btReceive);
    
    // call landmark_counter wjen a landmark is detected 
    left_landmark_sensor.rise(&landmark_counter);
    right_landmark_sensor.rise(&landmark_counter);
    // update status when the button is pressed
    stop_button.rise(&state_update);
    run_button.rise(&state_update);
    wait_button.rise(&state_update);
 
 bt.baud(BLUETOOTHBAUDRATE);
 //Sets the communication rate of the micro-controller to the Bluetooth module.
pc.printf("Hello World!\n");
bt.printf("Hello World!\n");

t.start();
float time = t.read();

 //prints the string to the Tera-Term Console using the Bluetooth object ‘bt’.
 while(1) {
     
     pc.printf("\n\n");

     pc.printf("\r\nSteering enabled? : %d",steering_enabled);
     pc.printf("\r\nCurrent duty cycle : %f ", current_duty_cycle);
     pc.printf("\r\nLeft duty cycle : %f ", left_distance_sensor.read());
     pc.printf("\r\nRight voltage : %f"      , right_distance_sensor.read());
     pc.printf("\r\nCurrent duty cycle : %f ", current_duty_cycle);
       fault_check();
      //wait(0.5); //commented out the wait bc it slows down the fault_check, and it breaks the analogIn readings for the driving input
                    //the driving input ticker is faster than the analog.read() function, so all analog.read() methods must be in the main loop
     switch(current_state ){
         case STOP :    pc.printf("\r\nCurrent state is stop"); break; 
         case RUN:      pc.printf("\r\nCurrent state is RUN"); break; 
         case WAIT :    pc.printf("\r\nCurrent state is WAIT"); break; 
         };
     switch(fault_type) {
         case CLEAR :       pc.printf("\r\nFault: CLEAR"); break; 
         case OFF_TRACK:    pc.printf("\r\nFault: OFF TRACK"); break;
         case BUMPER :      pc.printf("\r\nFault: OBSTACLE"); break;
         case LOW_VOLTAGE : pc.printf("Fault: LOW VOLTAGE");  break;
          }
          
x = abs(acc.getAccX());
pot1Voltage = pot1 * 3.3f;
//dutyCycleLeft = (pot1 * fullBatScalar);
//dutyCycleRight = (pot1 * fullBatScalar);

//speedLeft = (speedSensorLeft * speedSensorScalar);
//speedRight = (speedSensorRight * speedSensorScalar);

speedLeftVolt = (speedSensorLeft * 3.3f);
speedRightVolt = (speedSensorRight * 3.3f);

bt.printf("Duty Cycle = %1.2f     ", dutyCycleLeft);
bt.printf("Speed (V) L,R = %1.2f", speedLeftVolt);
bt.printf(", %1.2f     ", speedRightVolt);

bt.printf("Error L,R: %5.2f", errorLeft);
bt.printf(", %5.2f     ", errorRight);

bt.printf("ErrorArea: %1.2f     ", errorAreaLeft);
bt.printf("Output: %1.2f     ", controllerOutputLeft);

bt.printf("Batt Voltage: %1.2f \n",avgCellVoltage);

//setpointLeft = pot1;
//setpointRight = pot1;


if (t.read() > 5){
    setpointLeft = 0.0;
    setpointRight = 0.0;
    }
    
    if (t.read_ms() > 5100){
        setpointLeft = 0.2;
        setpointRight = 0.2;
        }

 if (newData){  
      newData = false;
  //    bt.printf("Got %c %3i\n",newInputChar, newInputInt);
      
      if (newInputChar == 'L')
      setpointLeft = (newInputInt / 100.0f);
      if (newInputChar == 'R')
      setpointRight = (newInputInt / 100.0f);
      

//wait(0.1);
        }
    }
}