Cube_Mini_Solution
Dependencies: mbed QEI MPU6050 BLE_API nRF51822 MCP4725 eMPL_MPU6050
main.cpp@8:71babe904b92, 2020-05-12 (annotated)
- Committer:
- BoulusAJ
- Date:
- Tue May 12 19:23:59 2020 +0000
- Revision:
- 8:71babe904b92
- Parent:
- 7:07c5b6709d87
- Child:
- 9:43e5e3430621
It Lifts Up! May 12, 2020 - 21:23
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
BoulusAJ | 0:8e87cdf07037 | 1 | /* |
BoulusAJ | 0:8e87cdf07037 | 2 | Boulus Abu Joudom |
BoulusAJ | 3:1a714eccfe9f | 3 | Version: May 5, 2020. 17:00 |
BoulusAJ | 0:8e87cdf07037 | 4 | ZHAW - IMS |
BoulusAJ | 0:8e87cdf07037 | 5 | |
BoulusAJ | 0:8e87cdf07037 | 6 | Cubiod Balancing Experiment - Control Lab |
BoulusAJ | 0:8e87cdf07037 | 7 | Seeed Tiny Microcontroller on Cuboid 1.0 |
BoulusAJ | 0:8e87cdf07037 | 8 | */ |
BoulusAJ | 0:8e87cdf07037 | 9 | // Libraries |
BoulusAJ | 0:8e87cdf07037 | 10 | #include "mbed.h" |
BoulusAJ | 0:8e87cdf07037 | 11 | #include "math.h" |
BoulusAJ | 0:8e87cdf07037 | 12 | #include "MPU6050.h" |
BoulusAJ | 0:8e87cdf07037 | 13 | #include "IIR_filter.h" |
BoulusAJ | 0:8e87cdf07037 | 14 | #include "mcp4725.h" |
BoulusAJ | 0:8e87cdf07037 | 15 | #include <stdbool.h> |
BoulusAJ | 0:8e87cdf07037 | 16 | #include "LinearCharacteristics.h" |
BoulusAJ | 7:07c5b6709d87 | 17 | #include "PID_Cntrl_Old.h" |
BoulusAJ | 0:8e87cdf07037 | 18 | #include "PID_Cntrl_2.h" |
BoulusAJ | 0:8e87cdf07037 | 19 | |
BoulusAJ | 0:8e87cdf07037 | 20 | // Serial PC Communication |
BoulusAJ | 0:8e87cdf07037 | 21 | #define UART_TX p9 |
BoulusAJ | 0:8e87cdf07037 | 22 | #define UART_RX p11 |
BoulusAJ | 0:8e87cdf07037 | 23 | |
BoulusAJ | 0:8e87cdf07037 | 24 | // IMU SDA and SCL |
BoulusAJ | 0:8e87cdf07037 | 25 | #define MPU6050_SDA p12 |
BoulusAJ | 0:8e87cdf07037 | 26 | #define MPU6050_SCL p13 |
BoulusAJ | 0:8e87cdf07037 | 27 | |
BoulusAJ | 0:8e87cdf07037 | 28 | // PI Value |
BoulusAJ | 0:8e87cdf07037 | 29 | #define PI 3.1415927f |
BoulusAJ | 0:8e87cdf07037 | 30 | #define pi 3.1415927f |
BoulusAJ | 0:8e87cdf07037 | 31 | |
BoulusAJ | 0:8e87cdf07037 | 32 | // Analog/Digitsl I/O Definitions |
BoulusAJ | 0:8e87cdf07037 | 33 | AnalogIn Velocity_Voltage_Input(p5); |
BoulusAJ | 2:e088fa08e244 | 34 | DigitalOut Pin_3V3(p30); |
BoulusAJ | 0:8e87cdf07037 | 35 | |
BoulusAJ | 0:8e87cdf07037 | 36 | // PC Serial |
BoulusAJ | 0:8e87cdf07037 | 37 | Serial pc(UART_TX, UART_RX); |
BoulusAJ | 0:8e87cdf07037 | 38 | |
BoulusAJ | 0:8e87cdf07037 | 39 | //-------- USER INPUT ( Desired Values) ------- |
BoulusAJ | 0:8e87cdf07037 | 40 | // User Input |
BoulusAJ | 0:8e87cdf07037 | 41 | float Desired_input = 0.0f; |
BoulusAJ | 0:8e87cdf07037 | 42 | // Initial System input in Amperes |
BoulusAJ | 0:8e87cdf07037 | 43 | float Sys_input_Amps = 0.0f; |
BoulusAJ | 0:8e87cdf07037 | 44 | //-------------------------------------------- |
BoulusAJ | 0:8e87cdf07037 | 45 | |
BoulusAJ | 0:8e87cdf07037 | 46 | // ------------- Variables ------------------- |
BoulusAJ | 0:8e87cdf07037 | 47 | |
BoulusAJ | 0:8e87cdf07037 | 48 | // Sample time of main loops |
BoulusAJ | 8:71babe904b92 | 49 | float Ts = 0.005; |
BoulusAJ | 0:8e87cdf07037 | 50 | |
BoulusAJ | 0:8e87cdf07037 | 51 | // MPU 6050 Variables - Acceleration and Gyroscope Raw and Converted Data Variables |
BoulusAJ | 0:8e87cdf07037 | 52 | int16_t AccX_Raw, AccY_Raw, AccZ_Raw; |
BoulusAJ | 0:8e87cdf07037 | 53 | int16_t GyroX_Raw, GyroY_Raw, GyroZ_Raw; |
BoulusAJ | 0:8e87cdf07037 | 54 | |
BoulusAJ | 0:8e87cdf07037 | 55 | double AccX_g, AccY_g, AccZ_g; |
BoulusAJ | 0:8e87cdf07037 | 56 | double GyroX_Degrees, GyroY_Degrees, GyroZ_Degrees, GyroZ_RadiansPerSecond; |
BoulusAJ | 0:8e87cdf07037 | 57 | |
BoulusAJ | 0:8e87cdf07037 | 58 | int16_t IMU_Temperature; |
BoulusAJ | 0:8e87cdf07037 | 59 | // ---------------- |
BoulusAJ | 0:8e87cdf07037 | 60 | |
BoulusAJ | 0:8e87cdf07037 | 61 | // Angle Variables |
BoulusAJ | 0:8e87cdf07037 | 62 | double Cuboid_Angle_Radians, Cuboid_Angle_Degrees; |
BoulusAJ | 0:8e87cdf07037 | 63 | double Cuboid_Angle_Speed_Degrees = 0.0; |
BoulusAJ | 0:8e87cdf07037 | 64 | |
BoulusAJ | 0:8e87cdf07037 | 65 | // Low pass filter variables |
BoulusAJ | 0:8e87cdf07037 | 66 | float t = 0.5f; |
BoulusAJ | 0:8e87cdf07037 | 67 | |
BoulusAJ | 0:8e87cdf07037 | 68 | // printf Variable |
BoulusAJ | 0:8e87cdf07037 | 69 | int k = 0; |
BoulusAJ | 0:8e87cdf07037 | 70 | int LoopCounter = 0; |
BoulusAJ | 1:d3406369c297 | 71 | float LoopTime = 0.0; |
BoulusAJ | 0:8e87cdf07037 | 72 | |
BoulusAJ | 0:8e87cdf07037 | 73 | // Flywheel Position and Velocity variables |
BoulusAJ | 0:8e87cdf07037 | 74 | double Velocity_Input_Voltage = 0.0f; |
BoulusAJ | 0:8e87cdf07037 | 75 | double Velocity, Velocity_Voltage, Velocity_rpm, Velocity_Voltage_Read; |
BoulusAJ | 0:8e87cdf07037 | 76 | |
BoulusAJ | 0:8e87cdf07037 | 77 | // ------------------ |
BoulusAJ | 0:8e87cdf07037 | 78 | |
BoulusAJ | 0:8e87cdf07037 | 79 | //----------------- Controller VARIABLES------------------ |
BoulusAJ | 0:8e87cdf07037 | 80 | // Variables concerning the Controller, the Design is in reference to the Matlab Simulink .............. and Variables are in reference to the ............. File |
BoulusAJ | 0:8e87cdf07037 | 81 | |
BoulusAJ | 0:8e87cdf07037 | 82 | // Sate Space Controller Values |
BoulusAJ | 8:71babe904b92 | 83 | float K_SS_Controller [2] = {-57.1176*0.3, -2.6398}; // From Matlab |
BoulusAJ | 0:8e87cdf07037 | 84 | |
BoulusAJ | 0:8e87cdf07037 | 85 | // Controller Variables |
BoulusAJ | 0:8e87cdf07037 | 86 | float Loop1_output; // Loop 1 controller output |
BoulusAJ | 0:8e87cdf07037 | 87 | float Loop2_output; // Loop 2 controller output |
BoulusAJ | 0:8e87cdf07037 | 88 | float PID_Input, PID_Output; |
BoulusAJ | 0:8e87cdf07037 | 89 | |
BoulusAJ | 0:8e87cdf07037 | 90 | // Saturation Parameters |
BoulusAJ | 0:8e87cdf07037 | 91 | // PI Controller Limits |
BoulusAJ | 6:122879c1503a | 92 | const float uMin1 = -5.0f; |
BoulusAJ | 6:122879c1503a | 93 | const float uMax1= 5.0f; |
BoulusAJ | 0:8e87cdf07037 | 94 | |
BoulusAJ | 0:8e87cdf07037 | 95 | // Cuboid Driver Input Limits |
BoulusAJ | 8:71babe904b92 | 96 | const float uMin2 = -12.0f; |
BoulusAJ | 8:71babe904b92 | 97 | const float uMax2= 12.0f; |
BoulusAJ | 0:8e87cdf07037 | 98 | |
BoulusAJ | 0:8e87cdf07037 | 99 | // Controller Loop 2 (PI-Part) |
BoulusAJ | 0:8e87cdf07037 | 100 | float Kp_1 = -0.09; |
BoulusAJ | 0:8e87cdf07037 | 101 | float Ki_1 = -0.09; |
BoulusAJ | 0:8e87cdf07037 | 102 | float Kd_1 = 0; // No D-Part |
BoulusAJ | 0:8e87cdf07037 | 103 | float Tf_1 = 1; // No D-Part |
BoulusAJ | 0:8e87cdf07037 | 104 | |
BoulusAJ | 0:8e87cdf07037 | 105 | // Controller Loop (PI-Part) in Case 2 (breaking case) |
BoulusAJ | 0:8e87cdf07037 | 106 | float Kp_2 = 4; |
BoulusAJ | 0:8e87cdf07037 | 107 | float Ki_2 = 80; |
BoulusAJ | 0:8e87cdf07037 | 108 | float Kd_2 = 0; // No D-Part |
BoulusAJ | 0:8e87cdf07037 | 109 | float Tf_2 = 1; // No D-Part |
BoulusAJ | 0:8e87cdf07037 | 110 | |
BoulusAJ | 0:8e87cdf07037 | 111 | // ---------- Functions ------------- |
BoulusAJ | 0:8e87cdf07037 | 112 | |
BoulusAJ | 0:8e87cdf07037 | 113 | // Interrupts |
BoulusAJ | 0:8e87cdf07037 | 114 | Ticker ControllerLoopTimer; // Interrupt for control loop |
BoulusAJ | 0:8e87cdf07037 | 115 | |
BoulusAJ | 0:8e87cdf07037 | 116 | // DAC |
BoulusAJ | 0:8e87cdf07037 | 117 | MCP4725 VoltageOut(p3, p4, MCP4725::Fast400kHz, 0); |
BoulusAJ | 0:8e87cdf07037 | 118 | |
BoulusAJ | 0:8e87cdf07037 | 119 | // IMU - MPU6050 Functions and I2C Functions |
BoulusAJ | 0:8e87cdf07037 | 120 | I2C i2c(MPU6050_SDA, MPU6050_SCL); |
BoulusAJ | 0:8e87cdf07037 | 121 | MPU6050 mpu(i2c); |
BoulusAJ | 0:8e87cdf07037 | 122 | |
BoulusAJ | 0:8e87cdf07037 | 123 | // Accelerometer and Gyroscope Filters |
BoulusAJ | 0:8e87cdf07037 | 124 | IIR_filter FilterAccX(t, Ts, 1.0f); |
BoulusAJ | 0:8e87cdf07037 | 125 | IIR_filter FilterAccY(t, Ts, 1.0f); |
BoulusAJ | 0:8e87cdf07037 | 126 | IIR_filter FilterGyro(t, Ts, t); |
BoulusAJ | 0:8e87cdf07037 | 127 | |
BoulusAJ | 0:8e87cdf07037 | 128 | // Linear Scaler |
BoulusAJ | 3:1a714eccfe9f | 129 | LinearCharacteristics CurrentToVoltage(-15.0f, 15.0f, 0.0f, 5.0f); |
BoulusAJ | 3:1a714eccfe9f | 130 | LinearCharacteristics VoltageToVelocity(0.0f, 3.0f, -4000.0f, 4000.0f); |
BoulusAJ | 0:8e87cdf07037 | 131 | |
BoulusAJ | 0:8e87cdf07037 | 132 | // PID Controllers |
BoulusAJ | 7:07c5b6709d87 | 133 | PID_Cntrl_Old C1(Kp_1,Ki_1,Kd_1,Tf_1,Ts,uMin1,uMax1); // Defining the 1st Loop Controller (PI-Part) |
BoulusAJ | 7:07c5b6709d87 | 134 | PID_Cntrl_Old C2(Kp_2,Ki_2,Kd_2,Tf_2,Ts,uMin1,uMax1); // Defining the PI Controller for Chase (State 2) to keep motor velocity at zero |
BoulusAJ | 0:8e87cdf07037 | 135 | |
BoulusAJ | 0:8e87cdf07037 | 136 | // Timers |
BoulusAJ | 0:8e87cdf07037 | 137 | Timer Loop; |
BoulusAJ | 0:8e87cdf07037 | 138 | |
BoulusAJ | 0:8e87cdf07037 | 139 | // ----- User defined functions ----------- |
BoulusAJ | 0:8e87cdf07037 | 140 | void updateControllers(void); // speed controller loop (via interrupt) |
BoulusAJ | 0:8e87cdf07037 | 141 | |
BoulusAJ | 0:8e87cdf07037 | 142 | // -------------- MAIN LOOP ---------------- |
BoulusAJ | 0:8e87cdf07037 | 143 | int main() |
BoulusAJ | 0:8e87cdf07037 | 144 | { |
BoulusAJ | 0:8e87cdf07037 | 145 | |
BoulusAJ | 0:8e87cdf07037 | 146 | pc.baud(115200); |
BoulusAJ | 2:e088fa08e244 | 147 | Pin_3V3.write(1); |
BoulusAJ | 4:2a5cd0ad8100 | 148 | |
BoulusAJ | 8:71babe904b92 | 149 | //Loop.start(); |
BoulusAJ | 0:8e87cdf07037 | 150 | |
BoulusAJ | 0:8e87cdf07037 | 151 | // Reset Filters |
BoulusAJ | 0:8e87cdf07037 | 152 | FilterAccX.reset(0.0f); |
BoulusAJ | 0:8e87cdf07037 | 153 | FilterAccY.reset(0.0f); |
BoulusAJ | 0:8e87cdf07037 | 154 | FilterGyro.reset(0.0f); |
BoulusAJ | 0:8e87cdf07037 | 155 | |
BoulusAJ | 0:8e87cdf07037 | 156 | //--------------------------------- IMU - MPU6050 initialize ------------------------------------- |
BoulusAJ | 0:8e87cdf07037 | 157 | pc.printf("MPU6050 initialize \n\r"); |
BoulusAJ | 0:8e87cdf07037 | 158 | mpu.initialize(); |
BoulusAJ | 0:8e87cdf07037 | 159 | pc.printf("MPU6050 testConnection \n\r"); |
BoulusAJ | 0:8e87cdf07037 | 160 | |
BoulusAJ | 0:8e87cdf07037 | 161 | bool mpu6050TestResult = mpu.testConnection(); |
BoulusAJ | 0:8e87cdf07037 | 162 | if(mpu6050TestResult) { |
BoulusAJ | 0:8e87cdf07037 | 163 | pc.printf("MPU6050 test passed \n\r"); |
BoulusAJ | 0:8e87cdf07037 | 164 | } else { |
BoulusAJ | 0:8e87cdf07037 | 165 | pc.printf("MPU6050 test failed \n\r"); |
BoulusAJ | 0:8e87cdf07037 | 166 | } |
BoulusAJ | 0:8e87cdf07037 | 167 | |
BoulusAJ | 0:8e87cdf07037 | 168 | // Set Low Pass Filter Bandwidth to 44Hz (4.9ms) for the Acc and 42Hz (4.8ms) for the Gyroscope |
BoulusAJ | 0:8e87cdf07037 | 169 | //mpu.setDLPFMode(MPU6050_DLPF_BW_42); |
BoulusAJ | 0:8e87cdf07037 | 170 | |
BoulusAJ | 0:8e87cdf07037 | 171 | // Set Low Pass Filter Bandwidth to 21Hz (8.5ms) for the Acc and 20Hz (8.3ms) for the Gyroscope |
BoulusAJ | 0:8e87cdf07037 | 172 | mpu.setDLPFMode(MPU6050_DLPF_BW_20); |
BoulusAJ | 0:8e87cdf07037 | 173 | |
BoulusAJ | 0:8e87cdf07037 | 174 | // Change the scale of the Gyroscope to +/- 1000 degrees/sec |
BoulusAJ | 0:8e87cdf07037 | 175 | mpu.setFullScaleGyroRange(2u); |
BoulusAJ | 0:8e87cdf07037 | 176 | |
BoulusAJ | 0:8e87cdf07037 | 177 | // Change the scale of the Accelerometer to +/- 4g - Sensitivity: 4096 LSB/mg |
BoulusAJ | 0:8e87cdf07037 | 178 | mpu.setFullScaleAccelRange(MPU6050_ACCEL_FS_4); |
BoulusAJ | 0:8e87cdf07037 | 179 | |
BoulusAJ | 0:8e87cdf07037 | 180 | //---------------------------------------------------------------------------------------------- |
BoulusAJ | 4:2a5cd0ad8100 | 181 | |
BoulusAJ | 0:8e87cdf07037 | 182 | // Reset PID |
BoulusAJ | 6:122879c1503a | 183 | C1.reset(0.0f); |
BoulusAJ | 0:8e87cdf07037 | 184 | |
BoulusAJ | 0:8e87cdf07037 | 185 | pc.printf("Hello World!\n\r"); |
BoulusAJ | 4:2a5cd0ad8100 | 186 | wait(1); |
BoulusAJ | 8:71babe904b92 | 187 | Loop.start(); |
BoulusAJ | 7:07c5b6709d87 | 188 | ControllerLoopTimer.attach(&updateControllers, Ts); |
BoulusAJ | 7:07c5b6709d87 | 189 | |
BoulusAJ | 7:07c5b6709d87 | 190 | } |
BoulusAJ | 4:2a5cd0ad8100 | 191 | |
BoulusAJ | 8:71babe904b92 | 192 | // Loop.reset(); |
BoulusAJ | 8:71babe904b92 | 193 | |
BoulusAJ | 7:07c5b6709d87 | 194 | void updateControllers(void) |
BoulusAJ | 7:07c5b6709d87 | 195 | { |
BoulusAJ | 8:71babe904b92 | 196 | |
BoulusAJ | 0:8e87cdf07037 | 197 | |
BoulusAJ | 7:07c5b6709d87 | 198 | // Counter |
BoulusAJ | 7:07c5b6709d87 | 199 | //LoopCounter++; |
BoulusAJ | 0:8e87cdf07037 | 200 | |
BoulusAJ | 7:07c5b6709d87 | 201 | // Acquire Velocity |
BoulusAJ | 7:07c5b6709d87 | 202 | Velocity_Voltage_Read = Velocity_Voltage_Input.read(); |
BoulusAJ | 7:07c5b6709d87 | 203 | Velocity_Voltage = 3.2442178*(Velocity_Voltage_Read); |
BoulusAJ | 7:07c5b6709d87 | 204 | Velocity_rpm = VoltageToVelocity(Velocity_Voltage); |
BoulusAJ | 7:07c5b6709d87 | 205 | Velocity = Velocity_rpm*2.0*pi/60.0; |
BoulusAJ | 7:07c5b6709d87 | 206 | |
BoulusAJ | 0:8e87cdf07037 | 207 | |
BoulusAJ | 7:07c5b6709d87 | 208 | // Aquire Raw Acceleration and Gyro Data form the IMU |
BoulusAJ | 7:07c5b6709d87 | 209 | mpu.getMotion6(&AccX_Raw, &AccY_Raw, &AccZ_Raw, &GyroX_Raw, &GyroY_Raw, &GyroZ_Raw); |
BoulusAJ | 7:07c5b6709d87 | 210 | |
BoulusAJ | 7:07c5b6709d87 | 211 | // -------------- Convert Raw data to SI Units -------------------- |
BoulusAJ | 7:07c5b6709d87 | 212 | |
BoulusAJ | 7:07c5b6709d87 | 213 | //Convert Acceleration Raw Data to (ms^-2) - (Settings of +/- 4g) |
BoulusAJ | 7:07c5b6709d87 | 214 | AccX_g = AccX_Raw / 8192.0f; |
BoulusAJ | 7:07c5b6709d87 | 215 | AccY_g = AccY_Raw / 8192.0f; |
BoulusAJ | 7:07c5b6709d87 | 216 | AccZ_g = AccZ_Raw / 8192.0f; |
BoulusAJ | 0:8e87cdf07037 | 217 | |
BoulusAJ | 7:07c5b6709d87 | 218 | //Convert Gyroscope Raw Data to Degrees per second |
BoulusAJ | 7:07c5b6709d87 | 219 | GyroX_Degrees = GyroX_Raw / 32.768f; // (2^15/1000 = 32.768) |
BoulusAJ | 7:07c5b6709d87 | 220 | GyroY_Degrees = GyroY_Raw / 32.768f; // (2^15/1000 = 32.768) |
BoulusAJ | 7:07c5b6709d87 | 221 | GyroZ_Degrees = GyroZ_Raw / 32.768f; // (2^15/1000 = 32.768) |
BoulusAJ | 0:8e87cdf07037 | 222 | |
BoulusAJ | 7:07c5b6709d87 | 223 | //Convert Gyroscope Raw Data to Degrees per second |
BoulusAJ | 7:07c5b6709d87 | 224 | GyroZ_RadiansPerSecond = (GyroZ_Raw / 32.768f)* pi/180.0f; |
BoulusAJ | 0:8e87cdf07037 | 225 | |
BoulusAJ | 7:07c5b6709d87 | 226 | // ----- Combine Accelerometer Data and Gyro Data to Get Angle ------ |
BoulusAJ | 0:8e87cdf07037 | 227 | |
BoulusAJ | 7:07c5b6709d87 | 228 | Cuboid_Angle_Radians = -1*atan2(-FilterAccX(AccX_g), FilterAccY(AccY_g)) + 0.7854f + FilterGyro(GyroZ_RadiansPerSecond); // Check later if fast enough!! |
BoulusAJ | 7:07c5b6709d87 | 229 | Cuboid_Angle_Degrees = Cuboid_Angle_Radians*180.0f/pi; |
BoulusAJ | 0:8e87cdf07037 | 230 | |
BoulusAJ | 0:8e87cdf07037 | 231 | |
BoulusAJ | 7:07c5b6709d87 | 232 | // ------------------------- Controller ----------------------------- |
BoulusAJ | 7:07c5b6709d87 | 233 | // Current Input Updater - Amperes |
BoulusAJ | 7:07c5b6709d87 | 234 | // Loop 1 |
BoulusAJ | 7:07c5b6709d87 | 235 | Loop1_output = Cuboid_Angle_Radians*K_SS_Controller[0]; |
BoulusAJ | 7:07c5b6709d87 | 236 | // Loop 2 |
BoulusAJ | 7:07c5b6709d87 | 237 | Loop2_output = GyroZ_RadiansPerSecond*K_SS_Controller[1]; |
BoulusAJ | 7:07c5b6709d87 | 238 | // PI Controller |
BoulusAJ | 7:07c5b6709d87 | 239 | PID_Input = Desired_input - Velocity; |
BoulusAJ | 8:71babe904b92 | 240 | PID_Output = C1.update(PID_Input); |
BoulusAJ | 7:07c5b6709d87 | 241 | /* |
BoulusAJ | 8:71babe904b92 | 242 | |
BoulusAJ | 7:07c5b6709d87 | 243 | //PID_Output = C1.update(-1*PID_Input); |
BoulusAJ | 7:07c5b6709d87 | 244 | //PID_Output = 0; // CHANGE LATER - This is for testing purposes |
BoulusAJ | 7:07c5b6709d87 | 245 | */ |
BoulusAJ | 0:8e87cdf07037 | 246 | |
BoulusAJ | 7:07c5b6709d87 | 247 | // System input |
BoulusAJ | 8:71babe904b92 | 248 | Sys_input_Amps = PID_Output - Loop1_output - Loop2_output; |
BoulusAJ | 3:1a714eccfe9f | 249 | |
BoulusAJ | 3:1a714eccfe9f | 250 | |
BoulusAJ | 7:07c5b6709d87 | 251 | if (Sys_input_Amps > uMax2) { |
BoulusAJ | 7:07c5b6709d87 | 252 | Sys_input_Amps = uMax2; |
BoulusAJ | 7:07c5b6709d87 | 253 | } |
BoulusAJ | 7:07c5b6709d87 | 254 | if (Sys_input_Amps < uMin2) { |
BoulusAJ | 7:07c5b6709d87 | 255 | Sys_input_Amps = uMin2; |
BoulusAJ | 7:07c5b6709d87 | 256 | } |
BoulusAJ | 4:2a5cd0ad8100 | 257 | |
BoulusAJ | 7:07c5b6709d87 | 258 | // Scaling the controller output from -15 A --> 15 A to 0 V --> 5 V |
BoulusAJ | 7:07c5b6709d87 | 259 | VoltageOut.write(CurrentToVoltage(Sys_input_Amps)); |
BoulusAJ | 7:07c5b6709d87 | 260 | |
BoulusAJ | 7:07c5b6709d87 | 261 | // Print Data |
BoulusAJ | 8:71babe904b92 | 262 | if(++k >= 200) { |
BoulusAJ | 7:07c5b6709d87 | 263 | k = 0; |
BoulusAJ | 0:8e87cdf07037 | 264 | |
BoulusAJ | 4:2a5cd0ad8100 | 265 | //1LoopCounter,2.1CurrentToVoltage, 2Sys_input_Amps, 3Cuboid_Angle_Degrees, 4GyroZ_RadiansPerSecond, 5Velocity, 6Velocity_Voltage, 7AccX_g, 8AccY_g, 9PID_Input, 10PID_Output, (Loop1_output+Loop2_output), Loop.read()) |
BoulusAJ | 7:07c5b6709d87 | 266 | pc.printf(" %i, %0.5f, %0.5f, %0.5f, %0.5f, %0.5f, %0.5f, %0.5f, %0.5f, %0.5f, %0.5f, %0.5f, ", LoopCounter, CurrentToVoltage(Sys_input_Amps), Sys_input_Amps, Cuboid_Angle_Degrees, GyroZ_RadiansPerSecond, Velocity, Velocity_Voltage, AccX_g, AccY_g, PID_Input, PID_Output, (Loop1_output+Loop2_output)); |
BoulusAJ | 8:71babe904b92 | 267 | pc.printf("%i; %0.6f, \n\r", LoopCounter, Loop.read()); |
BoulusAJ | 7:07c5b6709d87 | 268 | } |
BoulusAJ | 4:2a5cd0ad8100 | 269 | |
BoulusAJ | 7:07c5b6709d87 | 270 | //pc.printf("%i; %0.6f, \n\r", LoopCounter, Loop.read()); |
BoulusAJ | 0:8e87cdf07037 | 271 | |
BoulusAJ | 7:07c5b6709d87 | 272 | // |
BoulusAJ | 7:07c5b6709d87 | 273 | |
BoulusAJ | 7:07c5b6709d87 | 274 | // wait(0.0025); |
BoulusAJ | 7:07c5b6709d87 | 275 | // pc.printf(" %0.5f \n\r", Loop.read()); |
BoulusAJ | 4:2a5cd0ad8100 | 276 | |
BoulusAJ | 7:07c5b6709d87 | 277 | /* |
BoulusAJ | 7:07c5b6709d87 | 278 | LoopTime = Loop.read(); |
BoulusAJ | 7:07c5b6709d87 | 279 | while(LoopTime < Ts) { |
BoulusAJ | 7:07c5b6709d87 | 280 | LoopTime = Loop.read(); |
BoulusAJ | 7:07c5b6709d87 | 281 | } |
BoulusAJ | 7:07c5b6709d87 | 282 | */ |
BoulusAJ | 4:2a5cd0ad8100 | 283 | |
BoulusAJ | 7:07c5b6709d87 | 284 | // pc.printf(" %0.5f \n\r", LoopTime); |
BoulusAJ | 7:07c5b6709d87 | 285 | |
BoulusAJ | 7:07c5b6709d87 | 286 | //pc.printf("Angle = %0.2f, Current = %0.2f, VoltageOut = %0.3f, Velocity = %0.2f, VoltageIn = %0.3f\n\r", Cuboid_Angle_Degrees, Sys_input_Amps, CurrentToVoltage(Sys_input_Amps), Velocity, Velocity_Voltage); |
BoulusAJ | 0:8e87cdf07037 | 287 | |
BoulusAJ | 7:07c5b6709d87 | 288 | /* |
BoulusAJ | 7:07c5b6709d87 | 289 | // Print Data |
BoulusAJ | 7:07c5b6709d87 | 290 | if(++k >= 300) { |
BoulusAJ | 7:07c5b6709d87 | 291 | k = 0; |
BoulusAJ | 7:07c5b6709d87 | 292 | //pc.printf("%0.8f\n\r", Loop.read()); |
BoulusAJ | 7:07c5b6709d87 | 293 | pc.printf("Angle = %0.2f, Current = %0.2f, VoltageOut = %0.3f, Velocity = %0.2f, VoltageIn = %0.3f\n\r", Cuboid_Angle_Degrees, Sys_input_Amps, CurrentToVoltage(Sys_input_Amps), Velocity, Velocity_Voltage); |
BoulusAJ | 0:8e87cdf07037 | 294 | |
BoulusAJ | 7:07c5b6709d87 | 295 | } |
BoulusAJ | 7:07c5b6709d87 | 296 | */ |
BoulusAJ | 4:2a5cd0ad8100 | 297 | |
BoulusAJ | 7:07c5b6709d87 | 298 | |
BoulusAJ | 1:d3406369c297 | 299 | } |
BoulusAJ | 0:8e87cdf07037 | 300 | |
BoulusAJ | 0:8e87cdf07037 | 301 | |
BoulusAJ | 0:8e87cdf07037 | 302 | |
BoulusAJ | 0:8e87cdf07037 | 303 | |
BoulusAJ | 0:8e87cdf07037 | 304 | |
BoulusAJ | 0:8e87cdf07037 | 305 | //------------------------------------------------------------------- |
BoulusAJ | 0:8e87cdf07037 | 306 | //--------------------------Example Codes----------------------------- |
BoulusAJ | 0:8e87cdf07037 | 307 | |
BoulusAJ | 0:8e87cdf07037 | 308 | // This code demonstrates how to change the Low Pass Filter Bandwidth |
BoulusAJ | 0:8e87cdf07037 | 309 | /* |
BoulusAJ | 0:8e87cdf07037 | 310 | pc.printf("Digital Low Pass Filter Bandwidth: %u \n\r", mpu.getDLPFMode()); |
BoulusAJ | 0:8e87cdf07037 | 311 | |
BoulusAJ | 0:8e87cdf07037 | 312 | mpu.setDLPFMode(MPU6050_DLPF_BW_42); |
BoulusAJ | 0:8e87cdf07037 | 313 | wait(0.1); |
BoulusAJ | 0:8e87cdf07037 | 314 | pc.printf("Digital Low Pass Filter Bandwidth: %u \n\r", mpu.getDLPFMode()); |
BoulusAJ | 0:8e87cdf07037 | 315 | */ |
BoulusAJ | 0:8e87cdf07037 | 316 | // ---------------- |
BoulusAJ | 0:8e87cdf07037 | 317 | |
BoulusAJ | 0:8e87cdf07037 | 318 | // This code demonstrates how to change the scale settings for the Gyro scope |
BoulusAJ | 0:8e87cdf07037 | 319 | /* |
BoulusAJ | 0:8e87cdf07037 | 320 | pc.printf("Gyro Range Scale: %u \n\r", mpu.getFullScaleGyroRange()); |
BoulusAJ | 0:8e87cdf07037 | 321 | int Temp123 = 0; |
BoulusAJ | 0:8e87cdf07037 | 322 | while(Temp123 < 5) { |
BoulusAJ | 0:8e87cdf07037 | 323 | |
BoulusAJ | 0:8e87cdf07037 | 324 | wait(0.4); |
BoulusAJ | 0:8e87cdf07037 | 325 | mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz); |
BoulusAJ | 0:8e87cdf07037 | 326 | //writing current accelerometer and gyro position |
BoulusAJ | 0:8e87cdf07037 | 327 | pc.printf(" AccX: %d; AccY: %d; AccZ: %d; GyroX: %d; GyroY: %d; GyroZ: %d;\n\r",ax,ay,az,gx,gy,gz); |
BoulusAJ | 0:8e87cdf07037 | 328 | Temp123 = Temp123 + 1; |
BoulusAJ | 0:8e87cdf07037 | 329 | } |
BoulusAJ | 0:8e87cdf07037 | 330 | Temp123 = 0; |
BoulusAJ | 0:8e87cdf07037 | 331 | |
BoulusAJ | 0:8e87cdf07037 | 332 | mpu.setFullScaleGyroRange(3u); |
BoulusAJ | 0:8e87cdf07037 | 333 | wait(0.1); |
BoulusAJ | 0:8e87cdf07037 | 334 | pc.printf("Gyro Range Scale: %u \n\r", mpu.getFullScaleGyroRange()); |
BoulusAJ | 0:8e87cdf07037 | 335 | while(Temp123 < 5) { |
BoulusAJ | 0:8e87cdf07037 | 336 | |
BoulusAJ | 0:8e87cdf07037 | 337 | wait(0.4); |
BoulusAJ | 0:8e87cdf07037 | 338 | mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz); |
BoulusAJ | 0:8e87cdf07037 | 339 | //writing current accelerometer and gyro position |
BoulusAJ | 0:8e87cdf07037 | 340 | pc.printf(" AccX: %d; AccY: %d; AccZ: %d; GyroX: %d; GyroY: %d; GyroZ: %d;\n\r",ax,ay,az,gx,gy,gz); |
BoulusAJ | 0:8e87cdf07037 | 341 | Temp123 = Temp123 + 1; |
BoulusAJ | 0:8e87cdf07037 | 342 | } |
BoulusAJ | 0:8e87cdf07037 | 343 | Temp123 = 0; |
BoulusAJ | 0:8e87cdf07037 | 344 | */ |
BoulusAJ | 0:8e87cdf07037 | 345 | |
BoulusAJ | 0:8e87cdf07037 | 346 | /* |
BoulusAJ | 0:8e87cdf07037 | 347 | while(1) { |
BoulusAJ | 0:8e87cdf07037 | 348 | wait(0.4); |
BoulusAJ | 0:8e87cdf07037 | 349 | mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz); |
BoulusAJ | 0:8e87cdf07037 | 350 | //writing current accelerometer and gyro position |
BoulusAJ | 0:8e87cdf07037 | 351 | pc.printf(" AccX: %d; AccY: %d; AccZ: %d; GyroX: %d; GyroY: %d; GyroZ: %d;\n\r",ax,ay,az,gx,gy,gz); |
BoulusAJ | 0:8e87cdf07037 | 352 | } |
BoulusAJ | 0:8e87cdf07037 | 353 | */ |
BoulusAJ | 0:8e87cdf07037 | 354 | // ------------------ |
BoulusAJ | 0:8e87cdf07037 | 355 | |
BoulusAJ | 0:8e87cdf07037 | 356 | |
BoulusAJ | 0:8e87cdf07037 | 357 | // Printing Register Values |
BoulusAJ | 0:8e87cdf07037 | 358 | //printf("i2c SCL : 0x%x\r\n", *((unsigned int *)0x40004508)); |
BoulusAJ | 0:8e87cdf07037 | 359 | |
BoulusAJ | 0:8e87cdf07037 | 360 | // ------------------ |
BoulusAJ | 0:8e87cdf07037 | 361 | |
BoulusAJ | 0:8e87cdf07037 | 362 | // Velocity Measurement Code for Designing a Filter |
BoulusAJ | 0:8e87cdf07037 | 363 | /* |
BoulusAJ | 0:8e87cdf07037 | 364 | if (aaa < (600*3)) { |
BoulusAJ | 0:8e87cdf07037 | 365 | if (++aaa <= 900) { |
BoulusAJ | 0:8e87cdf07037 | 366 | VoltageOut.write(CurrentToVoltage(1.0f)); |
BoulusAJ | 0:8e87cdf07037 | 367 | } else { |
BoulusAJ | 0:8e87cdf07037 | 368 | VoltageOut.write(CurrentToVoltage(-1.0f)); |
BoulusAJ | 0:8e87cdf07037 | 369 | } |
BoulusAJ | 0:8e87cdf07037 | 370 | pc.printf("%0.8f\n\r", Velocity_Voltage_Read); |
BoulusAJ | 0:8e87cdf07037 | 371 | } else { |
BoulusAJ | 0:8e87cdf07037 | 372 | VoltageOut.write(CurrentToVoltage(0.0f)); |
BoulusAJ | 0:8e87cdf07037 | 373 | } |
BoulusAJ | 0:8e87cdf07037 | 374 | */ |