Cube_Mini_Solution

Dependencies:   mbed QEI MPU6050 BLE_API nRF51822 MCP4725 eMPL_MPU6050

Committer:
BoulusAJ
Date:
Fri May 08 14:51:09 2020 +0000
Revision:
2:e088fa08e244
Parent:
1:d3406369c297
Child:
3:1a714eccfe9f
This version is the version before the assembly of Cuboid 2.0; ; May 5, 2020

Who changed what in which revision?

UserRevisionLine numberNew 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 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 1:d3406369c297 49 float Ts = 0.015;
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 0:8e87cdf07037 83 float K_SS_Controller [2] = {-33.5, -1.856}; // Currently for Cuboid 1.0 - UPDATE LATER for Cuboid 2.0 -
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 0:8e87cdf07037 92 const float uMin1 = -5.0f;
BoulusAJ 0:8e87cdf07037 93 const float uMax1= 5.0f;
BoulusAJ 0:8e87cdf07037 94
BoulusAJ 0:8e87cdf07037 95 // Cuboid Driver Input Limits
BoulusAJ 0:8e87cdf07037 96 const float uMin2 = -15.0f;
BoulusAJ 0:8e87cdf07037 97 const float uMax2= 15.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 0:8e87cdf07037 129 LinearCharacteristics CurrentToVoltage(-15.0f, 15.0f, 0.0f, 4.94f);
BoulusAJ 0:8e87cdf07037 130 LinearCharacteristics VoltageToVelocity(0.0f, 3.0f, -2000.0f, 2000.0f);
BoulusAJ 0:8e87cdf07037 131
BoulusAJ 0:8e87cdf07037 132 // PID Controllers
BoulusAJ 0:8e87cdf07037 133 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 134 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 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 1:d3406369c297 148
BoulusAJ 1:d3406369c297 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 0:8e87cdf07037 181
BoulusAJ 0:8e87cdf07037 182 // Reset PID
BoulusAJ 0:8e87cdf07037 183 C1.reset();
BoulusAJ 0:8e87cdf07037 184
BoulusAJ 0:8e87cdf07037 185 pc.printf("Hello World!\n\r");
BoulusAJ 1:d3406369c297 186 // ControllerLoopTimer.attach(&updateControllers, Ts);
BoulusAJ 1:d3406369c297 187
BoulusAJ 1:d3406369c297 188
BoulusAJ 0:8e87cdf07037 189
BoulusAJ 1:d3406369c297 190 while(1)
BoulusAJ 0:8e87cdf07037 191 {
BoulusAJ 1:d3406369c297 192 Loop.reset();
BoulusAJ 1:d3406369c297 193
BoulusAJ 0:8e87cdf07037 194 // Counter
BoulusAJ 0:8e87cdf07037 195 LoopCounter++;
BoulusAJ 0:8e87cdf07037 196
BoulusAJ 0:8e87cdf07037 197 // Acquire Velocity
BoulusAJ 0:8e87cdf07037 198 Velocity_Voltage_Read = Velocity_Voltage_Input.read();
BoulusAJ 0:8e87cdf07037 199 Velocity_Voltage = 3.25*(Velocity_Voltage_Read);
BoulusAJ 0:8e87cdf07037 200 Velocity_rpm = VoltageToVelocity(Velocity_Voltage);
BoulusAJ 0:8e87cdf07037 201 Velocity = Velocity_rpm*2.0*pi/60.0;
BoulusAJ 0:8e87cdf07037 202
BoulusAJ 0:8e87cdf07037 203 // Aquire Raw Acceleration and Gyro Data form the IMU
BoulusAJ 0:8e87cdf07037 204 mpu.getMotion6(&AccX_Raw, &AccY_Raw, &AccZ_Raw, &GyroX_Raw, &GyroY_Raw, &GyroZ_Raw);
BoulusAJ 0:8e87cdf07037 205
BoulusAJ 0:8e87cdf07037 206 // -------------- Convert Raw data to SI Units --------------------
BoulusAJ 0:8e87cdf07037 207
BoulusAJ 0:8e87cdf07037 208 //Convert Acceleration Raw Data to (ms^-2) - (Settings of +/- 4g)
BoulusAJ 0:8e87cdf07037 209 AccX_g = AccX_Raw / 8192.0f;
BoulusAJ 0:8e87cdf07037 210 AccY_g = AccY_Raw / 8192.0f;
BoulusAJ 0:8e87cdf07037 211 AccZ_g = AccZ_Raw / 8192.0f;
BoulusAJ 0:8e87cdf07037 212
BoulusAJ 0:8e87cdf07037 213 //Convert Gyroscope Raw Data to Degrees per second
BoulusAJ 0:8e87cdf07037 214 GyroX_Degrees = GyroX_Raw / 32.768f; // (2^15/1000 = 32.768)
BoulusAJ 0:8e87cdf07037 215 GyroY_Degrees = GyroY_Raw / 32.768f; // (2^15/1000 = 32.768)
BoulusAJ 0:8e87cdf07037 216 GyroZ_Degrees = GyroZ_Raw / 32.768f; // (2^15/1000 = 32.768)
BoulusAJ 0:8e87cdf07037 217
BoulusAJ 0:8e87cdf07037 218 //Convert Gyroscope Raw Data to Degrees per second
BoulusAJ 0:8e87cdf07037 219 GyroZ_RadiansPerSecond = (GyroZ_Raw / 32.768f)* pi/180.0f;
BoulusAJ 0:8e87cdf07037 220
BoulusAJ 0:8e87cdf07037 221 // ----- Combine Accelerometer Data and Gyro Data to Get Angle ------
BoulusAJ 0:8e87cdf07037 222
BoulusAJ 0:8e87cdf07037 223 Cuboid_Angle_Radians = -1*atan2(-FilterAccX(AccX_g), FilterAccY(AccY_g)) + 0.7854f + FilterGyro(GyroZ_RadiansPerSecond); // Check later if fast enough!!
BoulusAJ 0:8e87cdf07037 224 Cuboid_Angle_Degrees = Cuboid_Angle_Radians*180.0f/pi;
BoulusAJ 0:8e87cdf07037 225
BoulusAJ 0:8e87cdf07037 226
BoulusAJ 0:8e87cdf07037 227 // ------------------------- Controller -----------------------------
BoulusAJ 0:8e87cdf07037 228 // Current Input Updater - Amperes
BoulusAJ 0:8e87cdf07037 229 // Loop 1
BoulusAJ 0:8e87cdf07037 230 Loop1_output = Cuboid_Angle_Radians*K_SS_Controller[0];
BoulusAJ 0:8e87cdf07037 231 // Loop 2
BoulusAJ 0:8e87cdf07037 232 Loop2_output = GyroZ_RadiansPerSecond*K_SS_Controller[1];
BoulusAJ 0:8e87cdf07037 233 // PI Controller
BoulusAJ 0:8e87cdf07037 234 PID_Input = Desired_input - Velocity;
BoulusAJ 0:8e87cdf07037 235 PID_Output = C1.update(PID_Input);
BoulusAJ 0:8e87cdf07037 236 /*
BoulusAJ 0:8e87cdf07037 237 //PID_Input = 20.0 - Velocity;
BoulusAJ 0:8e87cdf07037 238 //PID_Output = C1.update(-1*PID_Input);
BoulusAJ 0:8e87cdf07037 239 */
BoulusAJ 0:8e87cdf07037 240
BoulusAJ 0:8e87cdf07037 241 // System input
BoulusAJ 0:8e87cdf07037 242 Sys_input_Amps = PID_Output - Loop1_output - Loop2_output;
BoulusAJ 0:8e87cdf07037 243 // Sys_input_Amps = - Loop1_output - Loop2_output;
BoulusAJ 0:8e87cdf07037 244 if (Sys_input_Amps > uMax2) {
BoulusAJ 0:8e87cdf07037 245 Sys_input_Amps = uMax2;
BoulusAJ 0:8e87cdf07037 246 }
BoulusAJ 0:8e87cdf07037 247 if (Sys_input_Amps < uMin2) {
BoulusAJ 0:8e87cdf07037 248 Sys_input_Amps = uMin2;
BoulusAJ 0:8e87cdf07037 249 }
BoulusAJ 0:8e87cdf07037 250
BoulusAJ 0:8e87cdf07037 251 // Scaling the controller output from -15 A --> 15 A to 0 V --> 1 V
BoulusAJ 0:8e87cdf07037 252 VoltageOut.write(CurrentToVoltage(Sys_input_Amps));
BoulusAJ 0:8e87cdf07037 253
BoulusAJ 0:8e87cdf07037 254 //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 255 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 256
BoulusAJ 0:8e87cdf07037 257 // pc.printf("%i; %0.6f, \n\r", LoopCounter, Loop.read());
BoulusAJ 1:d3406369c297 258
BoulusAJ 1:d3406369c297 259 //
BoulusAJ 1:d3406369c297 260
BoulusAJ 1:d3406369c297 261 // wait(0.0025);
BoulusAJ 1:d3406369c297 262 // pc.printf(" %0.5f \n\r", Loop.read());
BoulusAJ 1:d3406369c297 263
BoulusAJ 1:d3406369c297 264 LoopTime = Loop.read();
BoulusAJ 1:d3406369c297 265 while(LoopTime < Ts) {
BoulusAJ 1:d3406369c297 266 LoopTime = Loop.read();
BoulusAJ 1:d3406369c297 267 }
BoulusAJ 1:d3406369c297 268
BoulusAJ 1:d3406369c297 269 pc.printf(" %0.5f \n\r", LoopTime);
BoulusAJ 0:8e87cdf07037 270
BoulusAJ 0:8e87cdf07037 271 //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 272
BoulusAJ 0:8e87cdf07037 273 /*
BoulusAJ 0:8e87cdf07037 274 // Print Data
BoulusAJ 0:8e87cdf07037 275 if(++k >= 300) {
BoulusAJ 0:8e87cdf07037 276 k = 0;
BoulusAJ 0:8e87cdf07037 277 //pc.printf("%0.8f\n\r", Loop.read());
BoulusAJ 0:8e87cdf07037 278 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 279
BoulusAJ 0:8e87cdf07037 280 }
BoulusAJ 0:8e87cdf07037 281 */
BoulusAJ 0:8e87cdf07037 282
BoulusAJ 0:8e87cdf07037 283 }
BoulusAJ 1:d3406369c297 284 }
BoulusAJ 0:8e87cdf07037 285
BoulusAJ 0:8e87cdf07037 286
BoulusAJ 0:8e87cdf07037 287
BoulusAJ 0:8e87cdf07037 288
BoulusAJ 0:8e87cdf07037 289
BoulusAJ 0:8e87cdf07037 290 //-------------------------------------------------------------------
BoulusAJ 0:8e87cdf07037 291 //--------------------------Example Codes-----------------------------
BoulusAJ 0:8e87cdf07037 292
BoulusAJ 0:8e87cdf07037 293 // This code demonstrates how to change the Low Pass Filter Bandwidth
BoulusAJ 0:8e87cdf07037 294 /*
BoulusAJ 0:8e87cdf07037 295 pc.printf("Digital Low Pass Filter Bandwidth: %u \n\r", mpu.getDLPFMode());
BoulusAJ 0:8e87cdf07037 296
BoulusAJ 0:8e87cdf07037 297 mpu.setDLPFMode(MPU6050_DLPF_BW_42);
BoulusAJ 0:8e87cdf07037 298 wait(0.1);
BoulusAJ 0:8e87cdf07037 299 pc.printf("Digital Low Pass Filter Bandwidth: %u \n\r", mpu.getDLPFMode());
BoulusAJ 0:8e87cdf07037 300 */
BoulusAJ 0:8e87cdf07037 301 // ----------------
BoulusAJ 0:8e87cdf07037 302
BoulusAJ 0:8e87cdf07037 303 // This code demonstrates how to change the scale settings for the Gyro scope
BoulusAJ 0:8e87cdf07037 304 /*
BoulusAJ 0:8e87cdf07037 305 pc.printf("Gyro Range Scale: %u \n\r", mpu.getFullScaleGyroRange());
BoulusAJ 0:8e87cdf07037 306 int Temp123 = 0;
BoulusAJ 0:8e87cdf07037 307 while(Temp123 < 5) {
BoulusAJ 0:8e87cdf07037 308
BoulusAJ 0:8e87cdf07037 309 wait(0.4);
BoulusAJ 0:8e87cdf07037 310 mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
BoulusAJ 0:8e87cdf07037 311 //writing current accelerometer and gyro position
BoulusAJ 0:8e87cdf07037 312 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 313 Temp123 = Temp123 + 1;
BoulusAJ 0:8e87cdf07037 314 }
BoulusAJ 0:8e87cdf07037 315 Temp123 = 0;
BoulusAJ 0:8e87cdf07037 316
BoulusAJ 0:8e87cdf07037 317 mpu.setFullScaleGyroRange(3u);
BoulusAJ 0:8e87cdf07037 318 wait(0.1);
BoulusAJ 0:8e87cdf07037 319 pc.printf("Gyro Range Scale: %u \n\r", mpu.getFullScaleGyroRange());
BoulusAJ 0:8e87cdf07037 320 while(Temp123 < 5) {
BoulusAJ 0:8e87cdf07037 321
BoulusAJ 0:8e87cdf07037 322 wait(0.4);
BoulusAJ 0:8e87cdf07037 323 mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
BoulusAJ 0:8e87cdf07037 324 //writing current accelerometer and gyro position
BoulusAJ 0:8e87cdf07037 325 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 326 Temp123 = Temp123 + 1;
BoulusAJ 0:8e87cdf07037 327 }
BoulusAJ 0:8e87cdf07037 328 Temp123 = 0;
BoulusAJ 0:8e87cdf07037 329 */
BoulusAJ 0:8e87cdf07037 330
BoulusAJ 0:8e87cdf07037 331 /*
BoulusAJ 0:8e87cdf07037 332 while(1) {
BoulusAJ 0:8e87cdf07037 333 wait(0.4);
BoulusAJ 0:8e87cdf07037 334 mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
BoulusAJ 0:8e87cdf07037 335 //writing current accelerometer and gyro position
BoulusAJ 0:8e87cdf07037 336 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 337 }
BoulusAJ 0:8e87cdf07037 338 */
BoulusAJ 0:8e87cdf07037 339 // ------------------
BoulusAJ 0:8e87cdf07037 340
BoulusAJ 0:8e87cdf07037 341
BoulusAJ 0:8e87cdf07037 342 // Printing Register Values
BoulusAJ 0:8e87cdf07037 343 //printf("i2c SCL : 0x%x\r\n", *((unsigned int *)0x40004508));
BoulusAJ 0:8e87cdf07037 344
BoulusAJ 0:8e87cdf07037 345 // ------------------
BoulusAJ 0:8e87cdf07037 346
BoulusAJ 0:8e87cdf07037 347 // Velocity Measurement Code for Designing a Filter
BoulusAJ 0:8e87cdf07037 348 /*
BoulusAJ 0:8e87cdf07037 349 if (aaa < (600*3)) {
BoulusAJ 0:8e87cdf07037 350 if (++aaa <= 900) {
BoulusAJ 0:8e87cdf07037 351 VoltageOut.write(CurrentToVoltage(1.0f));
BoulusAJ 0:8e87cdf07037 352 } else {
BoulusAJ 0:8e87cdf07037 353 VoltageOut.write(CurrentToVoltage(-1.0f));
BoulusAJ 0:8e87cdf07037 354 }
BoulusAJ 0:8e87cdf07037 355 pc.printf("%0.8f\n\r", Velocity_Voltage_Read);
BoulusAJ 0:8e87cdf07037 356 } else {
BoulusAJ 0:8e87cdf07037 357 VoltageOut.write(CurrentToVoltage(0.0f));
BoulusAJ 0:8e87cdf07037 358 }
BoulusAJ 0:8e87cdf07037 359 */