Cube_Mini_Solution

Dependencies:   mbed QEI MPU6050 BLE_API nRF51822 MCP4725 eMPL_MPU6050

Committer:
BoulusAJ
Date:
Thu May 28 18:14:34 2020 +0000
Revision:
13:41edfb8e3b3c
Parent:
12:ba4ce3fa4f53
Child:
14:fe003a27018d
Template Version 1.0

Who changed what in which revision?

UserRevisionLine numberNew contents of line
BoulusAJ 0:8e87cdf07037 1 /*
BoulusAJ 0:8e87cdf07037 2 Boulus Abu Joudom
BoulusAJ 13:41edfb8e3b3c 3 Cube Mini - Prototype 1 V1
BoulusAJ 13:41edfb8e3b3c 4 Mbed Version: May 28, 2020. 18:00
BoulusAJ 0:8e87cdf07037 5 ZHAW - IMS
BoulusAJ 0:8e87cdf07037 6
BoulusAJ 13:41edfb8e3b3c 7 Cubiod Balancing Experiment - Control Lab
BoulusAJ 13:41edfb8e3b3c 8 Template Version: 1.0
BoulusAJ 13:41edfb8e3b3c 9 Seeed Tiny Microcontroller on Cuboid 1.0
BoulusAJ 13:41edfb8e3b3c 10 */
BoulusAJ 13:41edfb8e3b3c 11
BoulusAJ 13:41edfb8e3b3c 12 /*
BoulusAJ 13:41edfb8e3b3c 13 Settings for Maxon ESCON controller (upload via ESCON Studio)
BoulusAJ 13:41edfb8e3b3c 14 Escon Studio file Location: ...
BoulusAJ 13:41edfb8e3b3c 15
BoulusAJ 13:41edfb8e3b3c 16 Hardware Connections
BoulusAJ 13:41edfb8e3b3c 17 Serial PC Communication
BoulusAJ 13:41edfb8e3b3c 18 UART_TX p9
BoulusAJ 13:41edfb8e3b3c 19 UART_RX p11
BoulusAJ 13:41edfb8e3b3c 20 IMU SDA and SCL
BoulusAJ 13:41edfb8e3b3c 21 MPU6050_SDA p12
BoulusAJ 13:41edfb8e3b3c 22 MPU6050_SCL p13
BoulusAJ 13:41edfb8e3b3c 23 Velocity Input as Analogue Signal from Escon
BoulusAJ 13:41edfb8e3b3c 24 Pin 5
BoulusAJ 13:41edfb8e3b3c 25 Current output to Escon occurs through I2C connection to a DAC (Digital to Analogue Converter). DAC outputs Analogue voltage to Escon
BoulusAJ 13:41edfb8e3b3c 26 Pin SDA p3
BoulusAJ 13:41edfb8e3b3c 27 Pin SCL P4
BoulusAJ 13:41edfb8e3b3c 28
BoulusAJ 13:41edfb8e3b3c 29 Notes
BoulusAJ 13:41edfb8e3b3c 30 The Maximum output Current allowed is 13A and the Minimum is -13A !!!
BoulusAJ 13:41edfb8e3b3c 31 All needed Libraries for the IMU (Accelerometer and Gyroscope), DAC are included.
BoulusAJ 13:41edfb8e3b3c 32 The PID is to be written by students. Use PID_Cntrl.cpp and PID_Cntrl.h as templates
BoulusAJ 13:41edfb8e3b3c 33 ...
BoulusAJ 13:41edfb8e3b3c 34
BoulusAJ 13:41edfb8e3b3c 35
BoulusAJ 0:8e87cdf07037 36 */
BoulusAJ 0:8e87cdf07037 37 // Libraries
BoulusAJ 13:41edfb8e3b3c 38 #include <stdbool.h>
BoulusAJ 0:8e87cdf07037 39 #include "mbed.h"
BoulusAJ 13:41edfb8e3b3c 40 #include "MPU6050.h" // IMU Library
BoulusAJ 13:41edfb8e3b3c 41 #include "mcp4725.h" // DAC Library
BoulusAJ 13:41edfb8e3b3c 42 #include "IIR_filter.h" // Filter Library (Please go to the Filter Library and add in your Code)
BoulusAJ 13:41edfb8e3b3c 43 #include "LinearCharacteristics.h" // Linear Scaling Library
BoulusAJ 13:41edfb8e3b3c 44 #include "PID_Cntrl.h" // PID Library (Please go to PID Library and add in your PID Code)
BoulusAJ 13:41edfb8e3b3c 45
BoulusAJ 0:8e87cdf07037 46
BoulusAJ 0:8e87cdf07037 47 // Serial PC Communication
BoulusAJ 0:8e87cdf07037 48 #define UART_TX p9
BoulusAJ 0:8e87cdf07037 49 #define UART_RX p11
BoulusAJ 0:8e87cdf07037 50
BoulusAJ 0:8e87cdf07037 51 // IMU SDA and SCL
BoulusAJ 0:8e87cdf07037 52 #define MPU6050_SDA p12
BoulusAJ 0:8e87cdf07037 53 #define MPU6050_SCL p13
BoulusAJ 0:8e87cdf07037 54
BoulusAJ 12:ba4ce3fa4f53 55 // PI Values
BoulusAJ 0:8e87cdf07037 56 #define PI 3.1415927f
BoulusAJ 0:8e87cdf07037 57 #define pi 3.1415927f
BoulusAJ 0:8e87cdf07037 58
BoulusAJ 13:41edfb8e3b3c 59 // PC Serial Connection
BoulusAJ 13:41edfb8e3b3c 60 Serial pc(UART_TX, UART_RX); // serial connection via USB - programmer
BoulusAJ 0:8e87cdf07037 61
BoulusAJ 12:ba4ce3fa4f53 62 // -------------------------------
BoulusAJ 12:ba4ce3fa4f53 63 // Analog/Digitsl I/O Definitions
BoulusAJ 12:ba4ce3fa4f53 64 // -------------------------------
BoulusAJ 13:41edfb8e3b3c 65 AnalogIn Velocity_Voltage_Input(p5); // Velocity Input as Analogue Signal from Escon
BoulusAJ 13:41edfb8e3b3c 66 DigitalOut Pin_3V3(p30); // Defining P30 as 3V3 Pin for internal use. Please do not modify.
BoulusAJ 12:ba4ce3fa4f53 67
BoulusAJ 13:41edfb8e3b3c 68 // -------------------------------
BoulusAJ 13:41edfb8e3b3c 69 // Initialization of Values and Variables
BoulusAJ 13:41edfb8e3b3c 70 // -------------------------------
BoulusAJ 0:8e87cdf07037 71
BoulusAJ 0:8e87cdf07037 72 // Sample time of main loops
BoulusAJ 13:41edfb8e3b3c 73 float Ts = 0.007; // Around 143 Hz. Do not change. THis is the lowest value possible if printing to a serial connection is needed.
BoulusAJ 0:8e87cdf07037 74
BoulusAJ 0:8e87cdf07037 75 // MPU 6050 Variables - Acceleration and Gyroscope Raw and Converted Data Variables
BoulusAJ 0:8e87cdf07037 76 int16_t AccX_Raw, AccY_Raw, AccZ_Raw;
BoulusAJ 0:8e87cdf07037 77 int16_t GyroX_Raw, GyroY_Raw, GyroZ_Raw;
BoulusAJ 0:8e87cdf07037 78 double AccX_g, AccY_g, AccZ_g;
BoulusAJ 0:8e87cdf07037 79 double GyroX_Degrees, GyroY_Degrees, GyroZ_Degrees, GyroZ_RadiansPerSecond;
BoulusAJ 0:8e87cdf07037 80
BoulusAJ 0:8e87cdf07037 81 // printf Variable
BoulusAJ 0:8e87cdf07037 82 int k = 0;
BoulusAJ 13:41edfb8e3b3c 83
BoulusAJ 13:41edfb8e3b3c 84
BoulusAJ 13:41edfb8e3b3c 85 // -------------------------------
BoulusAJ 13:41edfb8e3b3c 86 // Variables: Here define variables such as gains etc.
BoulusAJ 13:41edfb8e3b3c 87 // -------------------------------
BoulusAJ 13:41edfb8e3b3c 88
BoulusAJ 13:41edfb8e3b3c 89
BoulusAJ 13:41edfb8e3b3c 90 // Accelerometer and Gyroscope
BoulusAJ 13:41edfb8e3b3c 91
BoulusAJ 13:41edfb8e3b3c 92
BoulusAJ 13:41edfb8e3b3c 93 // Cube Angle and Speed Variables
BoulusAJ 13:41edfb8e3b3c 94
BoulusAJ 13:41edfb8e3b3c 95
BoulusAJ 13:41edfb8e3b3c 96 // Low pass filter variables
BoulusAJ 13:41edfb8e3b3c 97
BoulusAJ 0:8e87cdf07037 98
BoulusAJ 0:8e87cdf07037 99 // Flywheel Position and Velocity variables
BoulusAJ 13:41edfb8e3b3c 100
BoulusAJ 0:8e87cdf07037 101
BoulusAJ 13:41edfb8e3b3c 102 // -------------------------------
BoulusAJ 13:41edfb8e3b3c 103 // Controller Variables: Such as SS Controller Values and Saturation Limits
BoulusAJ 13:41edfb8e3b3c 104 // -------------------------------
BoulusAJ 13:41edfb8e3b3c 105 // Variables concerning the Controller Design is in reference to the Matlab and Simulink Files ..............
BoulusAJ 0:8e87cdf07037 106
BoulusAJ 0:8e87cdf07037 107 // Sate Space Controller Values
BoulusAJ 13:41edfb8e3b3c 108
BoulusAJ 0:8e87cdf07037 109
BoulusAJ 0:8e87cdf07037 110 // Controller Variables
BoulusAJ 13:41edfb8e3b3c 111
BoulusAJ 13:41edfb8e3b3c 112
BoulusAJ 13:41edfb8e3b3c 113 // PID (PI Parameters)
BoulusAJ 13:41edfb8e3b3c 114
BoulusAJ 0:8e87cdf07037 115
BoulusAJ 0:8e87cdf07037 116 // Saturation Parameters
BoulusAJ 0:8e87cdf07037 117
BoulusAJ 0:8e87cdf07037 118
BoulusAJ 13:41edfb8e3b3c 119 // Cuboid Escon Input Limits in Amps
BoulusAJ 13:41edfb8e3b3c 120 const float uMin = -13.0f; // Minimum Current Allowed
BoulusAJ 13:41edfb8e3b3c 121 const float uMax = 13.0f; // Maximum Current Allowe
BoulusAJ 0:8e87cdf07037 122
BoulusAJ 0:8e87cdf07037 123
BoulusAJ 13:41edfb8e3b3c 124 // -------------------------------
BoulusAJ 13:41edfb8e3b3c 125 // User-Defined Functions
BoulusAJ 13:41edfb8e3b3c 126 // -------------------------------
BoulusAJ 0:8e87cdf07037 127
BoulusAJ 0:8e87cdf07037 128 // Interrupts
BoulusAJ 0:8e87cdf07037 129 Ticker ControllerLoopTimer; // Interrupt for control loop
BoulusAJ 0:8e87cdf07037 130
BoulusAJ 13:41edfb8e3b3c 131
BoulusAJ 13:41edfb8e3b3c 132 // -------------------------------
BoulusAJ 13:41edfb8e3b3c 133 // Functions
BoulusAJ 13:41edfb8e3b3c 134 // -------------------------------
BoulusAJ 13:41edfb8e3b3c 135
BoulusAJ 13:41edfb8e3b3c 136 // DAC - For Current output
BoulusAJ 0:8e87cdf07037 137 MCP4725 VoltageOut(p3, p4, MCP4725::Fast400kHz, 0);
BoulusAJ 0:8e87cdf07037 138
BoulusAJ 0:8e87cdf07037 139 // IMU - MPU6050 Functions and I2C Functions
BoulusAJ 0:8e87cdf07037 140 I2C i2c(MPU6050_SDA, MPU6050_SCL);
BoulusAJ 0:8e87cdf07037 141 MPU6050 mpu(i2c);
BoulusAJ 0:8e87cdf07037 142
BoulusAJ 0:8e87cdf07037 143 // Timers
BoulusAJ 0:8e87cdf07037 144 Timer Loop;
BoulusAJ 0:8e87cdf07037 145
BoulusAJ 13:41edfb8e3b3c 146 // Accelerometer and Gyroscope Filters
BoulusAJ 13:41edfb8e3b3c 147
BoulusAJ 13:41edfb8e3b3c 148 // PID (PI) Controller
BoulusAJ 13:41edfb8e3b3c 149
BoulusAJ 13:41edfb8e3b3c 150
BoulusAJ 0:8e87cdf07037 151 // ----- User defined functions -----------
BoulusAJ 13:41edfb8e3b3c 152
BoulusAJ 13:41edfb8e3b3c 153 // Controller loop (via interrupt)
BoulusAJ 13:41edfb8e3b3c 154 void updateControllers(void);
BoulusAJ 13:41edfb8e3b3c 155
BoulusAJ 13:41edfb8e3b3c 156 // Linear Scaler - Follow the mapping mentioned in the Notes above
BoulusAJ 0:8e87cdf07037 157
BoulusAJ 12:ba4ce3fa4f53 158 //******************************************************************************
BoulusAJ 12:ba4ce3fa4f53 159 //---------- Main Loop -------------
BoulusAJ 12:ba4ce3fa4f53 160 //******************************************************************************
BoulusAJ 0:8e87cdf07037 161 int main()
BoulusAJ 0:8e87cdf07037 162 {
BoulusAJ 12:ba4ce3fa4f53 163 // Microcontroller initialization
BoulusAJ 2:e088fa08e244 164 Pin_3V3.write(1);
BoulusAJ 12:ba4ce3fa4f53 165 *((unsigned int *)0x40007504) = 0x400A; // Configure the ADC to the internel reference of 1.2V.
BoulusAJ 13:41edfb8e3b3c 166 // IMU - MPU6050 initialization - Do NOT Modify
BoulusAJ 12:ba4ce3fa4f53 167 mpu.initialize();
BoulusAJ 13:41edfb8e3b3c 168 mpu.setDLPFMode(MPU6050_DLPF_BW_42); // Set Low Pass Filter Bandwidth to 44Hz (4.9ms) for the Acc and 42Hz (4.8ms) for the Gyroscope
BoulusAJ 12:ba4ce3fa4f53 169 mpu.setFullScaleGyroRange(2u); // Change the scale of the Gyroscope to +/- 1000 degrees/sec
BoulusAJ 12:ba4ce3fa4f53 170 mpu.setFullScaleAccelRange(MPU6050_ACCEL_FS_4); // Change the scale of the Accelerometer to +/- 4g - Sensitivity: 4096 LSB/mg
BoulusAJ 13:41edfb8e3b3c 171
BoulusAJ 13:41edfb8e3b3c 172 // Serial Communication
BoulusAJ 13:41edfb8e3b3c 173 pc.baud(115200);
BoulusAJ 13:41edfb8e3b3c 174
BoulusAJ 13:41edfb8e3b3c 175 VoltageOut.write(2.5); // Output Zero Current to the Motor
BoulusAJ 0:8e87cdf07037 176
BoulusAJ 13:41edfb8e3b3c 177 // Reset Filters and PID Controller
BoulusAJ 11:af811e6f9a04 178
BoulusAJ 12:ba4ce3fa4f53 179 // Control Loop Interrupt at Ts, 200Hz Rate
BoulusAJ 7:07c5b6709d87 180 ControllerLoopTimer.attach(&updateControllers, Ts);
BoulusAJ 7:07c5b6709d87 181
BoulusAJ 7:07c5b6709d87 182 }
BoulusAJ 12:ba4ce3fa4f53 183 //******************************************************************************
BoulusAJ 13:41edfb8e3b3c 184 //---------- Control Loop (called via interrupt) -------------
BoulusAJ 12:ba4ce3fa4f53 185 //******************************************************************************
BoulusAJ 7:07c5b6709d87 186 void updateControllers(void)
BoulusAJ 7:07c5b6709d87 187 {
BoulusAJ 13:41edfb8e3b3c 188
BoulusAJ 13:41edfb8e3b3c 189 // Acquire Velocity
BoulusAJ 13:41edfb8e3b3c 190 Velocity_Voltage = 3.0*1.1978917*(Velocity_Voltage_Input.read()); // *1.2V because the Vref is 1.2V // *3 because the prescaling is 1/3 // *1.1978917 is for Calibration purposes instead of 1.2
BoulusAJ 13:41edfb8e3b3c 191 // Velocity = .... Refer to mapping
BoulusAJ 13:41edfb8e3b3c 192
BoulusAJ 8:71babe904b92 193
BoulusAJ 12:ba4ce3fa4f53 194 // Aquire Raw Acceleration and Gyro Data form the IMU
BoulusAJ 12:ba4ce3fa4f53 195 mpu.getMotion6(&AccX_Raw, &AccY_Raw, &AccZ_Raw, &GyroX_Raw, &GyroY_Raw, &GyroZ_Raw);
BoulusAJ 0:8e87cdf07037 196
BoulusAJ 7:07c5b6709d87 197 // -------------- Convert Raw data to SI Units --------------------
BoulusAJ 7:07c5b6709d87 198
BoulusAJ 7:07c5b6709d87 199 //Convert Acceleration Raw Data to (ms^-2) - (Settings of +/- 4g)
BoulusAJ 13:41edfb8e3b3c 200 AccX_g = AccX_Raw / 8192.0f; AccY_g = AccY_Raw / 8192.0f; AccZ_g = AccZ_Raw / 8192.0f;
BoulusAJ 0:8e87cdf07037 201
BoulusAJ 13:41edfb8e3b3c 202 //Convert Gyroscope Raw Data to Degrees per second - (2^15/1000 = 32.768)
BoulusAJ 13:41edfb8e3b3c 203 GyroX_Degrees = GyroX_Raw / 32.768f; GyroY_Degrees = GyroY_Raw / 32.768f; GyroZ_Degrees = GyroZ_Raw / 32.768f;
BoulusAJ 0:8e87cdf07037 204
BoulusAJ 7:07c5b6709d87 205 //Convert Gyroscope Raw Data to Degrees per second
BoulusAJ 7:07c5b6709d87 206 GyroZ_RadiansPerSecond = (GyroZ_Raw / 32.768f)* pi/180.0f;
BoulusAJ 0:8e87cdf07037 207
BoulusAJ 7:07c5b6709d87 208 // ----- Combine Accelerometer Data and Gyro Data to Get Angle ------
BoulusAJ 0:8e87cdf07037 209
BoulusAJ 13:41edfb8e3b3c 210
BoulusAJ 0:8e87cdf07037 211
BoulusAJ 0:8e87cdf07037 212
BoulusAJ 7:07c5b6709d87 213 // ------------------------- Controller -----------------------------
BoulusAJ 10:4e9899cef12c 214
BoulusAJ 13:41edfb8e3b3c 215 // ......
BoulusAJ 12:ba4ce3fa4f53 216
BoulusAJ 12:ba4ce3fa4f53 217 // Print Data
BoulusAJ 13:41edfb8e3b3c 218 if(++k >= 100) {
BoulusAJ 12:ba4ce3fa4f53 219 k = 0;
BoulusAJ 12:ba4ce3fa4f53 220 pc.printf("Some Outputs: %0.6f, %0.6f\n\r", Sys_input_Amps, Velocity);
BoulusAJ 12:ba4ce3fa4f53 221 }
BoulusAJ 12:ba4ce3fa4f53 222
BoulusAJ 13:41edfb8e3b3c 223
BoulusAJ 1:d3406369c297 224 }
BoulusAJ 0:8e87cdf07037 225