ESP Line Following Buggy
2nd Year Embedded System Project (Group 48 - 2023/24)
Loading...
Searching...
No Matches
main.cpp File Reference

main buggy code More...

#include "mbed.h"
#include "QEI.h"
#include "constants.h"
#include "pin_assignments.h"
#include "bluetooth.h"
#include "motor.h"
#include "PID.h"
#include "motor_driver_board.h"
#include "sensor_array.h"
Include dependency graph for main.cpp:

Go to the source code of this file.

Classes

struct  Buggy_status
 Represents the status of a buggy. More...
 

Enumerations

enum  Bt_cmd_chars {
  ch_execute = 'E' , ch_get = 'G' , ch_set = 'S' , ch_continous = 'C' ,
  ch_stop = 'S' , ch_active_stop = 'X' , ch_uturn = 'U' , ch_encoder_test = 'E' ,
  ch_motor_pwm_test = 'M' , ch_straight_test = 'Z' , ch_square_test = 'Q' , ch_PID_test = 'P' ,
  ch_toggle_led_test = 'L' , ch_line_follow = 'F' , ch_static_tracking = 'T' , ch_line_follow_auto = 'A' ,
  ch_calibrate = 'C' , ch_pwm_duty = 'D' , ch_ticks_cumulative = 'E' , ch_speed = 'S' ,
  ch_gains_PID = 'P' , ch_tau_PID = 'T' , ch_current_usage = 'C' , ch_runtime = 'R' ,
  ch_loop_time = 'X' , ch_loop_count = 'Y' , ch_motor_left = 'L' , ch_motor_right = 'R' ,
  ch_motor_both = 'B' , ch_sensor = 'S' , ch_no_obj = 'D'
}
 
enum  Buggy_modes {
  square_mode , straight_test , PID_test , line_test ,
  line_follow , task_test , task_test_inactive , inactive ,
  active_stop , uturn , static_tracking , line_follow_auto ,
  stop_detect_line , calibration
}
 

Functions

DigitalOut LED (LED_PIN)
 
Serial pc (USBTX, USBRX, 115200)
 
void stop_motors (void)
 Set pwm dc to 0 for both motors.
 
void update_buggy_status (int tick_count_left, int tick_count_right)
 Update buggy status variables.
 
void reset_everything (void)
 Reset all buggy values and all objects variables.
 
bool bt_parse_rx (char *rx_buffer)
 Parse recieved bluetooth data.
 
void control_update_ISR (void)
 ISR updating the control algorithm.
 
void serial_update_ISR (void)
 ISR to update flag to send data to pc/bt in main()
 
void stop_detect_ISR (void)
 
void bt_send_data (void)
 Send data to the bt module.
 
void pc_send_data (void)
 Send data to the pc.
 
void sensor_update_ISR ()
 
void slow_accel_ISR (void)
 
int main ()
 

Variables

volatile bool pc_serial_update = false
 
volatile bool bt_serial_update = false
 
int ISR_exec_time = 0
 
int loop_exec_time = 0
 
float bt_float_data [3] = {0}
 
short int data_log [LOG_SIZE][5] = {0}
 
int log_index = 0
 
char bt_data_sent
 
char bt_obj_sent
 
float * pid_constants
 
float * cal_constants
 
Buggy_modes buggy_mode
 
Buggy_modes prev_buggy_mode
 
Buggy_status buggy_status = {0}
 
volatile float lf_velocity = LINE_FOLLOW_VELOCITY
 
Timer global_timer
 
Ticker control_ticker
 
Ticker serial_ticker
 
Ticker sensor_ticker
 
Timeout logic_timout
 
Bluetooth bt (BT_TX_PIN, BT_RX_PIN, BT_BAUD_RATE)
 
MotorDriverBoard driver_board (DRIVER_ENABLE_PIN, DRIVER_MONITOR_PIN)
 
SensorArray sensor_array (SENSOR0_IN_PIN, SENSOR1_IN_PIN, SENSOR2_IN_PIN, SENSOR3_IN_PIN, SENSOR4_IN_PIN, SENSOR5_IN_PIN, SENSOR0_OUT_PIN, SENSOR1_OUT_PIN, SENSOR2_OUT_PIN, SENSOR3_OUT_PIN, SENSOR4_OUT_PIN, SENSOR5_OUT_PIN, SENS_SAMPLE_COUNT, SENS_DETECT_RANGE, SENS_ANGLE_COEFF)
 
