robot code for summer school

Dependencies:   PM2_Libary Eigen

Fork of PM2_Example_Summer_School by Alex Hawkins

Robot_Library/robot.h

Committer:
eversonrosed
Date:
2022-05-25
Branch:
a-star
Revision:
51:884fca7f02de
Parent:
49:7da71f479dac
Child:
61:9b1fc2bc7172

File content as of revision 51:884fca7f02de:

#ifndef ROBOT
#define ROBOT

#include "mbed.h"
#include "I2C.h"
#include "SensorBar.h"
#include "SDFileSystem.h"
#include "controller.h"


class Robot {

    public:
        Robot();
        void Update();

    private:
        Controller controller; // a reference to the robots controller
        
        enum MovementStates {
            INITIAL, 
            IDLE, 
            FOLLOWING_LINE,
            RIGHT_TURN_90,
            LEFT_TURN_90
        };
        
        MovementStates state;

        // Finite state machine functions
        void Initial();
        void Idle();
        void FollowingLine();
        void LeftTurn();
        void RightTurn();

        //bool motors_enabled;


        /* CONSTANTS */

        const float DISTANCE_THRESHOLD = 0.2f;        // minimum allowed distance to obstacle in [m]
        const float TRANSLATIONAL_VELOCITY = 0.4f;    // translational velocity in [m/s]
        const float ROTATIONAL_VELOCITY = 1.6f;       // rotational velocity in [rad/s]
        const float VELOCITY_THRESHOLD = 0.05;        // velocity threshold before switching off, in [m/s] and [rad/s]

        const float WHEEL_RADIUS = 1.0f;
        const float DISTANCE_BETWEEN_WHEELS = 1.0f;

        const float MAX_MOTOR_VOLTAGE = 12.0f; // define maximum motor voltage
        const float COUNTS_PER_TURN = 64.0f * 19.0f;    // define counts per turn at gearbox end: counts/turn * gearratio
        const float KN = 530.0f / 12.0f;      

        Eigen::Matrix2f wheel_to_robot;// Transoformation matrix
        Eigen::Matrix2f robot_to_wheel;
        /* HARDWARE SETUP */

        //--- SENSORS ---//
        AnalogIn dist; // data in from the IR sensor TODO: UNDERSTAND THIS!
        DigitalOut bit0;
        DigitalOut bit1;
        DigitalOut bit2;
        IRSensor ir_sensor_0;  // one IR sensor

        I2C i2c2; // not sure what this does
        SensorBar line_sensor; // Something about using raw value only


        /// MOTORS + MOTION //


        FastPWM pwm_M1;  // motor M1 is closed-loop speed controlled (angle velocity)
        FastPWM pwm_M2;  // motor M2 is closed-loop speed controlled (angle velocity)
        EncoderCounter encoder_M1; // create encoder objects to read in the encoder counter values
        EncoderCounter encoder_M2;

        Motion* trajectoryPlanners[2]; // two directory planners, 1 for each motor
        SpeedController* speedControllers[2];

        Eigen::Vector2f robot_speed_desired;    // Robot speed vector [x_dt, alpha_dt] in [m/s] and [rad/s]
        Eigen::Vector2f wheel_speed_desired;    // Wheel speeds [w_R, w_L] in [rad/s]
        Eigen::Vector2f wheel_speed_smooth;     // Wheel speeds limited and smoothed
        Eigen::Vector2f wheel_speed_actual;     // Measured wheel speeds
        Eigen::Vector2f robot_speed_actual;     // Measured robot speed

        // data storage

        SDFileSystem sd;
        bool sd_on;

 // TODO, pas in motors as seperate objects n stuff ? not sure

};

#endif