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