robot

Dependencies:   MPU6050_Lab6_Part3 mbed

Fork of ESE519_Lab6_part3_skeleton by Carter Sharer

Committer:
csharer
Date:
Thu Nov 10 19:20:55 2016 +0000
Revision:
6:ae3e6aefe908
Parent:
4:2512939c10f0
Child:
9:d9776d8ce47c
ese519 lab6 part 3;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
csharer 6:ae3e6aefe908 1 //Balance Bot V4
csharer 4:2512939c10f0 2 //Author: Carter Sharer
csharer 4:2512939c10f0 3 //Date: 10/13/2016
csharer 6:ae3e6aefe908 4 //ESE519 Lab 6 Part 3 Skeleton Code
csharer 3:2f76ffbc5cef 5
csharer 6:ae3e6aefe908 6 /******************************* README USAGE *******************************
csharer 6:ae3e6aefe908 7 * This robot must be powered on while it is laying down flat on a still table
csharer 6:ae3e6aefe908 8 * This allows the robot to calibrate the IMU (~5 seconds)
csharer 6:ae3e6aefe908 9 * The motors are DISABLED when the robot tilts more then +-45 degrees from
csharer 6:ae3e6aefe908 10 * vertical. To ENABLE the motors you must lift the robot to < +- 45 degres and
csharer 6:ae3e6aefe908 11 * press the joystick button.
csharer 6:ae3e6aefe908 12 * To reset the motor positions you must press the josystick button anytime.
csharer 6:ae3e6aefe908 13 ******************************************************************************/
csharer 6:ae3e6aefe908 14
csharer 6:ae3e6aefe908 15 //Balance Bot Begin
csharer 3:2f76ffbc5cef 16 #include "pin_assignments.h"
csharer 3:2f76ffbc5cef 17 #include "I2Cdev.h"
csharer 3:2f76ffbc5cef 18 #include "JJ_MPU6050_DMP_6Axis.h"
csharer 6:ae3e6aefe908 19 #include "balance_bot.h"
csharer 6:ae3e6aefe908 20 #include "balance_bot_IMU.h"
csharer 3:2f76ffbc5cef 21 #include "stepper_motors.h"
csharer 4:2512939c10f0 22 #include "MRF24J40.h"
csharer 4:2512939c10f0 23
csharer 6:ae3e6aefe908 24 //Angle Offset is used to set the natural balance point of your robot.
csharer 6:ae3e6aefe908 25 //You should adjust this offset so that your robots balance point is near 0
csharer 6:ae3e6aefe908 26 #define ANGLE_OFFSET 107
csharer 6:ae3e6aefe908 27
csharer 4:2512939c10f0 28 //For RF Communication
csharer 4:2512939c10f0 29 #define JSTICK_H 8
csharer 4:2512939c10f0 30 #define JSTICK_V 9
csharer 4:2512939c10f0 31 #define SPACE 10
csharer 4:2512939c10f0 32 #define KNOB1 11
csharer 4:2512939c10f0 33 #define KNOB2 12
csharer 4:2512939c10f0 34 #define KNOB3 13
csharer 4:2512939c10f0 35 #define KNOB4 14
csharer 4:2512939c10f0 36 #define ANGLE 15
csharer 4:2512939c10f0 37 #define BUTTON 16
csharer 4:2512939c10f0 38 #define JSTICK_OFFSET 100
csharer 4:2512939c10f0 39 #define TX_BUFFER_LEN 18
csharer 4:2512939c10f0 40 #define TX_ANGLE_OFFSET 100
csharer 4:2512939c10f0 41 //Knobs
csharer 4:2512939c10f0 42 #define POT1 p17
csharer 4:2512939c10f0 43 #define POT2 p18
csharer 4:2512939c10f0 44 #define POT3 p16
csharer 4:2512939c10f0 45 #define POT4 p15
csharer 4:2512939c10f0 46 //JoyStick
csharer 4:2512939c10f0 47 #define POTV p19
csharer 4:2512939c10f0 48 #define POTH p20
csharer 3:2f76ffbc5cef 49
csharer 3:2f76ffbc5cef 50 //PID
csharer 6:ae3e6aefe908 51 #define MAX_THROTTLE 100
csharer 3:2f76ffbc5cef 52 #define MAX_TARGET_ANGLE 12
csharer 6:ae3e6aefe908 53 //PID Default control values from constant definitions
csharer 6:ae3e6aefe908 54 float Kp1;
csharer 6:ae3e6aefe908 55 float Kd1;
csharer 6:ae3e6aefe908 56 float Kp2;
csharer 6:ae3e6aefe908 57 float Kd2;
csharer 4:2512939c10f0 58
csharer 4:2512939c10f0 59 //Controller Values
csharer 4:2512939c10f0 60 uint8_t knob1, knob2, knob3, knob4;
csharer 4:2512939c10f0 61 int8_t jstick_h, jstick_v;
csharer 4:2512939c10f0 62
csharer 6:ae3e6aefe908 63 //Control Variables
csharer 3:2f76ffbc5cef 64 float target_angle;
csharer 6:ae3e6aefe908 65 float throttle = 0; //From joystick
csharer 6:ae3e6aefe908 66 float steering = 0; //From joystick
csharer 3:2f76ffbc5cef 67 float max_target_angle = MAX_TARGET_ANGLE;
csharer 6:ae3e6aefe908 68 int robot_pos = 0; //Robots position
csharer 6:ae3e6aefe908 69 bool fallen = true;
csharer 3:2f76ffbc5cef 70
csharer 3:2f76ffbc5cef 71 Timer timer;
csharer 6:ae3e6aefe908 72 int timer_value;
csharer 6:ae3e6aefe908 73 int timer_old;
csharer 4:2512939c10f0 74 int dt;
csharer 3:2f76ffbc5cef 75
csharer 6:ae3e6aefe908 76 //Loop Counters
csharer 3:2f76ffbc5cef 77 uint8_t slow_loop_counter;
csharer 4:2512939c10f0 78 uint8_t medium_loop_counter;
csharer 3:2f76ffbc5cef 79 uint8_t loop_counter;
csharer 3:2f76ffbc5cef 80
csharer 3:2f76ffbc5cef 81 Serial pc(USBTX, USBRX);
csharer 3:2f76ffbc5cef 82
csharer 4:2512939c10f0 83 // LEDs
csharer 4:2512939c10f0 84 DigitalOut led1(LED1);
csharer 4:2512939c10f0 85 DigitalOut led2(LED2);
csharer 4:2512939c10f0 86 DigitalOut led3(LED3);
csharer 4:2512939c10f0 87 DigitalOut led4(LED4);
csharer 4:2512939c10f0 88
csharer 4:2512939c10f0 89 //Button
csharer 4:2512939c10f0 90 bool button;
csharer 6:ae3e6aefe908 91 #include "communication.h"
csharer 4:2512939c10f0 92
csharer 3:2f76ffbc5cef 93 // ================================================================
csharer 3:2f76ffbc5cef 94 // === INITIAL SETUP ===
csharer 3:2f76ffbc5cef 95 // ================================================================
csharer 3:2f76ffbc5cef 96 void init_imu()
csharer 3:2f76ffbc5cef 97 {
csharer 3:2f76ffbc5cef 98 pc.printf("\r\r\n\n Start \r\n");
csharer 3:2f76ffbc5cef 99
csharer 3:2f76ffbc5cef 100 // Manual MPU initialization... accel=2G, gyro=2000º/s, filter=20Hz BW, output=200Hz
csharer 3:2f76ffbc5cef 101 mpu.setClockSource(MPU6050_CLOCK_PLL_ZGYRO);
csharer 3:2f76ffbc5cef 102 mpu.setFullScaleGyroRange(MPU6050_GYRO_FS_2000);
csharer 3:2f76ffbc5cef 103 mpu.setFullScaleAccelRange(MPU6050_ACCEL_FS_2);
csharer 3:2f76ffbc5cef 104 mpu.setDLPFMode(MPU6050_DLPF_BW_10); //10,20,42,98,188 // Default factor for BROBOT:10
csharer 4:2512939c10f0 105 mpu.setRate(4); // 0=1khz 1=500hz, 2=333hz, 3=250hz [4=200hz]default
csharer 3:2f76ffbc5cef 106 mpu.setSleepEnabled(false);
csharer 3:2f76ffbc5cef 107 wait_ms(500);
csharer 3:2f76ffbc5cef 108
csharer 3:2f76ffbc5cef 109 // load and configure the DMP
csharer 3:2f76ffbc5cef 110 devStatus = mpu.dmpInitialize();
csharer 3:2f76ffbc5cef 111 if(devStatus == 0) {
csharer 3:2f76ffbc5cef 112 mpu.setDMPEnabled(true);
csharer 3:2f76ffbc5cef 113 mpuIntStatus = mpu.getIntStatus();
csharer 3:2f76ffbc5cef 114 dmpReady = true;
csharer 4:2512939c10f0 115 } else {
csharer 3:2f76ffbc5cef 116 // 1 = initial memory load failed
csharer 3:2f76ffbc5cef 117 // 2 = DMP configuration updates failed
csharer 3:2f76ffbc5cef 118 pc.printf("DMP INIT error \r\n");
csharer 3:2f76ffbc5cef 119 }
csharer 3:2f76ffbc5cef 120
csharer 3:2f76ffbc5cef 121 //Gyro Calibration
csharer 3:2f76ffbc5cef 122 wait_ms(500);
csharer 3:2f76ffbc5cef 123 pc.printf("Gyro calibration!! Dont move the robot in 10 seconds... \r\n");
csharer 3:2f76ffbc5cef 124 wait_ms(500);
csharer 4:2512939c10f0 125
csharer 3:2f76ffbc5cef 126 // verify connection
csharer 3:2f76ffbc5cef 127 pc.printf(mpu.testConnection() ? "Connection Good \r\n" : "Connection Failed\r\n");
csharer 3:2f76ffbc5cef 128
csharer 3:2f76ffbc5cef 129 //Adjust Sensor Fusion Gain
csharer 3:2f76ffbc5cef 130 dmpSetSensorFusionAccelGain(0x20);
csharer 3:2f76ffbc5cef 131
csharer 3:2f76ffbc5cef 132 wait_ms(200);
csharer 3:2f76ffbc5cef 133 mpu.resetFIFO();
csharer 3:2f76ffbc5cef 134 }
csharer 3:2f76ffbc5cef 135
csharer 3:2f76ffbc5cef 136 // ================================================================
csharer 3:2f76ffbc5cef 137 // === MAIN PROGRAM LOOP ===
csharer 3:2f76ffbc5cef 138 // ================================================================
csharer 3:2f76ffbc5cef 139 int main()
csharer 3:2f76ffbc5cef 140 {
csharer 6:ae3e6aefe908 141 //Set the Channel. 0 is default, 15 is max
csharer 6:ae3e6aefe908 142 uint8_t channel = 2;
csharer 6:ae3e6aefe908 143 mrf.SetChannel(channel);
csharer 6:ae3e6aefe908 144
csharer 6:ae3e6aefe908 145 pc.baud(115200);
csharer 3:2f76ffbc5cef 146 pc.printf("Start\r\n");
csharer 3:2f76ffbc5cef 147 init_imu();
csharer 3:2f76ffbc5cef 148 timer.start();
csharer 3:2f76ffbc5cef 149 //timer
csharer 4:2512939c10f0 150 timer_value = timer.read_us();
syundo0730 0:8d2c753a96e7 151
csharer 3:2f76ffbc5cef 152 //Init Stepper Motors
csharer 3:2f76ffbc5cef 153 //Attach Timer Interupts (Tiker)
csharer 3:2f76ffbc5cef 154 timer_M1.attach_us(&ISR1, ZERO_SPEED);
csharer 3:2f76ffbc5cef 155 timer_M2.attach_us(&ISR2, ZERO_SPEED);
csharer 3:2f76ffbc5cef 156 step_M1 = 1;
csharer 3:2f76ffbc5cef 157 dir_M1 = 1;
csharer 6:ae3e6aefe908 158 enable = DISABLE; //Disable Motors
csharer 4:2512939c10f0 159
csharer 4:2512939c10f0 160 //Attach Interupt for IMU
csharer 4:2512939c10f0 161 checkpin.rise(&dmpDataReady);
csharer 4:2512939c10f0 162
csharer 4:2512939c10f0 163 //Used to set angle upon startup, filter
csharer 4:2512939c10f0 164 bool FILTER_DISABLE = true;
csharer 4:2512939c10f0 165
csharer 6:ae3e6aefe908 166 //Enable Motors
csharer 6:ae3e6aefe908 167 enable = ENABLE;
csharer 6:ae3e6aefe908 168
csharer 3:2f76ffbc5cef 169 while(1) {
csharer 6:ae3e6aefe908 170 //Led 4 to indicate if robot it STANDING or FALLEN
csharer 6:ae3e6aefe908 171 led4 = !fallen;
csharer 4:2512939c10f0 172
csharer 6:ae3e6aefe908 173 //Led 2 to indicate a button press
csharer 6:ae3e6aefe908 174 led2 = button;
csharer 6:ae3e6aefe908 175
csharer 6:ae3e6aefe908 176 //If button is pressed reset motor position
csharer 4:2512939c10f0 177 if(button) {
csharer 6:ae3e6aefe908 178 pos_M1 = 0; //Reset position of Motor 1
csharer 6:ae3e6aefe908 179 pos_M2 = 0; //Reset position of motor 2
csharer 6:ae3e6aefe908 180 fallen = false; //Reset fallen flag
csharer 4:2512939c10f0 181 }
csharer 4:2512939c10f0 182
csharer 6:ae3e6aefe908 183 //This is the main while loop, all your code goes here
csharer 6:ae3e6aefe908 184 while(!mpuInterrupt) {
csharer 6:ae3e6aefe908 185 //Timer
csharer 4:2512939c10f0 186 timer_value = timer.read_us();
csharer 4:2512939c10f0 187
csharer 6:ae3e6aefe908 188 //Set gainz with knobs
csharer 6:ae3e6aefe908 189 Kp1 = knob1;
csharer 6:ae3e6aefe908 190 Kd1 = knob2;
csharer 6:ae3e6aefe908 191 Kp2 = knob3;
csharer 6:ae3e6aefe908 192 Kd2 = knob4;
csharer 4:2512939c10f0 193
csharer 4:2512939c10f0 194 //Joystick control
csharer 6:ae3e6aefe908 195 throttle = jstick_v;
csharer 6:ae3e6aefe908 196 steering = jstick_h;
csharer 4:2512939c10f0 197
csharer 6:ae3e6aefe908 198 /**** Update Values DO NOT MODIFY ********/
csharer 3:2f76ffbc5cef 199 loop_counter++;
csharer 3:2f76ffbc5cef 200 slow_loop_counter++;
csharer 4:2512939c10f0 201 medium_loop_counter++;
csharer 3:2f76ffbc5cef 202 dt = (timer_value - timer_old);
csharer 3:2f76ffbc5cef 203 timer_old = timer_value;
csharer 4:2512939c10f0 204 angle_old = angle;
csharer 6:ae3e6aefe908 205 /*****************************************/
csharer 3:2f76ffbc5cef 206
csharer 6:ae3e6aefe908 207 //STANDING: Motor Control Enabled
csharer 6:ae3e6aefe908 208 if(((angle < 45) && (angle > -45)) && (fallen == false)) {
csharer 3:2f76ffbc5cef 209
csharer 6:ae3e6aefe908 210 //Enable Motor
csharer 6:ae3e6aefe908 211 enable = ENABLE;
csharer 4:2512939c10f0 212
csharer 6:ae3e6aefe908 213 /* This is where you want to impliment your controlers
csharer 6:ae3e6aefe908 214 * Start off with a simple P controller.
csharer 6:ae3e6aefe908 215 *
csharer 6:ae3e6aefe908 216 * float error = angle - 0; //should be balanced at 0
csharer 6:ae3e6aefe908 217 * motor1 = error * Kp;
csharer 6:ae3e6aefe908 218 * motor2 = error * Kp; */
csharer 4:2512939c10f0 219
csharer 6:ae3e6aefe908 220 //Calculate motor inputs
csharer 6:ae3e6aefe908 221 motor1 = int16_t(throttle/2 + steering/8);
csharer 6:ae3e6aefe908 222 motor2 = int16_t(throttle/2 - steering/8);
csharer 4:2512939c10f0 223
csharer 6:ae3e6aefe908 224 //Cap the max and min values [-100, 100]
csharer 4:2512939c10f0 225 motor1 = CAP(motor1, MAX_CONTROL_OUTPUT);
csharer 4:2512939c10f0 226 motor2 = CAP(motor2, MAX_CONTROL_OUTPUT);
csharer 6:ae3e6aefe908 227
csharer 6:ae3e6aefe908 228 //Set Motor Speed here
csharer 6:ae3e6aefe908 229 setMotor1Speed(motor1);
csharer 6:ae3e6aefe908 230 setMotor2Speed(motor2);
csharer 4:2512939c10f0 231
csharer 6:ae3e6aefe908 232 } else { //FALLEN: Motor Control Disabled
csharer 4:2512939c10f0 233 //Disable Motors
csharer 4:2512939c10f0 234 enable = DISABLE;
csharer 6:ae3e6aefe908 235
csharer 6:ae3e6aefe908 236 //Set fallen flag
csharer 6:ae3e6aefe908 237 fallen = true;
csharer 4:2512939c10f0 238 }
csharer 6:ae3e6aefe908 239
csharer 6:ae3e6aefe908 240 /* Here are some loops that trigger at different intervals, this
csharer 6:ae3e6aefe908 241 * will allow you to do things at a slower rate, thus saving speed
csharer 6:ae3e6aefe908 242 * it is important to keep this fast so we dont miss IMU readings */
csharer 6:ae3e6aefe908 243
csharer 6:ae3e6aefe908 244 //Fast Loop: Good for printing to serial monitor
csharer 4:2512939c10f0 245 if(loop_counter >= 5) {
csharer 4:2512939c10f0 246 loop_counter = 0;
csharer 6:ae3e6aefe908 247 pc.printf("angle:%d Kp1:%0.3f Kd1:%0.2f Kp2:%0.2f Kd2:%0.3f pos_M1:%d pos_M2:%d \r\n", (int)angle, Kp1, Kd1, Kp2, Kd2, pos_M1, pos_M2, robot_pos);
csharer 4:2512939c10f0 248 }
csharer 3:2f76ffbc5cef 249
csharer 6:ae3e6aefe908 250 //Meduim Loop: Good for sending and receiving
csharer 4:2512939c10f0 251 if (medium_loop_counter >= 10) {
csharer 4:2512939c10f0 252 medium_loop_counter = 0; // Read status
csharer 3:2f76ffbc5cef 253
csharer 4:2512939c10f0 254 //Recieve Data
csharer 6:ae3e6aefe908 255 rxLen = rf_receive(rxBuffer, 128);
csharer 6:ae3e6aefe908 256 if(rxLen > 0) {
csharer 6:ae3e6aefe908 257 led1 = led1^1;
csharer 6:ae3e6aefe908 258 //Process data with our protocal
csharer 6:ae3e6aefe908 259 communication_protocal(rxLen);
csharer 4:2512939c10f0 260 }
csharer 6:ae3e6aefe908 261
csharer 4:2512939c10f0 262 } // End of medium loop
csharer 6:ae3e6aefe908 263
csharer 6:ae3e6aefe908 264 //Slow Loop: Good for sending if speed is not an issue
csharer 4:2512939c10f0 265 if(slow_loop_counter >= 99) {
csharer 4:2512939c10f0 266 slow_loop_counter = 0;
csharer 3:2f76ffbc5cef 267
csharer 6:ae3e6aefe908 268 /* Send Data To Controller goes here *
csharer 6:ae3e6aefe908 269 * */
csharer 4:2512939c10f0 270 } //End of Slow Loop
csharer 3:2f76ffbc5cef 271
csharer 4:2512939c10f0 272 //Reattach interupt
csharer 4:2512939c10f0 273 checkpin.rise(&dmpDataReady);
csharer 4:2512939c10f0 274 } //END WHILE
csharer 4:2512939c10f0 275
csharer 6:ae3e6aefe908 276
csharer 6:ae3e6aefe908 277 /********************* All IMU Handling DO NOT MODIFY *****************/
csharer 4:2512939c10f0 278 //Disable IRQ
csharer 4:2512939c10f0 279 checkpin.rise(NULL);
csharer 6:ae3e6aefe908 280
csharer 6:ae3e6aefe908 281 //reset interrupt flag and get INT_STATUS byte
csharer 3:2f76ffbc5cef 282 mpuInterrupt = false;
csharer 3:2f76ffbc5cef 283 mpuIntStatus = mpu.getIntStatus();
csharer 3:2f76ffbc5cef 284
csharer 6:ae3e6aefe908 285 //get current FIFO count
csharer 3:2f76ffbc5cef 286 fifoCount = mpu.getFIFOCount();
csharer 3:2f76ffbc5cef 287
csharer 3:2f76ffbc5cef 288 // check for overflow (this should never happen unless our code is too inefficient)
csharer 3:2f76ffbc5cef 289 if ((mpuIntStatus & 0x10) || fifoCount == 1024) {
csharer 3:2f76ffbc5cef 290 // reset so we can continue cleanly
csharer 3:2f76ffbc5cef 291 mpu.resetFIFO();
csharer 4:2512939c10f0 292 pc.printf("FIFO overflow!");
csharer 4:2512939c10f0 293
csharer 6:ae3e6aefe908 294 //otherwise, check for DMP data ready interrupt (this should happen frequently)
csharer 3:2f76ffbc5cef 295 } else if (mpuIntStatus & 0x02) {
csharer 6:ae3e6aefe908 296 //wait for correct available data length, should be a VERY short wait
csharer 4:2512939c10f0 297 while (fifoCount < packetSize) fifoCount = mpu.getFIFOCount();
csharer 3:2f76ffbc5cef 298
csharer 6:ae3e6aefe908 299 //read a packet from FIFO
csharer 3:2f76ffbc5cef 300 mpu.getFIFOBytes(fifoBuffer, packetSize);
csharer 3:2f76ffbc5cef 301
csharer 6:ae3e6aefe908 302 //track FIFO count here in case there is > 1 packet available
csharer 6:ae3e6aefe908 303 //(this lets us immediately read more without waiting for an interrupt)
csharer 3:2f76ffbc5cef 304 fifoCount -= packetSize;
csharer 3:2f76ffbc5cef 305
csharer 4:2512939c10f0 306 //Read new angle from IMU
csharer 4:2512939c10f0 307 new_angle = (float)(dmpGetPhi() - ANGLE_OFFSET);
csharer 4:2512939c10f0 308 dAngle = new_angle - angle;
csharer 3:2f76ffbc5cef 309
csharer 4:2512939c10f0 310 //Filter out angle readings larger then MAX_ANGLE_DELTA
csharer 4:2512939c10f0 311 if( ((dAngle < 15) && (dAngle > -15)) || FILTER_DISABLE) {
csharer 4:2512939c10f0 312 angle = new_angle;
csharer 4:2512939c10f0 313 FILTER_DISABLE = false; //turn of filter disabler
csharer 6:ae3e6aefe908 314 }
csharer 6:ae3e6aefe908 315 }
csharer 6:ae3e6aefe908 316 /********************* All IMU Handling DO NOT MODIFY *****************/
csharer 3:2f76ffbc5cef 317
csharer 3:2f76ffbc5cef 318 } //end main loop
csharer 3:2f76ffbc5cef 319 } //End Main()