Cube_Mini_Solution

Dependencies:   mbed QEI MPU6050 BLE_API nRF51822 MCP4725 eMPL_MPU6050

Committer:
BoulusAJ
Date:
Fri Jan 31 17:59:21 2020 +0000
Revision:
0:8e87cdf07037
Child:
1:d3406369c297
Latest changes.

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