BroBot Code for ESE350 Lab6 part 3 (Skeleton)
Dependencies: MPU6050_V3 mbed-rtos mbed
Fork of BroBot_RTOS_ESE350 by
main.cpp@19:19d72dc64b43, 2017-03-22 (annotated)
- Committer:
- csharer
- Date:
- Wed Mar 22 21:41:07 2017 +0000
- Revision:
- 19:19d72dc64b43
- Parent:
- 18:ee252b8f7430
- Child:
- 20:a7cba632d0b1
Further Clean up for ESE350 (FULL IMPLEMENTATION)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
arvindnr89 | 17:8e2824f64b91 | 1 | /* |
arvindnr89 | 17:8e2824f64b91 | 2 | BroBOT Running the mBed RTOS |
arvindnr89 | 17:8e2824f64b91 | 3 | Authors: Arvind Ramesh and Carter Sharer |
arvindnr89 | 17:8e2824f64b91 | 4 | */ |
arvindnr89 | 17:8e2824f64b91 | 5 | |
csharer | 9:fb671fa0c0be | 6 | /******************************* README USAGE ******************************* |
csharer | 9:fb671fa0c0be | 7 | * This robot must be powered on while it is laying down flat on a still table |
csharer | 9:fb671fa0c0be | 8 | * This allows the robot to calibrate the IMU (~5 seconds) |
csharer | 9:fb671fa0c0be | 9 | * The motors are DISABLED when the robot tilts more then +-45 degrees from |
csharer | 19:19d72dc64b43 | 10 | * vertical. To ENABLE the motors you must lift the robot to 0 degres and |
csharer | 9:fb671fa0c0be | 11 | * press the joystick button. |
csharer | 9:fb671fa0c0be | 12 | * To reset the motor positions you must press the josystick button anytime. |
csharer | 9:fb671fa0c0be | 13 | ******************************************************************************/ |
csharer | 3:2f76ffbc5cef | 14 | |
arvindnr89 | 10:48f640a54401 | 15 | //Controller Values |
arvindnr89 | 10:48f640a54401 | 16 | float knob1, knob2, knob3, knob4; |
arvindnr89 | 10:48f640a54401 | 17 | float jstick_h, jstick_v; |
arvindnr89 | 10:48f640a54401 | 18 | |
arvindnr89 | 10:48f640a54401 | 19 | //Button |
arvindnr89 | 10:48f640a54401 | 20 | bool button; |
arvindnr89 | 10:48f640a54401 | 21 | |
csharer | 4:2512939c10f0 | 22 | //BroBot Begin |
csharer | 9:fb671fa0c0be | 23 | #include "cmsis_os.h" |
arvindnr89 | 11:2553f5798f84 | 24 | #include "rtos_definations.h" |
csharer | 3:2f76ffbc5cef | 25 | #include "pin_assignments.h" |
csharer | 3:2f76ffbc5cef | 26 | #include "I2Cdev.h" |
csharer | 3:2f76ffbc5cef | 27 | #include "JJ_MPU6050_DMP_6Axis.h" |
csharer | 3:2f76ffbc5cef | 28 | #include "BroBot.h" |
csharer | 3:2f76ffbc5cef | 29 | #include "BroBot_IMU.h" |
csharer | 3:2f76ffbc5cef | 30 | #include "stepper_motors.h" |
csharer | 4:2512939c10f0 | 31 | #include "MRF24J40.h" |
arvindnr89 | 10:48f640a54401 | 32 | #include "communication.h" |
csharer | 4:2512939c10f0 | 33 | |
csharer | 9:fb671fa0c0be | 34 | //Angle Offset is used to set the natural balance point of your robot. |
csharer | 18:ee252b8f7430 | 35 | //You should adjust this offset so that your robots balance point is near 0 |
csharer | 19:19d72dc64b43 | 36 | #define ANGLE_OFFSET 107 //THIS NEEDS TO BE CHANGED FOR YOUR ROBOT |
csharer | 19:19d72dc64b43 | 37 | #define THETA_OFFSET 0 |
csharer | 18:ee252b8f7430 | 38 | |
csharer | 18:ee252b8f7430 | 39 | //Set Channel to (Group Number - 1). Possible Channels are 0-15 |
arvindnr89 | 10:48f640a54401 | 40 | #define MRF_CHANNEL 2 |
csharer | 6:62cdb7482b50 | 41 | |
csharer | 4:2512939c10f0 | 42 | //Knobs |
csharer | 4:2512939c10f0 | 43 | #define POT1 p17 |
csharer | 4:2512939c10f0 | 44 | #define POT2 p18 |
csharer | 4:2512939c10f0 | 45 | #define POT3 p16 |
csharer | 4:2512939c10f0 | 46 | #define POT4 p15 |
csharer | 4:2512939c10f0 | 47 | //JoyStick |
csharer | 4:2512939c10f0 | 48 | #define POTV p19 |
csharer | 4:2512939c10f0 | 49 | #define POTH p20 |
csharer | 3:2f76ffbc5cef | 50 | |
csharer | 3:2f76ffbc5cef | 51 | //PID |
csharer | 3:2f76ffbc5cef | 52 | #define MAX_THROTTLE 580 |
csharer | 3:2f76ffbc5cef | 53 | #define MAX_STEERING 150 |
csharer | 19:19d72dc64b43 | 54 | #define MAX_TARGET_ANGLE 12 |
csharer | 3:2f76ffbc5cef | 55 | |
csharer | 19:19d72dc64b43 | 56 | //Joystick Control |
csharer | 19:19d72dc64b43 | 57 | #define THROTTLE_DAMPNER 30 |
csharer | 19:19d72dc64b43 | 58 | #define STREEING_DAMPNER 10 |
arvindnr89 | 17:8e2824f64b91 | 59 | #define VELOCITY_SAMPLES 10 |
arvindnr89 | 17:8e2824f64b91 | 60 | |
arvindnr89 | 10:48f640a54401 | 61 | //*********** Local Function Definations BEGIN **************// |
arvindnr89 | 10:48f640a54401 | 62 | void init_system(); |
arvindnr89 | 10:48f640a54401 | 63 | void init_imu(); |
arvindnr89 | 10:48f640a54401 | 64 | //*********** Local Function Definations END **************// |
arvindnr89 | 10:48f640a54401 | 65 | |
csharer | 3:2f76ffbc5cef | 66 | Timer timer; |
arvindnr89 | 12:16d1a5390022 | 67 | int timer_value = 0; |
arvindnr89 | 12:16d1a5390022 | 68 | int timer_old = 0; |
arvindnr89 | 15:d6d7623a17f8 | 69 | float angle = 0; |
arvindnr89 | 16:3a85662fb740 | 70 | float theta = 0; |
arvindnr89 | 16:3a85662fb740 | 71 | float totalTheta = 0; |
arvindnr89 | 16:3a85662fb740 | 72 | float deltaTheta = 0; |
arvindnr89 | 12:16d1a5390022 | 73 | float velocitySamples[VELOCITY_SAMPLES + 1] = {0.0}; |
csharer | 3:2f76ffbc5cef | 74 | |
arvindnr89 | 16:3a85662fb740 | 75 | float target_pos = 0; |
arvindnr89 | 17:8e2824f64b91 | 76 | float pos_error = 0; |
arvindnr89 | 16:3a85662fb740 | 77 | |
csharer | 3:2f76ffbc5cef | 78 | Serial pc(USBTX, USBRX); |
csharer | 3:2f76ffbc5cef | 79 | |
csharer | 4:2512939c10f0 | 80 | // LEDs |
csharer | 4:2512939c10f0 | 81 | DigitalOut led1(LED1); |
csharer | 4:2512939c10f0 | 82 | DigitalOut led2(LED2); |
csharer | 4:2512939c10f0 | 83 | DigitalOut led3(LED3); |
csharer | 4:2512939c10f0 | 84 | DigitalOut led4(LED4); |
csharer | 4:2512939c10f0 | 85 | |
arvindnr89 | 10:48f640a54401 | 86 | //Used to set angle upon startup |
arvindnr89 | 17:8e2824f64b91 | 87 | bool initilizeAngle; |
arvindnr89 | 17:8e2824f64b91 | 88 | bool initilizeTheta; |
arvindnr89 | 11:2553f5798f84 | 89 | |
csharer | 9:fb671fa0c0be | 90 | // ================================================================ |
arvindnr89 | 10:48f640a54401 | 91 | // === IMU Thread === |
csharer | 9:fb671fa0c0be | 92 | // ================================================================ |
arvindnr89 | 10:48f640a54401 | 93 | void imu_update_thread(void const *args) |
arvindnr89 | 15:d6d7623a17f8 | 94 | { |
arvindnr89 | 15:d6d7623a17f8 | 95 | float dAngle = 0; |
arvindnr89 | 16:3a85662fb740 | 96 | float dTheta = 0; |
arvindnr89 | 15:d6d7623a17f8 | 97 | float new_angle = 0; |
arvindnr89 | 16:3a85662fb740 | 98 | float new_theta = 0; |
arvindnr89 | 15:d6d7623a17f8 | 99 | long long timeOut = 0; |
arvindnr89 | 15:d6d7623a17f8 | 100 | |
arvindnr89 | 11:2553f5798f84 | 101 | pc.printf("Starting IMU Thread..\r\n"); |
arvindnr89 | 11:2553f5798f84 | 102 | while (1) { |
arvindnr89 | 11:2553f5798f84 | 103 | osSignalWait(IMU_UPDATE_SIGNAL,osWaitForever); |
arvindnr89 | 11:2553f5798f84 | 104 | if(mpuInterrupt) { |
arvindnr89 | 10:48f640a54401 | 105 | mpuInterrupt = false; |
csharer | 9:fb671fa0c0be | 106 | led3 = !led3; |
csharer | 9:fb671fa0c0be | 107 | /********************* All IMU Handling DO NOT MODIFY *****************/ |
csharer | 9:fb671fa0c0be | 108 | mpuIntStatus = mpu.getIntStatus(); |
csharer | 9:fb671fa0c0be | 109 | |
csharer | 9:fb671fa0c0be | 110 | //get current FIFO count |
csharer | 9:fb671fa0c0be | 111 | fifoCount = mpu.getFIFOCount(); |
csharer | 9:fb671fa0c0be | 112 | |
csharer | 9:fb671fa0c0be | 113 | // check for overflow (this should never happen unless our code is too inefficient) |
csharer | 9:fb671fa0c0be | 114 | if ((mpuIntStatus & 0x10) || fifoCount == 1024) { |
csharer | 9:fb671fa0c0be | 115 | // reset so we can continue cleanly |
csharer | 9:fb671fa0c0be | 116 | mpu.resetFIFO(); |
arvindnr89 | 10:48f640a54401 | 117 | pc.printf("MPU FIFO overflow!"); |
csharer | 9:fb671fa0c0be | 118 | //otherwise, check for DMP data ready interrupt (this should happen frequently) |
csharer | 9:fb671fa0c0be | 119 | } else if (mpuIntStatus & 0x02) { |
csharer | 9:fb671fa0c0be | 120 | //wait for correct available data length, should be a VERY short wait |
arvindnr89 | 15:d6d7623a17f8 | 121 | timeOut = 0; |
arvindnr89 | 15:d6d7623a17f8 | 122 | while (fifoCount < packetSize) { |
arvindnr89 | 15:d6d7623a17f8 | 123 | timeOut++; |
arvindnr89 | 15:d6d7623a17f8 | 124 | fifoCount = mpu.getFIFOCount(); |
arvindnr89 | 15:d6d7623a17f8 | 125 | if(timeOut > 100000000){ |
arvindnr89 | 15:d6d7623a17f8 | 126 | break; |
arvindnr89 | 15:d6d7623a17f8 | 127 | } |
arvindnr89 | 15:d6d7623a17f8 | 128 | } |
csharer | 9:fb671fa0c0be | 129 | |
csharer | 9:fb671fa0c0be | 130 | //read a packet from FIFO |
csharer | 9:fb671fa0c0be | 131 | mpu.getFIFOBytes(fifoBuffer, packetSize); |
csharer | 9:fb671fa0c0be | 132 | |
csharer | 9:fb671fa0c0be | 133 | //track FIFO count here in case there is > 1 packet available |
csharer | 9:fb671fa0c0be | 134 | //(this lets us immediately read more without waiting for an interrupt) |
csharer | 9:fb671fa0c0be | 135 | fifoCount -= packetSize; |
csharer | 9:fb671fa0c0be | 136 | |
csharer | 9:fb671fa0c0be | 137 | //Read new angle from IMU |
arvindnr89 | 16:3a85662fb740 | 138 | dmpGetReadings(&new_angle,&new_theta); |
arvindnr89 | 15:d6d7623a17f8 | 139 | new_angle = (float)(new_angle - ANGLE_OFFSET); |
arvindnr89 | 16:3a85662fb740 | 140 | new_theta = float(new_theta + THETA_OFFSET); |
arvindnr89 | 15:d6d7623a17f8 | 141 | |
arvindnr89 | 10:48f640a54401 | 142 | //Calculate the delta angle |
csharer | 9:fb671fa0c0be | 143 | dAngle = new_angle - angle; |
arvindnr89 | 16:3a85662fb740 | 144 | dTheta = new_theta - theta; |
csharer | 9:fb671fa0c0be | 145 | |
csharer | 9:fb671fa0c0be | 146 | //Filter out angle readings larger then MAX_ANGLE_DELTA |
arvindnr89 | 10:48f640a54401 | 147 | if(initilizeAngle) { |
csharer | 9:fb671fa0c0be | 148 | angle = new_angle; |
arvindnr89 | 10:48f640a54401 | 149 | initilizeAngle = false; |
arvindnr89 | 17:8e2824f64b91 | 150 | pc.printf("\t\t\t Setting Initial Value: %f\r\n",angle); |
arvindnr89 | 10:48f640a54401 | 151 | } else if( ((dAngle < 15) && (dAngle > -15))) { |
arvindnr89 | 10:48f640a54401 | 152 | angle = new_angle; |
arvindnr89 | 16:3a85662fb740 | 153 | theta = new_theta; |
csharer | 9:fb671fa0c0be | 154 | } else { |
arvindnr89 | 17:8e2824f64b91 | 155 | pc.printf("\t\t\t Delta Angle too Large: %f. Ignored \r\n",dAngle); |
arvindnr89 | 17:8e2824f64b91 | 156 | pc.printf("\t\t\t New Angle: %f Old Angle: %f\r\n",new_angle,angle); |
csharer | 9:fb671fa0c0be | 157 | } |
arvindnr89 | 16:3a85662fb740 | 158 | |
arvindnr89 | 16:3a85662fb740 | 159 | if(initilizeTheta) { |
arvindnr89 | 16:3a85662fb740 | 160 | theta = new_theta; |
arvindnr89 | 16:3a85662fb740 | 161 | initilizeTheta = false; |
arvindnr89 | 16:3a85662fb740 | 162 | } else if( ((dTheta < 15) && (dTheta > -15))) { |
arvindnr89 | 16:3a85662fb740 | 163 | theta = new_theta; |
arvindnr89 | 16:3a85662fb740 | 164 | } else { |
arvindnr89 | 17:8e2824f64b91 | 165 | //pc.printf("\t\t\t Delta Theta too Large: %f. Ignored \r\n",dTheta); |
arvindnr89 | 17:8e2824f64b91 | 166 | //pc.printf("\t\t\t New Theta: %f Old Theta: %f\r\n",new_theta,theta); |
arvindnr89 | 16:3a85662fb740 | 167 | theta = new_theta; |
arvindnr89 | 16:3a85662fb740 | 168 | dTheta = dTheta > 0 ? -1 : 1; |
arvindnr89 | 16:3a85662fb740 | 169 | dTheta = 1; |
arvindnr89 | 16:3a85662fb740 | 170 | } |
arvindnr89 | 16:3a85662fb740 | 171 | totalTheta += dTheta; |
csharer | 9:fb671fa0c0be | 172 | } |
arvindnr89 | 15:d6d7623a17f8 | 173 | else{ |
arvindnr89 | 17:8e2824f64b91 | 174 | pc.printf("\t\t\t IMU Error. MPU Status: %d!!\r\n",mpuIntStatus); |
arvindnr89 | 15:d6d7623a17f8 | 175 | } |
csharer | 9:fb671fa0c0be | 176 | /********************* All IMU Handling DO NOT MODIFY *****************/ |
arvindnr89 | 10:48f640a54401 | 177 | }//End of if(mpuInterrupt) loop |
arvindnr89 | 11:2553f5798f84 | 178 | osSignalClear(imu_update_thread_ID,IMU_UPDATE_SIGNAL); |
arvindnr89 | 17:8e2824f64b91 | 179 | osSignalSet(pid_update_thread_ID,PID_UPDATE_SIGNAL); |
arvindnr89 | 10:48f640a54401 | 180 | } |
arvindnr89 | 10:48f640a54401 | 181 | } |
csharer | 9:fb671fa0c0be | 182 | |
arvindnr89 | 11:2553f5798f84 | 183 | // ================================================================ |
arvindnr89 | 11:2553f5798f84 | 184 | // === PID Thread === |
arvindnr89 | 11:2553f5798f84 | 185 | // ================================================================ |
arvindnr89 | 11:2553f5798f84 | 186 | void pid_update_thread(void const *args) |
arvindnr89 | 11:2553f5798f84 | 187 | { |
arvindnr89 | 12:16d1a5390022 | 188 | float Kp1 = 0; |
arvindnr89 | 12:16d1a5390022 | 189 | float Kd1 = 0; |
arvindnr89 | 12:16d1a5390022 | 190 | float Kp2 = 0; |
arvindnr89 | 12:16d1a5390022 | 191 | float Kd2 = 0; |
arvindnr89 | 12:16d1a5390022 | 192 | float target_angle = 0; |
arvindnr89 | 12:16d1a5390022 | 193 | float target_angle_old = 0; |
arvindnr89 | 12:16d1a5390022 | 194 | float change_in_target_angle = 0; |
arvindnr89 | 12:16d1a5390022 | 195 | float change_in_angle = 0; |
arvindnr89 | 12:16d1a5390022 | 196 | float angle_old1 = 0; |
arvindnr89 | 12:16d1a5390022 | 197 | float angle_old2 = 0; |
arvindnr89 | 12:16d1a5390022 | 198 | float kp_term = 0; |
arvindnr89 | 12:16d1a5390022 | 199 | float kd_term = 0; |
arvindnr89 | 12:16d1a5390022 | 200 | float throttle = 0; |
arvindnr89 | 12:16d1a5390022 | 201 | float steering = 0; |
arvindnr89 | 12:16d1a5390022 | 202 | float control_output = 0; |
arvindnr89 | 12:16d1a5390022 | 203 | int robot_pos = 0; |
arvindnr89 | 17:8e2824f64b91 | 204 | //int loop_counter = 0; |
arvindnr89 | 12:16d1a5390022 | 205 | int velocitySamplesCounter = 0; |
arvindnr89 | 12:16d1a5390022 | 206 | int dt = 0; |
arvindnr89 | 12:16d1a5390022 | 207 | float error = 0; |
arvindnr89 | 12:16d1a5390022 | 208 | float velocity = 0; |
arvindnr89 | 12:16d1a5390022 | 209 | bool fallen = true; |
arvindnr89 | 12:16d1a5390022 | 210 | |
arvindnr89 | 12:16d1a5390022 | 211 | // --- For Position controller --- // |
arvindnr89 | 12:16d1a5390022 | 212 | float kp_pos_term = 0; |
arvindnr89 | 12:16d1a5390022 | 213 | float kd_pos_term = 0; |
arvindnr89 | 12:16d1a5390022 | 214 | float change_in_target_pos = 0; |
arvindnr89 | 16:3a85662fb740 | 215 | |
csharer | 18:ee252b8f7430 | 216 | // -- For Velocity Controller --- // |
arvindnr89 | 12:16d1a5390022 | 217 | float target_pos_old = 0; |
csharer | 14:94c65d1c8bad | 218 | float target_velocity = 0; |
arvindnr89 | 12:16d1a5390022 | 219 | float runningSumReplaceVal = 0; |
arvindnr89 | 12:16d1a5390022 | 220 | float newVelocity = 0; |
arvindnr89 | 12:16d1a5390022 | 221 | float velocitySum = 0; |
arvindnr89 | 12:16d1a5390022 | 222 | memset(velocitySamples,0,sizeof(velocitySamples)); |
csharer | 18:ee252b8f7430 | 223 | |
arvindnr89 | 11:2553f5798f84 | 224 | pc.printf("Starting PID Thread..\r\n"); |
arvindnr89 | 11:2553f5798f84 | 225 | while(1) { |
arvindnr89 | 17:8e2824f64b91 | 226 | osSignalWait(PID_UPDATE_SIGNAL,osWaitForever); |
csharer | 18:ee252b8f7430 | 227 | |
csharer | 18:ee252b8f7430 | 228 | //Button Press on the remote initilizes the robot position. |
arvindnr89 | 15:d6d7623a17f8 | 229 | if(button) { |
arvindnr89 | 15:d6d7623a17f8 | 230 | pos_M1 = 0; |
arvindnr89 | 15:d6d7623a17f8 | 231 | pos_M2 = 0; |
arvindnr89 | 15:d6d7623a17f8 | 232 | target_pos = 0; |
arvindnr89 | 15:d6d7623a17f8 | 233 | motor1 = 0; |
arvindnr89 | 15:d6d7623a17f8 | 234 | motor2 = 0; |
arvindnr89 | 15:d6d7623a17f8 | 235 | control_output = 0; |
arvindnr89 | 15:d6d7623a17f8 | 236 | fallen = false; |
arvindnr89 | 15:d6d7623a17f8 | 237 | } |
csharer | 18:ee252b8f7430 | 238 | |
arvindnr89 | 11:2553f5798f84 | 239 | //Get the time stamp as soon as it enters the loop |
arvindnr89 | 11:2553f5798f84 | 240 | timer_value = timer.read_us(); |
arvindnr89 | 11:2553f5798f84 | 241 | led4 = !fallen; |
arvindnr89 | 11:2553f5798f84 | 242 | led2 = button; |
csharer | 9:fb671fa0c0be | 243 | |
csharer | 18:ee252b8f7430 | 244 | //Set Gainz With Knobs |
csharer | 18:ee252b8f7430 | 245 | Kp1 = ((float)knob1); |
csharer | 18:ee252b8f7430 | 246 | Kd1 = ((float)knob2); |
csharer | 18:ee252b8f7430 | 247 | Kp2 = ((float)knob3); |
csharer | 18:ee252b8f7430 | 248 | Kd2 = ((float)knob4); |
arvindnr89 | 11:2553f5798f84 | 249 | |
csharer | 18:ee252b8f7430 | 250 | //Joystick Control |
csharer | 18:ee252b8f7430 | 251 | throttle = (float)jstick_v / THROTTLE_DAMPNER; |
csharer | 18:ee252b8f7430 | 252 | steering = (float)jstick_h / STREEING_DAMPNER; |
arvindnr89 | 11:2553f5798f84 | 253 | |
arvindnr89 | 11:2553f5798f84 | 254 | //Calculate the delta time |
arvindnr89 | 11:2553f5798f84 | 255 | dt = (timer_value - timer_old); |
arvindnr89 | 11:2553f5798f84 | 256 | timer_old = timer_value; |
csharer | 19:19d72dc64b43 | 257 | //angle_old = angle; |
arvindnr89 | 17:8e2824f64b91 | 258 | |
arvindnr89 | 11:2553f5798f84 | 259 | // STANDING: Motor Control Enabled |
arvindnr89 | 11:2553f5798f84 | 260 | //******************** PID Control BEGIN ********************** // |
arvindnr89 | 11:2553f5798f84 | 261 | if(((angle < 45) && (angle > -45)) && (fallen == false)) { |
arvindnr89 | 11:2553f5798f84 | 262 | |
arvindnr89 | 11:2553f5798f84 | 263 | //Robot Position |
arvindnr89 | 11:2553f5798f84 | 264 | robot_pos = (pos_M1 + pos_M2) / 2; |
csharer | 19:19d72dc64b43 | 265 | |
csharer | 19:19d72dc64b43 | 266 | //Target Position Incremented with Joystick |
csharer | 19:19d72dc64b43 | 267 | target_pos += throttle; |
arvindnr89 | 12:16d1a5390022 | 268 | |
csharer | 18:ee252b8f7430 | 269 | //*************** Velocity Computation ********************** |
arvindnr89 | 12:16d1a5390022 | 270 | velocitySamplesCounter++; |
arvindnr89 | 12:16d1a5390022 | 271 | if(velocitySamplesCounter == VELOCITY_SAMPLES){ |
arvindnr89 | 12:16d1a5390022 | 272 | velocitySamplesCounter = 0; |
arvindnr89 | 12:16d1a5390022 | 273 | } |
arvindnr89 | 12:16d1a5390022 | 274 | runningSumReplaceVal = velocitySamples[velocitySamplesCounter]; //value to replace |
arvindnr89 | 12:16d1a5390022 | 275 | newVelocity = (motor1 + motor2) / 2; |
arvindnr89 | 12:16d1a5390022 | 276 | velocitySamples[velocitySamplesCounter] = newVelocity; //replace value with |
arvindnr89 | 12:16d1a5390022 | 277 | velocitySum = velocitySum - runningSumReplaceVal + newVelocity; |
arvindnr89 | 12:16d1a5390022 | 278 | velocity = velocitySum/VELOCITY_SAMPLES; |
csharer | 14:94c65d1c8bad | 279 | |
csharer | 19:19d72dc64b43 | 280 | // ***************** Position Controller ********************* |
csharer | 19:19d72dc64b43 | 281 | //Inputs: robot_position, target_pos |
csharer | 19:19d72dc64b43 | 282 | //Error: distance from target position |
csharer | 19:19d72dc64b43 | 283 | //Output: target_angle |
arvindnr89 | 15:d6d7623a17f8 | 284 | |
csharer | 18:ee252b8f7430 | 285 | float Kp1P = Kp2/1000; |
csharer | 18:ee252b8f7430 | 286 | float Kd1P = Kd2/100; |
csharer | 19:19d72dc64b43 | 287 | |
csharer | 19:19d72dc64b43 | 288 | //Calculate the Position Error using robot_pos and target_pos |
arvindnr89 | 11:2553f5798f84 | 289 | pos_error = robot_pos - target_pos; //robot_pos - target_pos; |
arvindnr89 | 11:2553f5798f84 | 290 | |
csharer | 19:19d72dc64b43 | 291 | //Calculate the Proportional Term (P Term) Using The Error |
arvindnr89 | 15:d6d7623a17f8 | 292 | kp_pos_term = Kp1P * pos_error; |
arvindnr89 | 11:2553f5798f84 | 293 | |
csharer | 19:19d72dc64b43 | 294 | //Calculate the Derivative Term (D Term) Using The Error |
arvindnr89 | 11:2553f5798f84 | 295 | change_in_target_pos = target_pos - target_pos_old; |
arvindnr89 | 15:d6d7623a17f8 | 296 | kd_pos_term = ((Kd1P * change_in_target_pos) + (Kd1P*velocity)); |
csharer | 18:ee252b8f7430 | 297 | target_velocity = kp_pos_term + -kd_pos_term; |
csharer | 14:94c65d1c8bad | 298 | target_velocity = CAP(target_velocity, 100); |
csharer | 18:ee252b8f7430 | 299 | target_angle = -target_velocity; |
csharer | 19:19d72dc64b43 | 300 | |
csharer | 19:19d72dc64b43 | 301 | //************ PD Stability CONTROLLER HERE **************** |
csharer | 19:19d72dc64b43 | 302 | //Inputs: angle, target_angle |
csharer | 19:19d72dc64b43 | 303 | //Error: distance from target_angle |
csharer | 19:19d72dc64b43 | 304 | //Output: control_output (motor speed) |
csharer | 18:ee252b8f7430 | 305 | |
csharer | 19:19d72dc64b43 | 306 | //Scale the Knob Values; |
csharer | 19:19d72dc64b43 | 307 | float Kp1S = Kp1/100; |
csharer | 19:19d72dc64b43 | 308 | float Kd1S = Kd1*100; |
arvindnr89 | 12:16d1a5390022 | 309 | |
csharer | 19:19d72dc64b43 | 310 | //Calculate the Angle Error Using target_angle and angle |
csharer | 19:19d72dc64b43 | 311 | error = target_angle - angle; |
csharer | 18:ee252b8f7430 | 312 | |
csharer | 19:19d72dc64b43 | 313 | //Calculate the Proportional Term (P term) Using the Error |
csharer | 14:94c65d1c8bad | 314 | kp_term = Kp1S * error; |
arvindnr89 | 11:2553f5798f84 | 315 | |
csharer | 19:19d72dc64b43 | 316 | //Calculate the Derivatve Term (D term) |
arvindnr89 | 11:2553f5798f84 | 317 | change_in_target_angle = target_angle - target_angle_old; //add |
arvindnr89 | 11:2553f5798f84 | 318 | change_in_angle = angle - angle_old2; //add |
csharer | 14:94c65d1c8bad | 319 | kd_term = ((Kd1S * change_in_target_angle) - (Kd1S*change_in_angle)) / dt; |
arvindnr89 | 11:2553f5798f84 | 320 | //kd_term = ((Kd1 * change_in_target_angle) - (Kd1*velocity)); |
arvindnr89 | 11:2553f5798f84 | 321 | |
csharer | 19:19d72dc64b43 | 322 | //Calculate the Control Output |
csharer | 18:ee252b8f7430 | 323 | control_output += kp_term + kd_term; //-100 to 100 |
csharer | 18:ee252b8f7430 | 324 | control_output = CAP(control_output, MAX_CONTROL_OUTPUT); // Limit max output from control |
csharer | 18:ee252b8f7430 | 325 | |
csharer | 18:ee252b8f7430 | 326 | //*************** Set Motor Speed ************************* |
csharer | 19:19d72dc64b43 | 327 | motor1 = (int16_t)(control_output + (steering)); |
csharer | 19:19d72dc64b43 | 328 | motor2 = (int16_t)(control_output - (steering)); |
arvindnr89 | 11:2553f5798f84 | 329 | motor1 = CAP(motor1, MAX_CONTROL_OUTPUT); |
arvindnr89 | 11:2553f5798f84 | 330 | motor2 = CAP(motor2, MAX_CONTROL_OUTPUT); |
csharer | 18:ee252b8f7430 | 331 | setMotor1Speed(-motor1); |
csharer | 18:ee252b8f7430 | 332 | setMotor2Speed(-motor2); |
arvindnr89 | 11:2553f5798f84 | 333 | |
arvindnr89 | 11:2553f5798f84 | 334 | //Update variables |
arvindnr89 | 11:2553f5798f84 | 335 | target_angle_old = target_angle; |
arvindnr89 | 11:2553f5798f84 | 336 | angle_old2 = angle_old1; |
arvindnr89 | 11:2553f5798f84 | 337 | angle_old1 = angle; |
arvindnr89 | 11:2553f5798f84 | 338 | |
csharer | 18:ee252b8f7430 | 339 | //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); |
csharer | 18:ee252b8f7430 | 340 | |
csharer | 13:a18d41a60d0b | 341 | //Enable Motors |
arvindnr89 | 11:2553f5798f84 | 342 | enable = ENABLE; |
arvindnr89 | 16:3a85662fb740 | 343 | |
arvindnr89 | 16:3a85662fb740 | 344 | if(abs(motor1) == MAX_CONTROL_OUTPUT || abs(motor2) == MAX_CONTROL_OUTPUT) { |
arvindnr89 | 17:8e2824f64b91 | 345 | pc.printf("Max Speed Reached. Killing the Robot\n"); |
arvindnr89 | 17:8e2824f64b91 | 346 | enable = DISABLE; |
arvindnr89 | 16:3a85662fb740 | 347 | fallen = true; |
arvindnr89 | 16:3a85662fb740 | 348 | } |
arvindnr89 | 11:2553f5798f84 | 349 | } else { //[FALLEN} |
arvindnr89 | 11:2553f5798f84 | 350 | //Disable Motors |
arvindnr89 | 11:2553f5798f84 | 351 | enable = DISABLE; |
arvindnr89 | 11:2553f5798f84 | 352 | |
arvindnr89 | 11:2553f5798f84 | 353 | //Set fallen flag |
arvindnr89 | 11:2553f5798f84 | 354 | fallen = true; |
arvindnr89 | 11:2553f5798f84 | 355 | } |
csharer | 18:ee252b8f7430 | 356 | osSignalClear(pid_update_thread_ID,PID_UPDATE_SIGNAL); |
arvindnr89 | 11:2553f5798f84 | 357 | } //end main loop |
arvindnr89 | 11:2553f5798f84 | 358 | } |
arvindnr89 | 11:2553f5798f84 | 359 | |
arvindnr89 | 11:2553f5798f84 | 360 | // ================================================================ |
arvindnr89 | 11:2553f5798f84 | 361 | // === Communication Thread === |
arvindnr89 | 11:2553f5798f84 | 362 | // ================================================================ |
arvindnr89 | 11:2553f5798f84 | 363 | void communication_update_thread(void const *args) |
arvindnr89 | 11:2553f5798f84 | 364 | { |
arvindnr89 | 11:2553f5798f84 | 365 | pc.printf("Starting Communication Thread..\r\n"); |
arvindnr89 | 11:2553f5798f84 | 366 | while(1) { |
arvindnr89 | 11:2553f5798f84 | 367 | //Recieve Data |
arvindnr89 | 11:2553f5798f84 | 368 | rxLen = rf_receive(rxBuffer, 128); |
arvindnr89 | 11:2553f5798f84 | 369 | if(rxLen > 0) { |
arvindnr89 | 11:2553f5798f84 | 370 | led1 = led1^1; |
arvindnr89 | 11:2553f5798f84 | 371 | //Process data with our protocal |
arvindnr89 | 11:2553f5798f84 | 372 | communication_protocal(rxLen); |
arvindnr89 | 11:2553f5798f84 | 373 | } |
arvindnr89 | 11:2553f5798f84 | 374 | } |
arvindnr89 | 11:2553f5798f84 | 375 | } |
arvindnr89 | 10:48f640a54401 | 376 | |
csharer | 3:2f76ffbc5cef | 377 | // ================================================================ |
csharer | 3:2f76ffbc5cef | 378 | // === INITIAL SETUP === |
csharer | 3:2f76ffbc5cef | 379 | // ================================================================ |
csharer | 3:2f76ffbc5cef | 380 | void init_imu() |
csharer | 3:2f76ffbc5cef | 381 | { |
arvindnr89 | 10:48f640a54401 | 382 | pc.printf("Start IMU Initilization.. \r\n"); |
csharer | 3:2f76ffbc5cef | 383 | |
csharer | 3:2f76ffbc5cef | 384 | // Manual MPU initialization... accel=2G, gyro=2000º/s, filter=20Hz BW, output=200Hz |
csharer | 3:2f76ffbc5cef | 385 | mpu.setClockSource(MPU6050_CLOCK_PLL_ZGYRO); |
csharer | 3:2f76ffbc5cef | 386 | mpu.setFullScaleGyroRange(MPU6050_GYRO_FS_2000); |
csharer | 3:2f76ffbc5cef | 387 | mpu.setFullScaleAccelRange(MPU6050_ACCEL_FS_2); |
csharer | 3:2f76ffbc5cef | 388 | mpu.setDLPFMode(MPU6050_DLPF_BW_10); //10,20,42,98,188 // Default factor for BROBOT:10 |
arvindnr89 | 17:8e2824f64b91 | 389 | mpu.setRate(4); // 0=1khz 1=500hz, 2=333hz, 3=250hz [4=200hz]default |
csharer | 3:2f76ffbc5cef | 390 | mpu.setSleepEnabled(false); |
csharer | 3:2f76ffbc5cef | 391 | wait_ms(500); |
csharer | 3:2f76ffbc5cef | 392 | |
csharer | 3:2f76ffbc5cef | 393 | // load and configure the DMP |
csharer | 3:2f76ffbc5cef | 394 | devStatus = mpu.dmpInitialize(); |
csharer | 3:2f76ffbc5cef | 395 | if(devStatus == 0) { |
csharer | 3:2f76ffbc5cef | 396 | mpu.setDMPEnabled(true); |
csharer | 3:2f76ffbc5cef | 397 | mpuIntStatus = mpu.getIntStatus(); |
csharer | 3:2f76ffbc5cef | 398 | dmpReady = true; |
csharer | 4:2512939c10f0 | 399 | } else { |
csharer | 3:2f76ffbc5cef | 400 | // 1 = initial memory load failed |
csharer | 3:2f76ffbc5cef | 401 | // 2 = DMP configuration updates failed |
csharer | 3:2f76ffbc5cef | 402 | pc.printf("DMP INIT error \r\n"); |
csharer | 3:2f76ffbc5cef | 403 | } |
csharer | 3:2f76ffbc5cef | 404 | |
csharer | 3:2f76ffbc5cef | 405 | //Gyro Calibration |
csharer | 3:2f76ffbc5cef | 406 | wait_ms(500); |
arvindnr89 | 10:48f640a54401 | 407 | pc.printf("Gyro calibration!! Dont move the robot in 1 second... \r\n"); |
csharer | 3:2f76ffbc5cef | 408 | wait_ms(500); |
csharer | 4:2512939c10f0 | 409 | |
csharer | 3:2f76ffbc5cef | 410 | // verify connection |
csharer | 3:2f76ffbc5cef | 411 | pc.printf(mpu.testConnection() ? "Connection Good \r\n" : "Connection Failed\r\n"); |
csharer | 3:2f76ffbc5cef | 412 | |
csharer | 3:2f76ffbc5cef | 413 | //Adjust Sensor Fusion Gain |
csharer | 3:2f76ffbc5cef | 414 | dmpSetSensorFusionAccelGain(0x20); |
csharer | 3:2f76ffbc5cef | 415 | |
csharer | 3:2f76ffbc5cef | 416 | wait_ms(200); |
csharer | 3:2f76ffbc5cef | 417 | mpu.resetFIFO(); |
csharer | 3:2f76ffbc5cef | 418 | } |
csharer | 3:2f76ffbc5cef | 419 | |
arvindnr89 | 10:48f640a54401 | 420 | void init_system() |
arvindnr89 | 10:48f640a54401 | 421 | { |
arvindnr89 | 10:48f640a54401 | 422 | initilizeAngle = true; |
arvindnr89 | 17:8e2824f64b91 | 423 | initilizeTheta = true; |
arvindnr89 | 16:3a85662fb740 | 424 | totalTheta = 0; |
arvindnr89 | 16:3a85662fb740 | 425 | target_pos = 0; |
arvindnr89 | 16:3a85662fb740 | 426 | pos_error = 0; |
arvindnr89 | 10:48f640a54401 | 427 | //Set the Channel. 0 is default, 15 is max |
arvindnr89 | 10:48f640a54401 | 428 | mrf.SetChannel(MRF_CHANNEL); |
arvindnr89 | 10:48f640a54401 | 429 | enable = DISABLE; //Disable Motors |
arvindnr89 | 10:48f640a54401 | 430 | pc.baud(115200); |
arvindnr89 | 10:48f640a54401 | 431 | pc.printf("Booting BroBot mbed RTOS..\r\n"); |
arvindnr89 | 10:48f640a54401 | 432 | |
arvindnr89 | 10:48f640a54401 | 433 | //Initilize the IMU |
arvindnr89 | 10:48f640a54401 | 434 | init_imu(); |
arvindnr89 | 10:48f640a54401 | 435 | //Attach Interupt for IMU on rising edge of checkpin |
arvindnr89 | 10:48f640a54401 | 436 | checkpin.rise(&dmpDataReady); |
arvindnr89 | 10:48f640a54401 | 437 | pc.printf("IMU Initilized..\r\n"); |
arvindnr89 | 10:48f640a54401 | 438 | |
arvindnr89 | 10:48f640a54401 | 439 | //Init Stepper Motors |
arvindnr89 | 10:48f640a54401 | 440 | //Attach Motor Control Timer Call Back Functions |
arvindnr89 | 10:48f640a54401 | 441 | timer_M1.attach_us(&ISR1, ZERO_SPEED);//1s Period |
arvindnr89 | 10:48f640a54401 | 442 | timer_M2.attach_us(&ISR2, ZERO_SPEED);//1s Period |
arvindnr89 | 10:48f640a54401 | 443 | step_M1 = 1; |
arvindnr89 | 10:48f640a54401 | 444 | dir_M1 = 1; |
arvindnr89 | 10:48f640a54401 | 445 | pc.printf("Motors Initilized..\r\n"); |
arvindnr89 | 10:48f640a54401 | 446 | |
arvindnr89 | 10:48f640a54401 | 447 | //Timers initilized |
arvindnr89 | 10:48f640a54401 | 448 | timer.start(); |
arvindnr89 | 10:48f640a54401 | 449 | timer_value = timer.read_us(); |
arvindnr89 | 10:48f640a54401 | 450 | |
arvindnr89 | 11:2553f5798f84 | 451 | enable = ENABLE; //Enable Motors |
arvindnr89 | 10:48f640a54401 | 452 | } |
arvindnr89 | 10:48f640a54401 | 453 | |
csharer | 3:2f76ffbc5cef | 454 | // ================================================================ |
csharer | 3:2f76ffbc5cef | 455 | // === MAIN PROGRAM LOOP === |
csharer | 3:2f76ffbc5cef | 456 | // ================================================================ |
csharer | 3:2f76ffbc5cef | 457 | int main() |
csharer | 3:2f76ffbc5cef | 458 | { |
arvindnr89 | 10:48f640a54401 | 459 | init_system(); |
csharer | 4:2512939c10f0 | 460 | |
arvindnr89 | 10:48f640a54401 | 461 | //Create IMU Thread |
arvindnr89 | 11:2553f5798f84 | 462 | imu_update_thread_ID = osThreadCreate(osThread(imu_update_thread), NULL); |
csharer | 9:fb671fa0c0be | 463 | |
arvindnr89 | 11:2553f5798f84 | 464 | //Create PID Thread |
arvindnr89 | 11:2553f5798f84 | 465 | pid_update_thread_ID = osThreadCreate(osThread(pid_update_thread), NULL); |
csharer | 9:fb671fa0c0be | 466 | |
arvindnr89 | 11:2553f5798f84 | 467 | //Create Communication Thread |
arvindnr89 | 11:2553f5798f84 | 468 | communication_update_thread_ID = osThreadCreate(osThread(communication_update_thread), NULL); |
csharer | 4:2512939c10f0 | 469 | |
csharer | 3:2f76ffbc5cef | 470 | } //End Main() |