Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
RTOS-Setup/src/setup.cpp@49:c882f9135033, 2014-05-19 (annotated)
- Committer:
- pHysiX
- Date:
- Mon May 19 13:33:34 2014 +0000
- Revision:
- 49:c882f9135033
- Parent:
- 48:9dbdc4144f00
Changing RtosTimer functions into Threads
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
pHysiX | 22:ef8aa9728013 | 1 | /* File: Setup.h |
pHysiX | 22:ef8aa9728013 | 2 | * Author: Trung Tin Ian HUA |
pHysiX | 22:ef8aa9728013 | 3 | * Date: May 2014 |
pHysiX | 22:ef8aa9728013 | 4 | * Purpose: Setup code to initialise all device |
pHysiX | 22:ef8aa9728013 | 5 | */ |
pHysiX | 0:8c28fac22d27 | 6 | #include "setup.h" |
pHysiX | 0:8c28fac22d27 | 7 | #include "tasks.h" |
pHysiX | 0:8c28fac22d27 | 8 | |
pHysiX | 4:01921a136f58 | 9 | Serial BT(p28, p27); |
pHysiX | 49:c882f9135033 | 10 | Serial PC(USBTX, USBRX); |
pHysiX | 4:01921a136f58 | 11 | DigitalOut BT_CMD(p29); |
pHysiX | 48:9dbdc4144f00 | 12 | //MPU6050 imu(p9, p10); |
pHysiX | 48:9dbdc4144f00 | 13 | MPU6050_NB imu_nb(p9, p10); |
pHysiX | 22:ef8aa9728013 | 14 | MPL3115A2 altimeter(p9, p10); |
pHysiX | 48:9dbdc4144f00 | 15 | AnalogIn voltageSense(p20); |
pHysiX | 12:953d25061417 | 16 | #ifdef ENABLE_COMPASS |
pHysiX | 12:953d25061417 | 17 | HMC5883L compass(p9, p10); |
pHysiX | 12:953d25061417 | 18 | #endif |
pHysiX | 12:953d25061417 | 19 | |
pHysiX | 48:9dbdc4144f00 | 20 | float KP_YAW_RATE = P_Y_RATE; |
pHysiX | 39:02782ad251db | 21 | float KP_PITCH_RATE = P_P_RATE; |
pHysiX | 39:02782ad251db | 22 | float KP_ROLL_RATE = P_R_RATE; |
pHysiX | 39:02782ad251db | 23 | |
pHysiX | 39:02782ad251db | 24 | float TI_YAW_RATE = TI_Y_RATE; |
pHysiX | 39:02782ad251db | 25 | float TI_PITCH_RATE = TI_P_RATE; |
pHysiX | 39:02782ad251db | 26 | float TI_ROLL_RATE = TI_R_RATE; |
pHysiX | 12:953d25061417 | 27 | |
pHysiX | 39:02782ad251db | 28 | float KP_PITCH_ATTITUDE = P_P_ATTITUDE; |
pHysiX | 39:02782ad251db | 29 | float KP_ROLL_ATTITUDE = P_R_ATTITUDE; |
pHysiX | 39:02782ad251db | 30 | |
pHysiX | 39:02782ad251db | 31 | float TI_PITCH_ATTITUDE = TI_P_ATTITUDE; |
pHysiX | 39:02782ad251db | 32 | float TI_ROLL_ATTITUDE = TI_R_ATTITUDE; |
pHysiX | 39:02782ad251db | 33 | |
pHysiX | 39:02782ad251db | 34 | PID yawPIDrate(KP_YAW_RATE, TI_Y_RATE, 0.0, TASK2_SLAVE_PERIOD/1000.0); |
pHysiX | 32:7a9be7761c46 | 35 | PID pitchPIDrate(KP_PITCH_RATE, TI_PITCH_RATE, 0.0, TASK2_SLAVE_PERIOD/1000.0); |
pHysiX | 32:7a9be7761c46 | 36 | PID rollPIDrate(KP_ROLL_RATE, TI_ROLL_RATE, 0.0, TASK2_SLAVE_PERIOD/1000.0); |
pHysiX | 20:b193a50a2ba3 | 37 | |
pHysiX | 39:02782ad251db | 38 | PID pitchPIDattitude(KP_PITCH_ATTITUDE, TI_PITCH_ATTITUDE, 0.0, TASK2_MASTER_PERIOD/1000.0); |
pHysiX | 39:02782ad251db | 39 | PID rollPIDattitude(KP_ROLL_ATTITUDE, TI_ROLL_ATTITUDE, 0.0, TASK2_MASTER_PERIOD/1000.0); |
pHysiX | 39:02782ad251db | 40 | |
pHysiX | 3:605fbcb54e75 | 41 | PwmOut ESC[4] = {p21, p22, p23, p24}; |
pHysiX | 3:605fbcb54e75 | 42 | |
pHysiX | 3:605fbcb54e75 | 43 | |
pHysiX | 2:ab967d7b4346 | 44 | |
pHysiX | 34:228d87c45151 | 45 | // ========================== |
pHysiX | 3:605fbcb54e75 | 46 | // === MAIN SETUP ROUTINE === |
pHysiX | 34:228d87c45151 | 47 | // ========================== |
pHysiX | 3:605fbcb54e75 | 48 | bool setupALLdevices(void) |
pHysiX | 48:9dbdc4144f00 | 49 | { |
pHysiX | 3:605fbcb54e75 | 50 | bool error = false; |
pHysiX | 22:ef8aa9728013 | 51 | box_demo = false; |
pHysiX | 27:18b6580eb0b1 | 52 | mode = ATTITUDE; |
pHysiX | 3:605fbcb54e75 | 53 | |
pHysiX | 3:605fbcb54e75 | 54 | if (!setup_ESC()) { |
pHysiX | 49:c882f9135033 | 55 | PC.printf("ESC FAILED!!!\n"); |
pHysiX | 3:605fbcb54e75 | 56 | error = true; |
pHysiX | 3:605fbcb54e75 | 57 | } |
pHysiX | 0:8c28fac22d27 | 58 | |
pHysiX | 49:c882f9135033 | 59 | if (setup_bt()) |
pHysiX | 49:c882f9135033 | 60 | PC.printf("BT established!\n"); |
pHysiX | 3:605fbcb54e75 | 61 | else error = true; |
pHysiX | 3:605fbcb54e75 | 62 | |
pHysiX | 49:c882f9135033 | 63 | if (setup_PID()) |
pHysiX | 49:c882f9135033 | 64 | PC.printf("PID established!\n"); |
pHysiX | 3:605fbcb54e75 | 65 | else error = true; |
pHysiX | 0:8c28fac22d27 | 66 | |
pHysiX | 49:c882f9135033 | 67 | if (setup_mpu6050()) |
pHysiX | 49:c882f9135033 | 68 | PC.printf("MPU6050 established!\n"); |
pHysiX | 3:605fbcb54e75 | 69 | else error = true; |
pHysiX | 3:605fbcb54e75 | 70 | |
pHysiX | 49:c882f9135033 | 71 | if (setup_altimeter()) |
pHysiX | 49:c882f9135033 | 72 | PC.printf("Altimeter established!\n"); |
pHysiX | 48:9dbdc4144f00 | 73 | else { |
pHysiX | 48:9dbdc4144f00 | 74 | error = true; |
pHysiX | 49:c882f9135033 | 75 | PC.printf("ALTIMETER FAILED\n"); |
pHysiX | 48:9dbdc4144f00 | 76 | } |
pHysiX | 22:ef8aa9728013 | 77 | |
pHysiX | 12:953d25061417 | 78 | #ifdef ENABLE_COMPASS |
pHysiX | 12:953d25061417 | 79 | if (setup_compass()) |
pHysiX | 49:c882f9135033 | 80 | PC.printf("Compass established!\n"); |
pHysiX | 12:953d25061417 | 81 | else error = true; |
pHysiX | 12:953d25061417 | 82 | #endif |
pHysiX | 12:953d25061417 | 83 | |
pHysiX | 3:605fbcb54e75 | 84 | return error; |
pHysiX | 3:605fbcb54e75 | 85 | } |
pHysiX | 0:8c28fac22d27 | 86 | |
pHysiX | 3:605fbcb54e75 | 87 | |
pHysiX | 3:605fbcb54e75 | 88 | |
pHysiX | 3:605fbcb54e75 | 89 | |
pHysiX | 34:228d87c45151 | 90 | // ************************* |
pHysiX | 34:228d87c45151 | 91 | // *** ESC SETUP ROUTINE *** |
pHysiX | 34:228d87c45151 | 92 | // ************************* |
pHysiX | 2:ab967d7b4346 | 93 | bool setup_ESC(void) |
pHysiX | 2:ab967d7b4346 | 94 | { |
pHysiX | 3:605fbcb54e75 | 95 | for (int i = 0; i < 4; i++) |
pHysiX | 3:605fbcb54e75 | 96 | ESC[i].period_us(ESC_PERIOD_US); |
pHysiX | 2:ab967d7b4346 | 97 | |
pHysiX | 3:605fbcb54e75 | 98 | for (int i = 0; i < 4; i++) |
pHysiX | 38:ef65533cca32 | 99 | ESC[i].pulsewidth_us(1000); |
pHysiX | 48:9dbdc4144f00 | 100 | |
pHysiX | 38:ef65533cca32 | 101 | for (int i = 0; i < 4; i++) |
pHysiX | 38:ef65533cca32 | 102 | ESCpower[i] = 990; |
pHysiX | 3:605fbcb54e75 | 103 | |
pHysiX | 14:267368c83b6a | 104 | armed = false; |
pHysiX | 14:267368c83b6a | 105 | |
pHysiX | 2:ab967d7b4346 | 106 | return true; |
pHysiX | 2:ab967d7b4346 | 107 | } |
pHysiX | 2:ab967d7b4346 | 108 | |
pHysiX | 3:605fbcb54e75 | 109 | // **************************************************************** |
pHysiX | 3:605fbcb54e75 | 110 | // === BLUETOOTH SETUP ROUTINE === |
pHysiX | 3:605fbcb54e75 | 111 | // **************************************************************** |
pHysiX | 0:8c28fac22d27 | 112 | bool setup_bt(void) |
pHysiX | 0:8c28fac22d27 | 113 | { |
pHysiX | 0:8c28fac22d27 | 114 | BT.baud(115200); |
pHysiX | 22:ef8aa9728013 | 115 | BT_CMD = 0; // Place bluetooth into normal mode |
pHysiX | 0:8c28fac22d27 | 116 | BT.printf("Bluetooth online!\n"); |
pHysiX | 0:8c28fac22d27 | 117 | return true; |
pHysiX | 0:8c28fac22d27 | 118 | } |
pHysiX | 0:8c28fac22d27 | 119 | |
pHysiX | 3:605fbcb54e75 | 120 | // **************************************************************** |
pHysiX | 3:605fbcb54e75 | 121 | // === PID SETUP ROUTINE === |
pHysiX | 3:605fbcb54e75 | 122 | // **************************************************************** |
pHysiX | 3:605fbcb54e75 | 123 | bool setup_PID(void) |
pHysiX | 3:605fbcb54e75 | 124 | { |
pHysiX | 39:02782ad251db | 125 | pitchPIDattitude.setInputLimits(-90.0, 90.0); |
pHysiX | 39:02782ad251db | 126 | pitchPIDattitude.setOutputLimits(-250, 250.0); |
pHysiX | 39:02782ad251db | 127 | pitchPIDattitude.setBias(0.0); |
pHysiX | 39:02782ad251db | 128 | pitchPIDattitude.setMode(AUTO_MODE); |
pHysiX | 22:ef8aa9728013 | 129 | |
pHysiX | 39:02782ad251db | 130 | rollPIDattitude.setInputLimits(-90.0, 90.0); |
pHysiX | 39:02782ad251db | 131 | rollPIDattitude.setOutputLimits(-250, 250.0); |
pHysiX | 39:02782ad251db | 132 | rollPIDattitude.setBias(0.0); |
pHysiX | 39:02782ad251db | 133 | rollPIDattitude.setMode(AUTO_MODE); |
pHysiX | 22:ef8aa9728013 | 134 | |
pHysiX | 21:b642c18eccd1 | 135 | yawPIDrate.setInputLimits(-500.0, 500.0); |
pHysiX | 21:b642c18eccd1 | 136 | yawPIDrate.setOutputLimits(-200.0, 200.0); |
pHysiX | 21:b642c18eccd1 | 137 | yawPIDrate.setBias(0.0); |
pHysiX | 3:605fbcb54e75 | 138 | yawPIDrate.setMode(AUTO_MODE); |
pHysiX | 12:953d25061417 | 139 | |
pHysiX | 21:b642c18eccd1 | 140 | pitchPIDrate.setInputLimits(-500.0, 500.0); |
pHysiX | 21:b642c18eccd1 | 141 | pitchPIDrate.setOutputLimits(-200.0, 200.0); |
pHysiX | 21:b642c18eccd1 | 142 | pitchPIDrate.setBias(0.0); |
pHysiX | 3:605fbcb54e75 | 143 | pitchPIDrate.setMode(AUTO_MODE); |
pHysiX | 22:ef8aa9728013 | 144 | |
pHysiX | 21:b642c18eccd1 | 145 | rollPIDrate.setInputLimits(-500.0, 500.0); |
pHysiX | 21:b642c18eccd1 | 146 | rollPIDrate.setOutputLimits(-200.0, 200.0); |
pHysiX | 21:b642c18eccd1 | 147 | rollPIDrate.setBias(0.0); |
pHysiX | 3:605fbcb54e75 | 148 | rollPIDrate.setMode(AUTO_MODE); |
pHysiX | 48:9dbdc4144f00 | 149 | |
pHysiX | 39:02782ad251db | 150 | pitchPIDattitude.reset(); |
pHysiX | 39:02782ad251db | 151 | rollPIDattitude.reset(); |
pHysiX | 24:54a8cdf17378 | 152 | yawPIDrate.reset(); |
pHysiX | 24:54a8cdf17378 | 153 | pitchPIDrate.reset(); |
pHysiX | 24:54a8cdf17378 | 154 | rollPIDrate.reset(); |
pHysiX | 12:953d25061417 | 155 | |
pHysiX | 3:605fbcb54e75 | 156 | return true; |
pHysiX | 3:605fbcb54e75 | 157 | } |
pHysiX | 3:605fbcb54e75 | 158 | |
pHysiX | 3:605fbcb54e75 | 159 | // **************************************************************** |
pHysiX | 3:605fbcb54e75 | 160 | // === MPU6050 SETUP ROUTINE === |
pHysiX | 3:605fbcb54e75 | 161 | // **************************************************************** |
pHysiX | 3:605fbcb54e75 | 162 | bool imu_available = false; |
pHysiX | 3:605fbcb54e75 | 163 | |
pHysiX | 3:605fbcb54e75 | 164 | /*/ MPU control/status variables: */ |
pHysiX | 3:605fbcb54e75 | 165 | bool dmpReady = false; // set true if DMP init was successful |
pHysiX | 3:605fbcb54e75 | 166 | uint8_t devStatus; // return status after each device operation (0 = success, !0 = error) |
pHysiX | 3:605fbcb54e75 | 167 | uint16_t packetSize; // expected DMP packet size (default is 42 bytes) |
pHysiX | 3:605fbcb54e75 | 168 | |
pHysiX | 0:8c28fac22d27 | 169 | bool setup_mpu6050(void) |
pHysiX | 0:8c28fac22d27 | 170 | { |
pHysiX | 48:9dbdc4144f00 | 171 | MPU6050 imu(p9, p10); |
pHysiX | 48:9dbdc4144f00 | 172 | |
pHysiX | 0:8c28fac22d27 | 173 | imu.reset(); |
pHysiX | 0:8c28fac22d27 | 174 | imu.debugSerial.baud(115200); |
pHysiX | 0:8c28fac22d27 | 175 | wait_ms(5); |
pHysiX | 0:8c28fac22d27 | 176 | |
pHysiX | 0:8c28fac22d27 | 177 | imu.initialize(); |
pHysiX | 0:8c28fac22d27 | 178 | imu_available = imu.testConnection(); |
pHysiX | 0:8c28fac22d27 | 179 | |
pHysiX | 48:9dbdc4144f00 | 180 | imu.debugSerial.printf("imu status...\t\t\t"); |
pHysiX | 0:8c28fac22d27 | 181 | imu_available ? imu.debugSerial.printf("OK!\n") : imu.debugSerial.printf("NOT OK!\n"); |
pHysiX | 0:8c28fac22d27 | 182 | |
pHysiX | 0:8c28fac22d27 | 183 | if (imu_available) { |
pHysiX | 0:8c28fac22d27 | 184 | devStatus = imu.dmpInitialize(); |
pHysiX | 3:605fbcb54e75 | 185 | // supply your own gyro offsets here, scaled for min sensitivity |
pHysiX | 4:01921a136f58 | 186 | //imu.setXGyroOffset(55); |
pHysiX | 4:01921a136f58 | 187 | //imu.setYGyroOffset(3); |
pHysiX | 4:01921a136f58 | 188 | //imu.setZGyroOffset(-2); |
pHysiX | 3:605fbcb54e75 | 189 | //mpu.setZAccelOffset(16282); // 1688 factory default for my test chip |
pHysiX | 0:8c28fac22d27 | 190 | |
pHysiX | 0:8c28fac22d27 | 191 | if(!devStatus) { |
pHysiX | 0:8c28fac22d27 | 192 | imu.setDMPEnabled(true); |
pHysiX | 0:8c28fac22d27 | 193 | while (!imu.getIntDataReadyStatus()); |
pHysiX | 0:8c28fac22d27 | 194 | packetSize = imu.dmpGetFIFOPacketSize(); |
pHysiX | 16:9072cd6fa8d1 | 195 | imu.debugSerial.printf("Packet Size: %d\n", packetSize); |
pHysiX | 0:8c28fac22d27 | 196 | dmpReady = true; |
pHysiX | 16:9072cd6fa8d1 | 197 | |
pHysiX | 16:9072cd6fa8d1 | 198 | int8_t xgOffsetTC = imu.getXGyroOffset(); |
pHysiX | 16:9072cd6fa8d1 | 199 | int8_t ygOffsetTC = imu.getYGyroOffset(); |
pHysiX | 16:9072cd6fa8d1 | 200 | int8_t zgOffsetTC = imu.getZGyroOffset(); |
pHysiX | 16:9072cd6fa8d1 | 201 | |
pHysiX | 16:9072cd6fa8d1 | 202 | imu.debugSerial.printf("X Offset: %4d Y Offset: %4d Z Offset: %4d\n", xgOffsetTC, ygOffsetTC, zgOffsetTC); |
pHysiX | 16:9072cd6fa8d1 | 203 | |
pHysiX | 0:8c28fac22d27 | 204 | } else { |
pHysiX | 0:8c28fac22d27 | 205 | imu.debugSerial.printf("\tDMP setup failed!\n"); |
pHysiX | 0:8c28fac22d27 | 206 | return false; |
pHysiX | 0:8c28fac22d27 | 207 | } |
pHysiX | 0:8c28fac22d27 | 208 | |
pHysiX | 0:8c28fac22d27 | 209 | imu.resetFIFO(); |
pHysiX | 0:8c28fac22d27 | 210 | } else { |
pHysiX | 0:8c28fac22d27 | 211 | return false; |
pHysiX | 0:8c28fac22d27 | 212 | } |
pHysiX | 0:8c28fac22d27 | 213 | |
pHysiX | 48:9dbdc4144f00 | 214 | imu.debugSerial.printf("imu setup routine done!"); |
pHysiX | 0:8c28fac22d27 | 215 | |
pHysiX | 0:8c28fac22d27 | 216 | return dmpReady; |
pHysiX | 0:8c28fac22d27 | 217 | } |
pHysiX | 0:8c28fac22d27 | 218 | |
pHysiX | 22:ef8aa9728013 | 219 | // **************************************************************** |
pHysiX | 22:ef8aa9728013 | 220 | // === MPL3115A2 SETUP ROUTINE === |
pHysiX | 22:ef8aa9728013 | 221 | // **************************************************************** |
pHysiX | 22:ef8aa9728013 | 222 | bool setup_altimeter(void) |
pHysiX | 22:ef8aa9728013 | 223 | { |
pHysiX | 22:ef8aa9728013 | 224 | if (altimeter.init()) |
pHysiX | 22:ef8aa9728013 | 225 | return true; |
pHysiX | 22:ef8aa9728013 | 226 | else |
pHysiX | 22:ef8aa9728013 | 227 | return false; |
pHysiX | 22:ef8aa9728013 | 228 | } |
pHysiX | 22:ef8aa9728013 | 229 | |
pHysiX | 12:953d25061417 | 230 | #ifdef ENABLE_COMPASS |
pHysiX | 12:953d25061417 | 231 | // **************************************************************** |
pHysiX | 12:953d25061417 | 232 | // === HMC5883L SETUP ROUTINE === |
pHysiX | 12:953d25061417 | 233 | // **************************************************************** |
pHysiX | 12:953d25061417 | 234 | bool setup_compass(void) |
pHysiX | 12:953d25061417 | 235 | { |
pHysiX | 12:953d25061417 | 236 | compass.setConfigurationA(AVG8_SAMPLES | OUTPUT_RATE_75); |
pHysiX | 12:953d25061417 | 237 | return true; |
pHysiX | 12:953d25061417 | 238 | } |
pHysiX | 12:953d25061417 | 239 | #endif |