Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
robot.h
00001 #ifndef ROBOT_H 00002 #define ROBOT_H 00003 00004 #include "mbed.h" 00005 #include "MPU6050.h" 00006 #include "nRF24L01P.h" 00007 00008 #define BaudRate_bt 9600 //Baud rate of 9600 00009 #define tx_bt PTA2 //Bluetooth connection pins 00010 #define rx_bt PTA1 //Bluetooth connection pins 00011 #define tx_mpu PTE0 //MPU connection pins 00012 #define rx_mpu PTE1 //MPU connection pins 00013 #define mpu_bandwidth MPU6050_BW_20 //set the MPU low pass filter bandwidth to 20hz 00014 00015 #define LED PTE3 //status LED pin 00016 #define CURRENTSENSOR_PIN PTC2 00017 #define VOLTAGESENSOR_PIN PTB0 00018 00019 #define CURRENT_R1 180 //160.0 //values of the current sensor opamp resistors 00020 #define CURRENT_R2 10 00021 #define CURRENT_R3 80 00022 #define CURRENT_R4 84.7 00023 #define VREF3_3 3.3 //digital logic voltage 00024 #define VREF5 5.0 //5v voltage //NOTE: 5v voltage is consistent when using new batts, but not using old blue batts 00025 00026 #define irL PTB1 00027 #define irC PTB3 00028 #define irR PTB2 00029 00030 #define MOT_PWMA_PIN PTA4 //Motor control pins 00031 #define MOT_PWMB_PIN PTA5 00032 #define MOT_STBY_PIN PTA15 00033 #define MOT_AIN1_PIN PTA14 00034 #define MOT_AIN2_PIN PTA13 00035 #define MOT_BIN1_PIN PTA16 00036 #define MOT_BIN2_PIN PTA17 00037 00038 #define M_PI 3.14159265359 //PI 00039 #define gyroCorrect 3720 //divide raw gyro data by this to get result in RAD/SECOND (if gyroRange is 500 rad/s) 00040 00041 //Correct direction of motors. If number = 1, forward. If number = 0, backwards (for if motors are wired backwards) 00042 #define MOTOR_R_DIRECTION 1 00043 #define MOTOR_L_DIRECTION 1 00044 00045 #define MOTOR_INTERVAL 20 //defines the interval (in milliseconds) between when motor can be set 00046 //NOTE: Can't make this less than 20ms, because that is the PWM frequency 00047 00048 //Key bindings for remote control robot - for the future try to use arrow keys instead of 'asdw' 00049 #define ctrl_forward 'i' //forward 00050 #define ctrl_backward 'k' //back 00051 #define ctrl_left 'j' //turn left 00052 #define ctrl_right 'l' //turn right 00053 #define ctrl_calibrate 'c' //re-calibrate the accelerometer and gyro 00054 #define ctrl_turn_angle_cw 'o' // turn angle 00055 #define ctrl_turn_angle_ccw 'p' 00056 // The nRF24L01+ supports transfers from 1 to 32 bytes, but Sparkfun's 00057 // "Nordic Serial Interface Board" (http://www.sparkfun.com/products/9019) 00058 // only handles 4 byte transfers in the ATMega code. 00059 #define TRANSFER_SIZE 4 00060 00061 class Robot 00062 { 00063 public: 00064 /** 00065 * Constructor - does nothing atm 00066 */ 00067 Robot(); 00068 00069 /** 00070 * MOTOR CONTROLS 00071 */ 00072 void motor_control(int Lspeed, int Rspeed); //Input speed for motors. Integer between 0 and 100 00073 void stop(); //stop motors 00074 void set_direction(double angle); //set angle for the robot to face (from origin) 00075 void set_direction_deg(double angle); 00076 void set_speed(int Speed ); //set speed for robot to travel at 00077 void auto_enable(bool x); 00078 /** 00079 * MPU CONTROLS 00080 */ 00081 00082 /** 00083 * UPDATE (this must be called repeatedly so the robot will sample accelerometer to find position) 00084 */ 00085 void update(); //print variable decides which values to print 00086 void remote_ctrl(char c,int desired_speed, int desired_angle); 00087 //print = 0: nothing 00088 //print = 1: Accelerometer and gyro 00089 //print = 2: Current and voltage 00090 00091 // calibrate the gyro and accelerometer // 00092 void calibrate(); 00093 00094 /** 00095 * Status: find the distance, orientation, battery values, etc of the robot 00096 */ 00097 //void distanceTravelled(double x[3]) 00098 //void orientation(something quaternion? on xy plane?) 00099 double getCurrent(); //Get the current drawn by the robot 00100 double getCurrent(int n); //get the current, averaged over n samples 00101 double getVoltage(); //get the battery voltage (ask connor for completed function) 00102 void getIMU(float *adata, float *gdata); 00103 double irReadL(); 00104 double irReadC(); 00105 double irReadR(); 00106 double return_rotation(); 00107 int isMPU(); 00108 00109 00110 //Wireless connections 00111 Serial bt; //bluetooth connection 00112 nRF24L01P rf24; //RF network connection 00113 00114 //RF24 network functions// 00115 void rf24_power(int status); //power on or off the RF network 00116 char rf24_read(); 00117 int rf24_write(char letter); //write a letter to RF 00118 int rf24_write(char* buffer, int length); //write 00119 int acc[3]; 00120 int gyr[3]; 00121 00122 00123 00124 //-------------------PRIVATE VARIABLES AND FUNCTIONS-----------------------------------------------// 00125 private: 00126 00127 //commands for remote control robot 00128 00129 MPU6050 mpu; //MPU connection 00130 DigitalOut myled; //(PTE3) Processor LED (1 = off, 0 = on) 00131 DigitalOut btSwitch; 00132 AnalogIn currentSensor; 00133 AnalogIn irSensorL; 00134 AnalogIn irSensorC; 00135 AnalogIn irSensorR; 00136 AnalogIn voltageSensor; 00137 00138 double dx; //distance travelled in x direction 00139 double dy; //distance travelled in y direction 00140 double dz; //distance travelled in z direction (zero?) 00141 double origin; //location of robot origin (or can be set if robot starting location is known. 00142 double target_angle; //direction that we want the robot to face (radians) 00143 00144 int accdata[3]; //data from accelerometer (raw) 00145 int gyrodata[3]; //data from gyro (raw) 00146 //double gyroCorrect; //= 3720; //divide by this to get gyro data in RAD/SECOND. This is above as a #DEFINE 00147 int gyroOffset[3]; //Correction value for each gyroscope to zero the values. 00148 int accOffset[3]; //correction value for each accelerometer 00149 int speed; //set the speed of robot 00150 /**Angle is always measured in clockwise direction, looking down from the top**/ 00151 /*Angle is measured in RADIANS*/ 00152 double rz; //Direction robot is facing 00153 double Irz; //integral of the rotation offset from target. (Optionally) Used for PID control of direction 00154 double angle_origin; //Angle of origin (can be changed later, or set if robot starts at known angle) 00155 bool AUTO_ORIENT; //if this flag is 1, the robot automatically orients itself to selected direction 00156 bool REMOTE_CONTROL; //if this flag is 1, the robot will be controlled over bluetooth 00157 00158 /////////// Motor control variables /////////// 00159 PwmOut PWMA;//(MOT_PWMA_PIN); 00160 PwmOut PWMB;//(MOT_PWMB_PIN); 00161 DigitalOut AIN1;//(MOT_AIN1_PIN); 00162 DigitalOut AIN2;//(MOT_AIN2_PIN); 00163 DigitalOut BIN1;//(MOT_BIN1_PIN); 00164 DigitalOut BIN2;//(MOT_BIN2_PIN); 00165 DigitalOut STBY;//(MOT_STBY_PIN); 00166 double timeNext; //next time that the motor is allowed to be updated 00167 00168 bool MPU_OK; 00169 00170 Timer tt; //timer 00171 00172 double time; //time of current iteration 00173 double timePrev; //time of previous iteration 00174 00175 00176 }; 00177 #endif
Generated on Sat Jul 16 2022 15:01:35 by
