car using PID from centre line

Dependencies:   FRDM-TFC mbed CBuffer XBEE mbed_angular_speed motor2 MMA8451Q

Fork of KL25Z_Camera_Test by GDP 4

main.h

Committer:
FatCookies
Date:
2016-12-15
Revision:
28:613239f10ba4
Parent:
27:627d67e3b9b0
Child:
38:72a174cccd43

File content as of revision 28:613239f10ba4:

#define BAUD_RATE 57600
#define CAM_THRESHOLD 100
#define CAM_DIFF 10
#define WHEEL_RADIUS 0.025f
#define RIGHT_MOTOR_COMPENSATION_RATIO 1.1586276
#define USE_COMMS 1

#define CLOSE_CAMERA TFC_LineScanImage0
#define LOOKAHEAD_CAMERA TFC_LineScanImage1


#define ALIGN_SERVO TFC_InitServos(0.00052,0.00122,0.02)

typedef struct {
    float Kp;
    float Ki;
    float Kd;
    float dt;
    float p_error;
    float pid_error;
    float integral;
        
    //Measured value is a float between -1.0 and 1.0 (from left to right)
    float measured_value;
    
    //Desired value is always 0.0 (as in, car is in the middle of the road)
    float desired_value;
    float derivative;
    float output;
} pid_instance;

extern inline void handleComms();
extern void sendString(const char *format, ...);
extern inline void sendImage();
extern inline void sendSpeeds();
extern void sendBattery();

extern void initVariables();
extern void initPID(pid_instance* pid, float Kp, float Ki, float Kd);
extern inline float findCentreValue(volatile uint16_t * cam_data, uint8_t &left, uint8_t &right);
extern inline void PIDController();
extern void handlePID(pid_instance *pid);
extern inline void handleStartStop();
extern inline void handleCornering();
extern inline float getLineEntropy();
extern inline void initSpeedSensors();


//Woo global variables!
bool onTrack;
char curr_line[128];
uint8_t curr_left;
uint8_t curr_right;
uint8_t right;
uint8_t left;
uint8_t farRight;
uint8_t farLeft;
int diff;
int prev;
int i = 0;
float measuredValBuffer[64];
uint8_t valBufferIndex;

//Communication Variables
uint8_t sendCam = 0;
int frame_counter = 0;
char curr_cmd = 0; // Current comms command
union {
    float _float;
    unsigned char bytes[4];
} byte_float_union;

//Some PID variables
pid_instance servo_pid;
pid_instance right_motor_pid;
pid_instance left_motor_pid;

// Maximum car speed (PWM)
float speed;

//Hacky start/stop signal detection
int startstop;
bool seen;

// Speed sensors
volatile float Time_L,Time_R;
float wL, wR;
float prevL, prevR;
void GetTime_L();
void GetTime_R();

// Turning detection
int turning;
int keepTurning;
bool slow;


//testing functions for the car
int lapTime();
float testSpeed(float speed);
void endTest();

float oldTime;
int lapNo;