Motor motor_left (MOTORL_PWM_PIN, MOTORL_DIRECTION_PIN, MOTORL_BIPOLAR_PIN, MOTORL_CHA_PIN, MOTORL_CHB_PIN, PULSE_PER_REV, MOTOR_PWM_FREQ, CONTROL_UPDATE_RATE, LP_SPEED_A0, LP_SPEED_B0, LP_SPEED_B1, WHEEL_RADIUS)
 
Motor motor_right (MOTORR_PWM_PIN, MOTORR_DIRECTION_PIN, MOTORR_BIPOLAR_PIN, MOTORR_CHA_PIN, MOTORR_CHB_PIN, PULSE_PER_REV, MOTOR_PWM_FREQ, CONTROL_UPDATE_RATE, LP_SPEED_A0, LP_SPEED_B0, LP_SPEED_B1, WHEEL_RADIUS)
 
PID PID_motor_left (PID_M_L_KP, PID_M_L_KI, PID_M_L_KD, PID_M_TAU, PID_M_MIN_OUT, PID_M_MAX_OUT, PID_M_MIN_INT, PID_M_MAX_INT, CONTROL_UPDATE_PERIOD)
 
PID PID_motor_right (PID_M_R_KP, PID_M_R_KI, PID_M_R_KD, PID_M_TAU, PID_M_MIN_OUT, PID_M_MAX_OUT, PID_M_MIN_INT, PID_M_MAX_INT, CONTROL_UPDATE_PERIOD)
 
PID PID_angle (PID_A_KP, PID_A_KI, PID_A_KD, PID_A_TAU, PID_A_MIN_OUT, PID_A_MAX_OUT, PID_A_MIN_INT, PID_A_MAX_INT, CONTROL_UPDATE_PERIOD)
 
PID PID_sensor (PID_S_KP, PID_S_KI, PID_S_KD, PID_S_TAU, PID_S_MIN_OUT, PID_S_MAX_OUT, PID_S_MIN_INT, PID_S_MAX_INT, SENSOR_UPDATE_PERIOD)
 

Detailed Description

main buggy code

This file contains all the buggy logic

Definition in file main.cpp.

Enumeration Type Documentation

◆ Bt_cmd_chars

Enumerator
ch_execute 
ch_get 
ch_set 
ch_continous 
ch_stop 
ch_active_stop 
ch_uturn 
ch_encoder_test 
ch_motor_pwm_test 
ch_straight_test 
ch_square_test 
ch_PID_test 
ch_toggle_led_test 
ch_line_follow 
ch_static_tracking 
ch_line_follow_auto 
ch_calibrate 
ch_pwm_duty 
ch_ticks_cumulative 
ch_speed 
ch_gains_PID 
ch_tau_PID 
ch_current_usage 
ch_runtime 
ch_loop_time 
ch_loop_count 
ch_motor_left 
ch_motor_right 
ch_motor_both 
ch_sensor 
ch_no_obj 

Definition at line 22 of file main.cpp.

◆ Buggy_modes

Enumerator
square_mode 
straight_test 
PID_test 
line_test 
line_follow 
task_test 
task_test_inactive 
inactive 
active_stop 
uturn 
static_tracking 
line_follow_auto 
stop_detect_line 
calibration 

Definition at line 66 of file main.cpp.

Function Documentation

◆ bt_parse_rx()

bool bt_parse_rx ( char * rx_buffer)

Parse recieved bluetooth data.

Definition at line 703 of file main.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ bt_send_data()

void bt_send_data ( void )

Send data to the bt module.

Definition at line 863 of file main.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ control_update_ISR()

void control_update_ISR ( void )

ISR updating the control algorithm.

Definition at line 530 of file main.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ LED()

DigitalOut LED ( LED_PIN )
Here is the caller graph for this function:

◆ main()

int main ( )

Definition at line 173 of file main.cpp.

Here is the call graph for this function:

◆ pc()

Serial pc ( USBTX ,
USBRX ,
115200  )
Here is the caller graph for this function:

◆ pc_send_data()

void pc_send_data ( void )

Send data to the pc.

Definition at line 958 of file main.cpp.

Here is the caller graph for this function:

◆ reset_everything()

void reset_everything ( void )

Reset all buggy values and all objects variables.

Definition at line 690 of file main.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ sensor_update_ISR()

void sensor_update_ISR ( void )

Definition at line 648 of file main.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ serial_update_ISR()

void serial_update_ISR ( void )

ISR to update flag to send data to pc/bt in main()

Definition at line 856 of file main.cpp.

Here is the caller graph for this function:

◆ slow_accel_ISR()

void slow_accel_ISR ( void )

Definition at line 655 of file main.cpp.

Here is the caller graph for this function:

◆ stop_detect_ISR()

void stop_detect_ISR ( void )

Definition at line 664 of file main.cpp.

