BroBot Code for ESE350 Lab6 part 3 (Skeleton)

Dependencies:   MPU6050_V3 mbed-rtos mbed

Fork of BroBot_RTOS_ESE350 by Carter Sharer

Committer:
csharer
Date:
Tue Jan 24 01:33:59 2017 +0000
Revision:
13:a18d41a60d0b
Parent:
12:16d1a5390022
Child:
14:94c65d1c8bad
test

Who changed what in which revision?

UserRevisionLine numberNew contents of line
csharer 9:fb671fa0c0be 1 //BroBot V3 this is the most up to date, velocity is now working
csharer 4:2512939c10f0 2 //Author: Carter Sharer
csharer 4:2512939c10f0 3 //Date: 10/13/2016
csharer 6:62cdb7482b50 4 //Added communication protocol v1 (no type selection)
csharer 9:fb671fa0c0be 5 //Knob1 39.898 Knob2 44.381 Knob3 10.55 Knob4 18.67
csharer 9:fb671fa0c0be 6 //Knob1 13.418 Knob2 28.848 Knob3 9.45 Knob4 42.29 Good
csharer 9:fb671fa0c0be 7 //Knob1 15.373 Knob2 28.261 Knob3 10.42 Knob4 40.97 Smoother
csharer 9:fb671fa0c0be 8
csharer 9:fb671fa0c0be 9 /******************************* README USAGE *******************************
csharer 9:fb671fa0c0be 10 * This robot must be powered on while it is laying down flat on a still table
csharer 9:fb671fa0c0be 11 * This allows the robot to calibrate the IMU (~5 seconds)
csharer 9:fb671fa0c0be 12 * The motors are DISABLED when the robot tilts more then +-45 degrees from
csharer 9:fb671fa0c0be 13 * vertical. To ENABLE the motors you must lift the robot to < +- 45 degres and
csharer 9:fb671fa0c0be 14 * press the joystick button.
csharer 9:fb671fa0c0be 15 * To reset the motor positions you must press the josystick button anytime.
csharer 9:fb671fa0c0be 16 ******************************************************************************/
csharer 3:2f76ffbc5cef 17
arvindnr89 10:48f640a54401 18 //Controller Values
arvindnr89 10:48f640a54401 19 float knob1, knob2, knob3, knob4;
arvindnr89 10:48f640a54401 20 float jstick_h, jstick_v;
arvindnr89 10:48f640a54401 21
arvindnr89 10:48f640a54401 22 //Button
arvindnr89 10:48f640a54401 23 bool button;
arvindnr89 10:48f640a54401 24
csharer 4:2512939c10f0 25 //BroBot Begin
csharer 9:fb671fa0c0be 26 #include "cmsis_os.h"
arvindnr89 11:2553f5798f84 27 #include "rtos_definations.h"
csharer 3:2f76ffbc5cef 28 #include "pin_assignments.h"
csharer 3:2f76ffbc5cef 29 #include "I2Cdev.h"
csharer 3:2f76ffbc5cef 30 #include "JJ_MPU6050_DMP_6Axis.h"
csharer 3:2f76ffbc5cef 31 #include "BroBot.h"
csharer 3:2f76ffbc5cef 32 #include "BroBot_IMU.h"
csharer 3:2f76ffbc5cef 33 #include "stepper_motors.h"
csharer 4:2512939c10f0 34 #include "MRF24J40.h"
arvindnr89 10:48f640a54401 35 #include "communication.h"
csharer 4:2512939c10f0 36
csharer 9:fb671fa0c0be 37 //Angle Offset is used to set the natural balance point of your robot.
csharer 6:62cdb7482b50 38 //You should adjust this offset so that your robots balance points is near 0
csharer 6:62cdb7482b50 39 #define ANGLE_OFFSET 107
arvindnr89 10:48f640a54401 40 #define MRF_CHANNEL 2
csharer 6:62cdb7482b50 41
csharer 4:2512939c10f0 42 //For RF Communication
csharer 4:2512939c10f0 43 #define JSTICK_H 8
csharer 4:2512939c10f0 44 #define JSTICK_V 9
csharer 4:2512939c10f0 45 #define SPACE 10
csharer 4:2512939c10f0 46 #define KNOB1 11
csharer 4:2512939c10f0 47 #define KNOB2 12
csharer 4:2512939c10f0 48 #define KNOB3 13
csharer 4:2512939c10f0 49 #define KNOB4 14
csharer 4:2512939c10f0 50 #define ANGLE 15
csharer 4:2512939c10f0 51 #define BUTTON 16
csharer 4:2512939c10f0 52 #define JSTICK_OFFSET 100
csharer 4:2512939c10f0 53 #define TX_BUFFER_LEN 18
csharer 4:2512939c10f0 54 #define TX_ANGLE_OFFSET 100
csharer 4:2512939c10f0 55 //Knobs
csharer 4:2512939c10f0 56 #define POT1 p17
csharer 4:2512939c10f0 57 #define POT2 p18
csharer 4:2512939c10f0 58 #define POT3 p16
csharer 4:2512939c10f0 59 #define POT4 p15
csharer 4:2512939c10f0 60 //JoyStick
csharer 4:2512939c10f0 61 #define POTV p19
csharer 4:2512939c10f0 62 #define POTH p20
csharer 3:2f76ffbc5cef 63
csharer 3:2f76ffbc5cef 64 //PID
csharer 3:2f76ffbc5cef 65 #define MAX_THROTTLE 580
csharer 3:2f76ffbc5cef 66 #define MAX_STEERING 150
csharer 3:2f76ffbc5cef 67 #define MAX_TARGET_ANGLE 12
csharer 3:2f76ffbc5cef 68 #define KP 0.19
csharer 3:2f76ffbc5cef 69 #define KD 28
csharer 3:2f76ffbc5cef 70 #define KP_THROTTLE 0.01 //0.07
csharer 3:2f76ffbc5cef 71 #define KI_THROTTLE 0//0.04
csharer 3:2f76ffbc5cef 72 #define ITERM_MAX_ERROR 25 // Iterm windup constants for PI control //40
csharer 3:2f76ffbc5cef 73 #define ITERM_MAX 8000 // 5000
csharer 3:2f76ffbc5cef 74
arvindnr89 10:48f640a54401 75 #define GPIOA p21
arvindnr89 10:48f640a54401 76 #define GPIOB p22
arvindnr89 10:48f640a54401 77 #define GPIOC p23
arvindnr89 10:48f640a54401 78 #define GPIOD p24
arvindnr89 10:48f640a54401 79
arvindnr89 11:2553f5798f84 80 #define THROTTLE_DAMPNER 1000
arvindnr89 11:2553f5798f84 81
arvindnr89 12:16d1a5390022 82 #define VELOCITY_SAMPLES 1000
arvindnr89 12:16d1a5390022 83
arvindnr89 11:2553f5798f84 84
arvindnr89 11:2553f5798f84 85
arvindnr89 11:2553f5798f84 86
arvindnr89 10:48f640a54401 87
arvindnr89 10:48f640a54401 88 //*********** Local Function Definations BEGIN **************//
arvindnr89 10:48f640a54401 89 void init_system();
arvindnr89 10:48f640a54401 90 void init_imu();
arvindnr89 10:48f640a54401 91 //*********** Local Function Definations END **************//
arvindnr89 10:48f640a54401 92
arvindnr89 10:48f640a54401 93
csharer 4:2512939c10f0 94
arvindnr89 12:16d1a5390022 95
arvindnr89 12:16d1a5390022 96
arvindnr89 12:16d1a5390022 97
arvindnr89 12:16d1a5390022 98
arvindnr89 12:16d1a5390022 99
arvindnr89 12:16d1a5390022 100
arvindnr89 12:16d1a5390022 101
arvindnr89 12:16d1a5390022 102
csharer 3:2f76ffbc5cef 103
csharer 3:2f76ffbc5cef 104 Timer timer;
arvindnr89 12:16d1a5390022 105 int timer_value = 0;
arvindnr89 12:16d1a5390022 106 int timer_old = 0;
arvindnr89 12:16d1a5390022 107 float velocitySamples[VELOCITY_SAMPLES + 1] = {0.0};
csharer 3:2f76ffbc5cef 108
csharer 3:2f76ffbc5cef 109 Serial pc(USBTX, USBRX);
csharer 3:2f76ffbc5cef 110
csharer 4:2512939c10f0 111 // LEDs
csharer 4:2512939c10f0 112 DigitalOut led1(LED1);
csharer 4:2512939c10f0 113 DigitalOut led2(LED2);
csharer 4:2512939c10f0 114 DigitalOut led3(LED3);
csharer 4:2512939c10f0 115 DigitalOut led4(LED4);
csharer 4:2512939c10f0 116
arvindnr89 10:48f640a54401 117 //GPIO Pins
arvindnr89 10:48f640a54401 118 DigitalOut gpioMonitorA(GPIOA);
arvindnr89 10:48f640a54401 119 DigitalOut gpioMonitorB(GPIOB);
arvindnr89 10:48f640a54401 120 DigitalOut gpioMonitorC(GPIOC);
arvindnr89 10:48f640a54401 121 DigitalOut gpioMonitorD(GPIOD);
csharer 4:2512939c10f0 122
arvindnr89 10:48f640a54401 123
arvindnr89 10:48f640a54401 124
arvindnr89 10:48f640a54401 125
arvindnr89 10:48f640a54401 126 //Used to set angle upon startup
arvindnr89 10:48f640a54401 127 bool initilizeAngle = true;
csharer 9:fb671fa0c0be 128
arvindnr89 11:2553f5798f84 129
csharer 9:fb671fa0c0be 130 // ================================================================
arvindnr89 10:48f640a54401 131 // === IMU Thread ===
csharer 9:fb671fa0c0be 132 // ================================================================
arvindnr89 10:48f640a54401 133 void imu_update_thread(void const *args)
csharer 9:fb671fa0c0be 134 {
arvindnr89 11:2553f5798f84 135 pc.printf("Starting IMU Thread..\r\n");
arvindnr89 11:2553f5798f84 136 while (1) {
arvindnr89 11:2553f5798f84 137 osSignalWait(IMU_UPDATE_SIGNAL,osWaitForever);
arvindnr89 11:2553f5798f84 138 //gpioMonitorA = 1;
arvindnr89 11:2553f5798f84 139 if(mpuInterrupt) {
arvindnr89 10:48f640a54401 140 mpuInterrupt = false;
csharer 9:fb671fa0c0be 141 led3 = !led3;
csharer 9:fb671fa0c0be 142 /********************* All IMU Handling DO NOT MODIFY *****************/
csharer 9:fb671fa0c0be 143 //Disable IRQ
csharer 9:fb671fa0c0be 144 //checkpin.rise(NULL);
csharer 9:fb671fa0c0be 145
csharer 9:fb671fa0c0be 146 //reset interrupt flag and get INT_STATUS byte
arvindnr89 10:48f640a54401 147
csharer 9:fb671fa0c0be 148 mpuIntStatus = mpu.getIntStatus();
csharer 9:fb671fa0c0be 149
csharer 9:fb671fa0c0be 150 //get current FIFO count
csharer 9:fb671fa0c0be 151 fifoCount = mpu.getFIFOCount();
csharer 9:fb671fa0c0be 152
csharer 9:fb671fa0c0be 153 // check for overflow (this should never happen unless our code is too inefficient)
csharer 9:fb671fa0c0be 154 if ((mpuIntStatus & 0x10) || fifoCount == 1024) {
csharer 9:fb671fa0c0be 155 // reset so we can continue cleanly
csharer 9:fb671fa0c0be 156 mpu.resetFIFO();
arvindnr89 10:48f640a54401 157 pc.printf("MPU FIFO overflow!");
csharer 9:fb671fa0c0be 158 //otherwise, check for DMP data ready interrupt (this should happen frequently)
csharer 9:fb671fa0c0be 159 } else if (mpuIntStatus & 0x02) {
csharer 9:fb671fa0c0be 160 //wait for correct available data length, should be a VERY short wait
csharer 9:fb671fa0c0be 161 while (fifoCount < packetSize) fifoCount = mpu.getFIFOCount();
csharer 9:fb671fa0c0be 162
csharer 9:fb671fa0c0be 163 //read a packet from FIFO
csharer 9:fb671fa0c0be 164 mpu.getFIFOBytes(fifoBuffer, packetSize);
csharer 9:fb671fa0c0be 165
csharer 9:fb671fa0c0be 166 //track FIFO count here in case there is > 1 packet available
csharer 9:fb671fa0c0be 167 //(this lets us immediately read more without waiting for an interrupt)
csharer 9:fb671fa0c0be 168 fifoCount -= packetSize;
csharer 9:fb671fa0c0be 169
csharer 9:fb671fa0c0be 170 //Read new angle from IMU
csharer 9:fb671fa0c0be 171 new_angle = (float)(dmpGetPhi() - ANGLE_OFFSET);
arvindnr89 10:48f640a54401 172 //Calculate the delta angle
csharer 9:fb671fa0c0be 173 dAngle = new_angle - angle;
csharer 9:fb671fa0c0be 174
csharer 9:fb671fa0c0be 175 //Filter out angle readings larger then MAX_ANGLE_DELTA
arvindnr89 10:48f640a54401 176 if(initilizeAngle) {
csharer 9:fb671fa0c0be 177 angle = new_angle;
arvindnr89 10:48f640a54401 178 initilizeAngle = false;
arvindnr89 10:48f640a54401 179 } else if( ((dAngle < 15) && (dAngle > -15))) {
arvindnr89 10:48f640a54401 180 angle = new_angle;
csharer 9:fb671fa0c0be 181 } else {
arvindnr89 10:48f640a54401 182 pc.printf("\t\t\t Delta Angle too Large: %d. Ignored \r\n",dAngle);
csharer 9:fb671fa0c0be 183 }
csharer 9:fb671fa0c0be 184 }
arvindnr89 11:2553f5798f84 185 //gpioMonitorA = 0;
csharer 9:fb671fa0c0be 186 /********************* All IMU Handling DO NOT MODIFY *****************/
arvindnr89 10:48f640a54401 187 }//End of if(mpuInterrupt) loop
arvindnr89 11:2553f5798f84 188 osSignalClear(imu_update_thread_ID,IMU_UPDATE_SIGNAL);
arvindnr89 10:48f640a54401 189 osThreadYield(); // Yield to a running thread
arvindnr89 10:48f640a54401 190 }
arvindnr89 10:48f640a54401 191 }
csharer 9:fb671fa0c0be 192
arvindnr89 11:2553f5798f84 193 // ================================================================
arvindnr89 11:2553f5798f84 194 // === PID Thread ===
arvindnr89 11:2553f5798f84 195 // ================================================================
arvindnr89 11:2553f5798f84 196 void pid_update_thread(void const *args)
arvindnr89 11:2553f5798f84 197 {
arvindnr89 12:16d1a5390022 198
arvindnr89 12:16d1a5390022 199 float Kp1 = 0;
arvindnr89 12:16d1a5390022 200 float Kd1 = 0;
arvindnr89 12:16d1a5390022 201 float Kp2 = 0;
arvindnr89 12:16d1a5390022 202 float Kd2 = 0;
arvindnr89 12:16d1a5390022 203 float target_angle = 0;
arvindnr89 12:16d1a5390022 204 float target_angle_old = 0;
arvindnr89 12:16d1a5390022 205 float change_in_target_angle = 0;
arvindnr89 12:16d1a5390022 206 float change_in_angle = 0;
arvindnr89 12:16d1a5390022 207 float angle_old1 = 0;
arvindnr89 12:16d1a5390022 208 float angle_old2 = 0;
arvindnr89 12:16d1a5390022 209 float kp_term = 0;
arvindnr89 12:16d1a5390022 210 float kd_term = 0;
arvindnr89 12:16d1a5390022 211 float throttle = 0;
arvindnr89 12:16d1a5390022 212 float steering = 0;
arvindnr89 12:16d1a5390022 213 float control_output = 0;
arvindnr89 12:16d1a5390022 214 int robot_pos = 0;
arvindnr89 12:16d1a5390022 215 int loop_counter = 0;
arvindnr89 12:16d1a5390022 216 int velocitySamplesCounter = 0;
arvindnr89 12:16d1a5390022 217 int dt = 0;
arvindnr89 12:16d1a5390022 218 float error = 0;
arvindnr89 12:16d1a5390022 219 float velocity = 0;
arvindnr89 12:16d1a5390022 220 bool fallen = true;
arvindnr89 12:16d1a5390022 221
arvindnr89 12:16d1a5390022 222 // --- For Position controller --- //
arvindnr89 12:16d1a5390022 223 float pos_error = 0;
arvindnr89 12:16d1a5390022 224 float kp_pos_term = 0;
arvindnr89 12:16d1a5390022 225 float kd_pos_term = 0;
arvindnr89 12:16d1a5390022 226 float change_in_target_pos = 0;
arvindnr89 12:16d1a5390022 227 float target_pos = 0;
arvindnr89 12:16d1a5390022 228 float target_pos_old = 0;
arvindnr89 12:16d1a5390022 229
arvindnr89 12:16d1a5390022 230 float runningSumReplaceVal = 0;
arvindnr89 12:16d1a5390022 231 float newVelocity = 0;
arvindnr89 12:16d1a5390022 232
arvindnr89 12:16d1a5390022 233 float velocitySum = 0;
arvindnr89 12:16d1a5390022 234 memset(velocitySamples,0,sizeof(velocitySamples));
arvindnr89 11:2553f5798f84 235 pc.printf("Starting PID Thread..\r\n");
arvindnr89 11:2553f5798f84 236 while(1) {
arvindnr89 11:2553f5798f84 237 gpioMonitorA = 1;
arvindnr89 11:2553f5798f84 238 //Get the time stamp as soon as it enters the loop
arvindnr89 11:2553f5798f84 239 timer_value = timer.read_us();
arvindnr89 11:2553f5798f84 240 //led1 = led1^1;
arvindnr89 11:2553f5798f84 241 led4 = !fallen;
arvindnr89 11:2553f5798f84 242 led2 = button;
csharer 9:fb671fa0c0be 243
arvindnr89 11:2553f5798f84 244 if(jstick_v > 80)
arvindnr89 11:2553f5798f84 245 led3 = 1;
arvindnr89 11:2553f5798f84 246 else
arvindnr89 11:2553f5798f84 247 led3 = 0;
arvindnr89 11:2553f5798f84 248
arvindnr89 11:2553f5798f84 249 //Button Press on the remote initilizes the robot position.
arvindnr89 11:2553f5798f84 250 if(button) {
arvindnr89 11:2553f5798f84 251 pos_M1 = 0;
arvindnr89 11:2553f5798f84 252 pos_M2 = 0;
arvindnr89 11:2553f5798f84 253 target_pos = 0;
arvindnr89 11:2553f5798f84 254 motor1 = 0;
arvindnr89 11:2553f5798f84 255 motor2 = 0;
arvindnr89 11:2553f5798f84 256 control_output = 0;
arvindnr89 11:2553f5798f84 257 fallen = false;
arvindnr89 11:2553f5798f84 258 }
arvindnr89 11:2553f5798f84 259
arvindnr89 11:2553f5798f84 260
arvindnr89 11:2553f5798f84 261 //Preset Knob Values for Green Balance Bot
arvindnr89 12:16d1a5390022 262 //knob1 = 1.132;
arvindnr89 12:16d1a5390022 263 //knob2 = 47.284;
arvindnr89 12:16d1a5390022 264 //knob3 = 18.46;
arvindnr89 12:16d1a5390022 265 //knob4 = 17.07;
arvindnr89 11:2553f5798f84 266
arvindnr89 11:2553f5798f84 267 //Set Gainz with knobs
arvindnr89 11:2553f5798f84 268 Kp1 = ((float)knob1) / 1000.0;
arvindnr89 11:2553f5798f84 269 Kd1 = ((float)knob2) / 1.0;
arvindnr89 11:2553f5798f84 270 Kp2 = ((float)knob3) / 1000.0;
arvindnr89 11:2553f5798f84 271 Kd2 = ((float)knob4) / 100.0;
arvindnr89 11:2553f5798f84 272
arvindnr89 11:2553f5798f84 273 //Joystick control
arvindnr89 11:2553f5798f84 274 throttle = (float)jstick_v /THROTTLE_DAMPNER;
arvindnr89 11:2553f5798f84 275 steering = (float)jstick_h / 10.0;
arvindnr89 11:2553f5798f84 276
arvindnr89 11:2553f5798f84 277 //Calculate the delta time
arvindnr89 11:2553f5798f84 278 dt = (timer_value - timer_old);
arvindnr89 11:2553f5798f84 279 timer_old = timer_value;
arvindnr89 11:2553f5798f84 280 angle_old = angle;
arvindnr89 11:2553f5798f84 281
arvindnr89 11:2553f5798f84 282 // STANDING: Motor Control Enabled
arvindnr89 11:2553f5798f84 283 //******************** PID Control BEGIN ********************** //
arvindnr89 11:2553f5798f84 284 if(((angle < 45) && (angle > -45)) && (fallen == false)) {
arvindnr89 12:16d1a5390022 285
arvindnr89 12:16d1a5390022 286 loop_counter++;
arvindnr89 11:2553f5798f84 287 //CS Pd Target Angle Contoller Goes Here
arvindnr89 11:2553f5798f84 288
arvindnr89 11:2553f5798f84 289 //Robot Position
arvindnr89 11:2553f5798f84 290 robot_pos = (pos_M1 + pos_M2) / 2;
arvindnr89 11:2553f5798f84 291 target_pos += throttle/2;
arvindnr89 12:16d1a5390022 292
arvindnr89 12:16d1a5390022 293 //Velocity Computation
arvindnr89 12:16d1a5390022 294 velocitySamplesCounter++;
arvindnr89 12:16d1a5390022 295 if(velocitySamplesCounter == VELOCITY_SAMPLES){
arvindnr89 12:16d1a5390022 296 velocitySamplesCounter = 0;
arvindnr89 12:16d1a5390022 297 }
arvindnr89 12:16d1a5390022 298 runningSumReplaceVal = velocitySamples[velocitySamplesCounter]; //value to replace
arvindnr89 12:16d1a5390022 299 newVelocity = (motor1 + motor2) / 2;
arvindnr89 12:16d1a5390022 300 velocitySamples[velocitySamplesCounter] = newVelocity; //replace value with
arvindnr89 12:16d1a5390022 301 velocitySum = velocitySum - runningSumReplaceVal + newVelocity;
arvindnr89 12:16d1a5390022 302 velocity = velocitySum/VELOCITY_SAMPLES;
arvindnr89 11:2553f5798f84 303
arvindnr89 11:2553f5798f84 304 //CS ***************** Velocity Controller *********************
arvindnr89 11:2553f5798f84 305 //float target_velocity = 0;
arvindnr89 11:2553f5798f84 306 //velocity_error = target_velocity - velocity;
arvindnr89 11:2553f5798f84 307 //target_angle = -velocity_error * Kp2 * 100;
arvindnr89 11:2553f5798f84 308 //CS ***************** Position Controller *********************
arvindnr89 11:2553f5798f84 309 pos_error = robot_pos - target_pos; //robot_pos - target_pos;
arvindnr89 11:2553f5798f84 310
arvindnr89 11:2553f5798f84 311 //KP Term
arvindnr89 11:2553f5798f84 312 kp_pos_term = -Kp2 * pos_error;
arvindnr89 11:2553f5798f84 313
arvindnr89 11:2553f5798f84 314 //KD Term
arvindnr89 11:2553f5798f84 315 change_in_target_pos = target_pos - target_pos_old;
arvindnr89 11:2553f5798f84 316 //kd_pos_term = ((-Kd2 * change_in_target_pos) + (-Kd2*change_in_pos)) /dt;
arvindnr89 11:2553f5798f84 317 kd_pos_term = ((Kd2 * change_in_target_pos) + (Kd2*velocity));
arvindnr89 11:2553f5798f84 318 target_angle = kp_pos_term + kd_pos_term;
arvindnr89 11:2553f5798f84 319 target_angle = CAP(target_angle, MAX_TARGET_ANGLE);
arvindnr89 12:16d1a5390022 320
arvindnr89 11:2553f5798f84 321 //CS ************ PD Stability CONTROLLER HERE ****************
arvindnr89 11:2553f5798f84 322 error = target_angle - angle;
arvindnr89 11:2553f5798f84 323 kp_term = Kp1 * error;
arvindnr89 11:2553f5798f84 324
arvindnr89 11:2553f5798f84 325 change_in_target_angle = target_angle - target_angle_old; //add
arvindnr89 11:2553f5798f84 326 change_in_angle = angle - angle_old2; //add
arvindnr89 11:2553f5798f84 327 kd_term = ((Kd1 * change_in_target_angle) - (Kd1*change_in_angle)) / dt;
arvindnr89 11:2553f5798f84 328 //kd_term = ((Kd1 * change_in_target_angle) - (Kd1*velocity));
arvindnr89 11:2553f5798f84 329
arvindnr89 11:2553f5798f84 330 //pc.printf("dAngle:%f\r\n", angle-angle_old1);
arvindnr89 11:2553f5798f84 331
arvindnr89 11:2553f5798f84 332 //Control Output
arvindnr89 12:16d1a5390022 333 control_output += kp_term + kd_term; //-100 to 100
arvindnr89 11:2553f5798f84 334 control_output = CAP(control_output, MAX_CONTROL_OUTPUT); // Limit max output from control
arvindnr89 11:2553f5798f84 335 motor1 = (int16_t)(control_output + (steering));
arvindnr89 11:2553f5798f84 336 motor2 = (int16_t)(control_output - (steering));
arvindnr89 11:2553f5798f84 337 motor1 = CAP(motor1, MAX_CONTROL_OUTPUT);
arvindnr89 11:2553f5798f84 338 motor2 = CAP(motor2, MAX_CONTROL_OUTPUT);
arvindnr89 11:2553f5798f84 339
arvindnr89 11:2553f5798f84 340 //Update variables
arvindnr89 11:2553f5798f84 341 target_angle_old = target_angle;
arvindnr89 11:2553f5798f84 342 angle_old2 = angle_old1;
arvindnr89 11:2553f5798f84 343 angle_old1 = angle;
arvindnr89 11:2553f5798f84 344
csharer 13:a18d41a60d0b 345 //Enable Motors
arvindnr89 11:2553f5798f84 346 enable = ENABLE;
arvindnr89 11:2553f5798f84 347 setMotor1Speed(-motor1);
arvindnr89 11:2553f5798f84 348 //setMotor1Speed(20);
arvindnr89 11:2553f5798f84 349 //setMotor2Speed(40);
arvindnr89 11:2553f5798f84 350 setMotor2Speed(-motor2);
arvindnr89 12:16d1a5390022 351 //robot_pos += (-motor1 + -motor2) / 2;
arvindnr89 11:2553f5798f84 352 //pc.printf("m1: %d m2: %d angle: %0.1f, controlout: %f tAngle: %f dt: %f timer: %d \r\n", motor1, motor2, angle, control_output, target_angle, dt, timer_value);
arvindnr89 11:2553f5798f84 353 } else { //[FALLEN}
arvindnr89 11:2553f5798f84 354 //Disable Motors
arvindnr89 11:2553f5798f84 355 enable = DISABLE;
arvindnr89 11:2553f5798f84 356
arvindnr89 11:2553f5798f84 357 //Set fallen flag
arvindnr89 11:2553f5798f84 358 fallen = true;
arvindnr89 11:2553f5798f84 359 }
arvindnr89 12:16d1a5390022 360
arvindnr89 12:16d1a5390022 361 if(loop_counter >= 20) {
arvindnr89 11:2553f5798f84 362 gpioMonitorC = 1;
arvindnr89 11:2553f5798f84 363 loop_counter = 0;
arvindnr89 12:16d1a5390022 364 pc.printf("Avg Vel: %f\r\n",velocity);
arvindnr89 11:2553f5798f84 365 //pc.printf("angle:%d Kp1: %0.3f Kd1: %0.2f Kp2: %0.2f Kd2: %0.3f tang: %0.2f dt:%d rob_pos: %d VE: %f\r\n", (int)angle, Kp1, Kd1, Kp2, Ki2, target_angle, dt, robot_pos, velocity_error);
arvindnr89 11:2553f5798f84 366 //pc.printf("Jstick_h: %d Jstick_v: %d Knob1 %d Knob2 %d Knob3 %d Knob4 %d Button: %d\r\n", jstick_h, jstick_v, knob1, knob2, knob3, knob4, button);
arvindnr89 11:2553f5798f84 367 //pc.printf("CAngle: %d, TAngle: %d,Pos: %d, dt: %d\r\n",(int)angle,target_angle,robot_pos,dt);
arvindnr89 11:2553f5798f84 368 //wait_us(200);
arvindnr89 11:2553f5798f84 369 gpioMonitorC = 0;
arvindnr89 12:16d1a5390022 370
arvindnr89 11:2553f5798f84 371 }
arvindnr89 11:2553f5798f84 372 gpioMonitorA = 0;
arvindnr89 11:2553f5798f84 373 } //end main loop
arvindnr89 11:2553f5798f84 374
arvindnr89 11:2553f5798f84 375 }
arvindnr89 11:2553f5798f84 376
arvindnr89 11:2553f5798f84 377 // ================================================================
arvindnr89 11:2553f5798f84 378 // === Communication Thread ===
arvindnr89 11:2553f5798f84 379 // ================================================================
arvindnr89 11:2553f5798f84 380 void communication_update_thread(void const *args)
arvindnr89 11:2553f5798f84 381 {
arvindnr89 11:2553f5798f84 382 pc.printf("Starting Communication Thread..\r\n");
arvindnr89 11:2553f5798f84 383 while(1) {
arvindnr89 11:2553f5798f84 384 //Recieve Data
arvindnr89 11:2553f5798f84 385 //gpioMonitorC = 1;
arvindnr89 11:2553f5798f84 386 rxLen = rf_receive(rxBuffer, 128);
arvindnr89 11:2553f5798f84 387 if(rxLen > 0) {
arvindnr89 11:2553f5798f84 388 led1 = led1^1;
arvindnr89 11:2553f5798f84 389 //Process data with our protocal
arvindnr89 11:2553f5798f84 390 communication_protocal(rxLen);
arvindnr89 11:2553f5798f84 391 }
arvindnr89 11:2553f5798f84 392 osDelay(30);
arvindnr89 11:2553f5798f84 393 //gpioMonitorC = 0;
arvindnr89 11:2553f5798f84 394 }
arvindnr89 11:2553f5798f84 395 }
arvindnr89 10:48f640a54401 396
csharer 9:fb671fa0c0be 397
csharer 3:2f76ffbc5cef 398 // ================================================================
csharer 3:2f76ffbc5cef 399 // === INITIAL SETUP ===
csharer 3:2f76ffbc5cef 400 // ================================================================
csharer 3:2f76ffbc5cef 401 void init_imu()
csharer 3:2f76ffbc5cef 402 {
arvindnr89 10:48f640a54401 403 pc.printf("Start IMU Initilization.. \r\n");
csharer 3:2f76ffbc5cef 404
csharer 3:2f76ffbc5cef 405 // Manual MPU initialization... accel=2G, gyro=2000º/s, filter=20Hz BW, output=200Hz
csharer 3:2f76ffbc5cef 406 mpu.setClockSource(MPU6050_CLOCK_PLL_ZGYRO);
csharer 3:2f76ffbc5cef 407 mpu.setFullScaleGyroRange(MPU6050_GYRO_FS_2000);
csharer 3:2f76ffbc5cef 408 mpu.setFullScaleAccelRange(MPU6050_ACCEL_FS_2);
csharer 3:2f76ffbc5cef 409 mpu.setDLPFMode(MPU6050_DLPF_BW_10); //10,20,42,98,188 // Default factor for BROBOT:10
csharer 4:2512939c10f0 410 mpu.setRate(4); // 0=1khz 1=500hz, 2=333hz, 3=250hz [4=200hz]default
csharer 3:2f76ffbc5cef 411 mpu.setSleepEnabled(false);
csharer 3:2f76ffbc5cef 412 wait_ms(500);
csharer 3:2f76ffbc5cef 413
csharer 3:2f76ffbc5cef 414 // load and configure the DMP
csharer 3:2f76ffbc5cef 415 devStatus = mpu.dmpInitialize();
csharer 3:2f76ffbc5cef 416 if(devStatus == 0) {
csharer 3:2f76ffbc5cef 417 mpu.setDMPEnabled(true);
csharer 3:2f76ffbc5cef 418 mpuIntStatus = mpu.getIntStatus();
csharer 3:2f76ffbc5cef 419 dmpReady = true;
csharer 4:2512939c10f0 420 } else {
csharer 3:2f76ffbc5cef 421 // 1 = initial memory load failed
csharer 3:2f76ffbc5cef 422 // 2 = DMP configuration updates failed
csharer 3:2f76ffbc5cef 423 pc.printf("DMP INIT error \r\n");
csharer 3:2f76ffbc5cef 424 }
csharer 3:2f76ffbc5cef 425
csharer 3:2f76ffbc5cef 426 //Gyro Calibration
csharer 3:2f76ffbc5cef 427 wait_ms(500);
arvindnr89 10:48f640a54401 428 pc.printf("Gyro calibration!! Dont move the robot in 1 second... \r\n");
csharer 3:2f76ffbc5cef 429 wait_ms(500);
csharer 4:2512939c10f0 430
csharer 3:2f76ffbc5cef 431 // verify connection
csharer 3:2f76ffbc5cef 432 pc.printf(mpu.testConnection() ? "Connection Good \r\n" : "Connection Failed\r\n");
csharer 3:2f76ffbc5cef 433
csharer 3:2f76ffbc5cef 434 //Adjust Sensor Fusion Gain
csharer 3:2f76ffbc5cef 435 dmpSetSensorFusionAccelGain(0x20);
csharer 3:2f76ffbc5cef 436
csharer 3:2f76ffbc5cef 437 wait_ms(200);
csharer 3:2f76ffbc5cef 438 mpu.resetFIFO();
csharer 3:2f76ffbc5cef 439 }
csharer 3:2f76ffbc5cef 440
arvindnr89 10:48f640a54401 441 void init_system()
arvindnr89 10:48f640a54401 442 {
arvindnr89 10:48f640a54401 443 initilizeAngle = true;
arvindnr89 10:48f640a54401 444 //Set the Channel. 0 is default, 15 is max
arvindnr89 10:48f640a54401 445 mrf.SetChannel(MRF_CHANNEL);
arvindnr89 10:48f640a54401 446 enable = DISABLE; //Disable Motors
arvindnr89 10:48f640a54401 447 pc.baud(115200);
arvindnr89 10:48f640a54401 448 pc.printf("Booting BroBot mbed RTOS..\r\n");
arvindnr89 10:48f640a54401 449
arvindnr89 10:48f640a54401 450
arvindnr89 10:48f640a54401 451 //Initilize the IMU
arvindnr89 10:48f640a54401 452 init_imu();
arvindnr89 10:48f640a54401 453 //Attach Interupt for IMU on rising edge of checkpin
arvindnr89 10:48f640a54401 454 checkpin.rise(&dmpDataReady);
arvindnr89 10:48f640a54401 455 pc.printf("IMU Initilized..\r\n");
arvindnr89 10:48f640a54401 456
arvindnr89 10:48f640a54401 457 //Init Stepper Motors
arvindnr89 10:48f640a54401 458 //Attach Motor Control Timer Call Back Functions
arvindnr89 10:48f640a54401 459 timer_M1.attach_us(&ISR1, ZERO_SPEED);//1s Period
arvindnr89 10:48f640a54401 460 timer_M2.attach_us(&ISR2, ZERO_SPEED);//1s Period
arvindnr89 10:48f640a54401 461 step_M1 = 1;
arvindnr89 10:48f640a54401 462 dir_M1 = 1;
arvindnr89 10:48f640a54401 463 pc.printf("Motors Initilized..\r\n");
arvindnr89 10:48f640a54401 464
arvindnr89 10:48f640a54401 465 //Timers initilized
arvindnr89 10:48f640a54401 466 timer.start();
arvindnr89 10:48f640a54401 467 timer_value = timer.read_us();
arvindnr89 10:48f640a54401 468
arvindnr89 10:48f640a54401 469 //Init GPIO Monitors
arvindnr89 10:48f640a54401 470 gpioMonitorA = 0;
arvindnr89 10:48f640a54401 471 gpioMonitorB = 0;
arvindnr89 10:48f640a54401 472 gpioMonitorC = 0;
arvindnr89 10:48f640a54401 473 gpioMonitorD = 0;
arvindnr89 10:48f640a54401 474
arvindnr89 11:2553f5798f84 475 enable = ENABLE; //Enable Motors
arvindnr89 10:48f640a54401 476 }
arvindnr89 10:48f640a54401 477
csharer 3:2f76ffbc5cef 478 // ================================================================
csharer 3:2f76ffbc5cef 479 // === MAIN PROGRAM LOOP ===
csharer 3:2f76ffbc5cef 480 // ================================================================
arvindnr89 11:2553f5798f84 481
csharer 4:2512939c10f0 482
csharer 3:2f76ffbc5cef 483 int main()
csharer 3:2f76ffbc5cef 484 {
csharer 9:fb671fa0c0be 485
arvindnr89 10:48f640a54401 486 init_system();
csharer 4:2512939c10f0 487
arvindnr89 10:48f640a54401 488 //Create IMU Thread
arvindnr89 11:2553f5798f84 489 imu_update_thread_ID = osThreadCreate(osThread(imu_update_thread), NULL);
csharer 9:fb671fa0c0be 490
arvindnr89 11:2553f5798f84 491 //Create PID Thread
arvindnr89 11:2553f5798f84 492 pid_update_thread_ID = osThreadCreate(osThread(pid_update_thread), NULL);
csharer 9:fb671fa0c0be 493
arvindnr89 11:2553f5798f84 494 //Create Communication Thread
arvindnr89 11:2553f5798f84 495 communication_update_thread_ID = osThreadCreate(osThread(communication_update_thread), NULL);
csharer 4:2512939c10f0 496
arvindnr89 10:48f640a54401 497
csharer 4:2512939c10f0 498
csharer 3:2f76ffbc5cef 499 } //End Main()