robot code for summer school
Dependencies: PM2_Libary Eigen
Fork of PM2_Example_Summer_School by
Diff: Robot_Library/robot.h
- Revision:
- 49:7da71f479dac
- Child:
- 51:884fca7f02de
- Child:
- 54:b442660523df
- Child:
- 56:3fce0a9bb6df
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Robot_Library/robot.h Wed May 25 11:36:19 2022 +0200 @@ -0,0 +1,89 @@ +#ifndef ROBOT +#define ROBOT + +#include "I2C.h" +#include "SensorBar.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 + + + // TODO, pas in motors as seperate objects n stuff ? not sure + +}; + +#endif \ No newline at end of file