Here is the caller graph for this function:

◆ stop_motors()

void stop_motors ( void )

Set pwm dc to 0 for both motors.

Definition at line 673 of file main.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ update_buggy_status()

void update_buggy_status ( int tick_count_left,
int tick_count_right )

Update buggy status variables.

Definition at line 680 of file main.cpp.

Here is the caller graph for this function:

Variable Documentation

◆ bt

Bluetooth bt(BT_TX_PIN, BT_RX_PIN, BT_BAUD_RATE) ( BT_TX_PIN ,
BT_RX_PIN ,
BT_BAUD_RATE  )

◆ bt_data_sent

char bt_data_sent

Definition at line 125 of file main.cpp.

◆ bt_float_data

float bt_float_data[3] = {0}

Definition at line 122 of file main.cpp.

◆ bt_obj_sent

char bt_obj_sent

Definition at line 126 of file main.cpp.

◆ bt_serial_update

volatile bool bt_serial_update = false

Definition at line 118 of file main.cpp.

◆ buggy_mode

Buggy_modes buggy_mode

Definition at line 130 of file main.cpp.

◆ buggy_status

Buggy_status buggy_status = {0}

Definition at line 132 of file main.cpp.

◆ cal_constants

float* cal_constants

Definition at line 128 of file main.cpp.

◆ control_ticker

Ticker control_ticker

Definition at line 141 of file main.cpp.

◆ data_log

short int data_log[LOG_SIZE][5] = {0}

Definition at line 123 of file main.cpp.

◆ driver_board

◆ global_timer

Timer global_timer

Definition at line 140 of file main.cpp.

◆ ISR_exec_time

int ISR_exec_time = 0

Definition at line 119 of file main.cpp.

◆ lf_velocity

volatile float lf_velocity = LINE_FOLLOW_VELOCITY

Definition at line 134 of file main.cpp.

◆ log_index

int log_index = 0

Definition at line 124 of file main.cpp.

◆ logic_timout

Timeout logic_timout

Definition at line 144 of file main.cpp.

◆ loop_exec_time

int loop_exec_time = 0

Definition at line 120 of file main.cpp.

◆ motor_left

Motor motor_left(MOTORL_PWM_PIN, MOTORL_DIRECTION_PIN, MOTORL_BIPOLAR_PIN, MOTORL_CHA_PIN, MOTORL_CHB_PIN, PULSE_PER_REV, MOTOR_PWM_FREQ, CONTROL_UPDATE_RATE, LP_SPEED_A0, LP_SPEED_B0, LP_SPEED_B1, WHEEL_RADIUS) ( MOTORL_PWM_PIN ,
MOTORL_DIRECTION_PIN ,
MOTORL_BIPOLAR_PIN ,
MOTORL_CHA_PIN ,
MOTORL_CHB_PIN ,
PULSE_PER_REV ,
MOTOR_PWM_FREQ ,
CONTROL_UPDATE_RATE ,
LP_SPEED_A0 ,
LP_SPEED_B0 ,
LP_SPEED_B1 ,
WHEEL_RADIUS  )

◆ motor_right

Motor motor_right(MOTORR_PWM_PIN, MOTORR_DIRECTION_PIN, MOTORR_BIPOLAR_PIN, MOTORR_CHA_PIN, MOTORR_CHB_PIN, PULSE_PER_REV, MOTOR_PWM_FREQ, CONTROL_UPDATE_RATE, LP_SPEED_A0, LP_SPEED_B0, LP_SPEED_B1, WHEEL_RADIUS) ( MOTORR_PWM_PIN ,
MOTORR_DIRECTION_PIN ,
MOTORR_BIPOLAR_PIN ,
MOTORR_CHA_PIN ,
MOTORR_CHB_PIN ,
PULSE_PER_REV ,
MOTOR_PWM_FREQ ,
CONTROL_UPDATE_RATE ,
LP_SPEED_A0 ,
LP_SPEED_B0 ,
LP_SPEED_B1 ,
WHEEL_RADIUS  )

◆ pc_serial_update

volatile bool pc_serial_update = false

Definition at line 117 of file main.cpp.

◆ PID_angle

◆ pid_constants

float* pid_constants

Definition at line 127 of file main.cpp.

◆ PID_motor_left

◆ PID_motor_right

◆ PID_sensor

◆ prev_buggy_mode

Buggy_modes prev_buggy_mode

Definition at line 131 of file main.cpp.

◆ sensor_array

◆ sensor_ticker

Ticker sensor_ticker

Definition at line 143 of file main.cpp.

◆ serial_ticker

Ticker serial_ticker

Definition at line 142 of file main.cpp.