amiro-os / devices / DiWheelDrive / global.hpp @ f3972840
History | View | Annotate | Download (8.153 KB)
1 | 58fe0e0b | Thomas Schöpping | #ifndef AMIRO_GLOBAL_HPP_
|
---|---|---|---|
2 | #define AMIRO_GLOBAL_HPP_
|
||
3 | |||
4 | #include <hal.h> |
||
5 | #include <qei.h> |
||
6 | |||
7 | #include <stdlib.h> |
||
8 | #include <array> |
||
9 | |||
10 | #include <board.h> |
||
11 | #include <amiro/proximity/vcnl4020.hpp> |
||
12 | #include <amiro/bus/i2c/HWI2CDriver.hpp> |
||
13 | #include <amiro/bus/i2c/mux/pca9544.hpp> |
||
14 | #include <amiro/bus/i2c/VI2CDriver.hpp> |
||
15 | #include <amiro/magneto/hmc5883l.hpp> |
||
16 | #include <amiro/bus/spi/HWSPIDriver.hpp> |
||
17 | #include <amiro/accel/lis331dlh.hpp> |
||
18 | #include <amiro/gyro/l3g4200d.hpp> |
||
19 | #include <amiro/power/ina219.hpp> |
||
20 | #include <amiro/power/ltc4412.hpp> |
||
21 | #include <amiro/eeprom/at24.hpp> |
||
22 | #include <amiro/FileSystemInputOutput/FSIODiWheelDrive.hpp> |
||
23 | #include <amiro/serial_reset/serial_can_mux.hpp> |
||
24 | #include <amiro/MotorIncrements.h> |
||
25 | #include <amiro/MotorControl.h> |
||
26 | #include <amiro/DistControl.h> |
||
27 | #include <amiro/Odometry.h> |
||
28 | #include <DiWheelDrive.h> |
||
29 | #include <userthread.hpp> |
||
30 | |||
31 | 9c46b728 | galberding | |
32 | 58fe0e0b | Thomas Schöpping | using namespace amiro; |
33 | |||
34 | class Global final |
||
35 | { |
||
36 | public:
|
||
37 | SerialConfig sd1_config{ |
||
38 | /* speed */ 115200, |
||
39 | /* CR1 register */ 0, |
||
40 | /* CR2 register */ 0, |
||
41 | /* CR3 register */ 0 |
||
42 | }; |
||
43 | |||
44 | I2CConfig i2c1_config{ |
||
45 | /* I2C mode */ OPMODE_I2C,
|
||
46 | /* frequency */ 400000, |
||
47 | /* I2C fast mode duty cycle */ FAST_DUTY_CYCLE_2
|
||
48 | }; |
||
49 | I2CConfig i2c2_config{ |
||
50 | /* I2C mode */ OPMODE_I2C,
|
||
51 | /* frequency */ 400000, |
||
52 | /* I2C fast mode duty cycle */ FAST_DUTY_CYCLE_2
|
||
53 | }; |
||
54 | |||
55 | VCNL4020::VCNL4020Config vcnl4020_config{ |
||
56 | /* command */ VCNL4020::ALS_EN | VCNL4020::PROX_EN | VCNL4020::SELFTIMED_EN,
|
||
57 | /* ambient parameter */ VCNL4020::AMBIENT_RATE_2 | VCNL4020::AMBIENT_AUTO_OFFSET | VCNL4020::AMBIENT_AVG_32,
|
||
58 | /* IR LED current [mA] */ 200u, |
||
59 | b4885314 | Thomas Schöpping | /* proximity rate */ VCNL4020::PROX_RATE_125
|
60 | 58fe0e0b | Thomas Schöpping | }; |
61 | |||
62 | /**
|
||
63 | * @brief I2C Bus 1
|
||
64 | * Conected devices:
|
||
65 | * HMC5883L
|
||
66 | */
|
||
67 | HWI2CDriver HW_I2C1; |
||
68 | /**
|
||
69 | * @brief I2C Bus 2
|
||
70 | * Connected devices:
|
||
71 | * PCA9544
|
||
72 | * VCNL4020
|
||
73 | * INA219
|
||
74 | * AT24Cxx
|
||
75 | */
|
||
76 | HWI2CDriver HW_I2C2; |
||
77 | |||
78 | PCA9544<true> HW_PCA9544;
|
||
79 | |||
80 | std::array<VI2CDriver, 4> V_I2C2;
|
||
81 | |||
82 | std::array<VCNL4020, 4> vcnl4020;
|
||
83 | |||
84 | HMC5883L::HMC5883LConfig hmc5883l_config{ |
||
85 | /* ctrlA */ HMC5883L::DO_20_HZ | HMC5883L::MS_NORMAL | HMC5883L::MA_AVG8,
|
||
86 | /* ctrlB */ HMC5883L::GN_5_GA,
|
||
87 | /* mode */ HMC5883L::MD_CONTCV | HMC5883L::HS_DISABLE
|
||
88 | }; |
||
89 | HMC5883L hmc5883l; |
||
90 | |||
91 | SPIConfig accel_spi1_config{ |
||
92 | /* callback function pointer */ NULL, |
||
93 | /* chip select line port */ GPIOC,
|
||
94 | /* chip select line pad number */ GPIOC_ACCEL_SS_N,
|
||
95 | /* initialzation data */ SPI_CR1_BR_0
|
||
96 | }; |
||
97 | |||
98 | SPIConfig gyro_spi1_config{ |
||
99 | /* callback function pointer */ NULL, |
||
100 | /* chip select line port */ GPIOC,
|
||
101 | /* chip select line pad number */ GPIOC_GYRO_SS_N,
|
||
102 | /* initialzation data */ SPI_CR1_BR_0
|
||
103 | }; |
||
104 | |||
105 | HWSPIDriver HW_SPI1_ACCEL; |
||
106 | HWSPIDriver HW_SPI1_GYRO; |
||
107 | |||
108 | LIS331DLH::LIS331DLHConfig accel_run_config{ |
||
109 | /* ctrl1 */ LIS331DLH::PM_ODR | LIS331DLH::DR_50HZ_37LP | LIS331DLH::ZEN | LIS331DLH::YEN | LIS331DLH::XEN,
|
||
110 | /* ctrl2 */ 0x00u, |
||
111 | /* ctrl3 */ LIS331DLH::INT_LOW | LIS331DLH::I1_CFG_DRY,
|
||
112 | /* ctrl4 */ LIS331DLH::BDU_CONT | LIS331DLH::BLE_LE | LIS331DLH::FS_4G | LIS331DLH::SIM_4WI,
|
||
113 | /* ctrl5 */ LIS331DLH::SLEEP_TO_WAKE_OFF,
|
||
114 | /* interrupt 1 configuration */ {/* configuration */ LIS331DLH::AOI_OR_INT, |
||
115 | /* ths */ 0x00u, |
||
116 | /* duration */ 0x00u}, |
||
117 | /* interrupt 2 configuration */ {/* configuration */ LIS331DLH::AOI_OR_INT, |
||
118 | /* ths */ 0x00u, |
||
119 | /* duration */ 0x00u} |
||
120 | }; |
||
121 | LIS331DLH::LIS331DLHConfig accel_sleep_config{ |
||
122 | /* ctrl1 */ LIS331DLH::PM_0_5_HZ | LIS331DLH::DR_50HZ_37LP | LIS331DLH::XEN,
|
||
123 | /* ctrl2 */ 0x00u, |
||
124 | /* ctrl3 */ LIS331DLH::INT_LOW | LIS331DLH::I1_CFG_I1,
|
||
125 | /* ctrl4 */ LIS331DLH::BDU_CONT | LIS331DLH::BLE_LE | LIS331DLH::FS_4G | LIS331DLH::SIM_4WI,
|
||
126 | /* ctrl5 */ LIS331DLH::SLEEP_TO_WAKE_ON,
|
||
127 | /* interrupt 1 configuration */ {/* configuration */ LIS331DLH::AOI_OR_INT | LIS331DLH::XHIE, |
||
128 | /* ths */ 0x10u, |
||
129 | /* duration */ 0x02u}, |
||
130 | /* interrupt 2 configuration */ {/* configuration */ LIS331DLH::AOI_OR_INT, |
||
131 | /* ths */ 0x00u, |
||
132 | /* duration */ 0x00u} |
||
133 | }; |
||
134 | LIS331DLH lis331dlh; |
||
135 | |||
136 | L3G4200D::L3G4200DConfig gyro_run_config{ |
||
137 | /* ctrl reg 1 */ L3G4200D::DR_100_HZ | L3G4200D::BW_25 | L3G4200D::PD | L3G4200D::ZEN | L3G4200D::YEN | L3G4200D::XEN,
|
||
138 | /* ctrl reg 2 */ 0, |
||
139 | /* ctrl reg 3 */ L3G4200D::I2_DRDY,
|
||
140 | /* ctrl reg 4 */ L3G4200D::FS_500_DPS | L3G4200D::SIM_4W,
|
||
141 | /* ctrl reg 5 */ 0 |
||
142 | }; |
||
143 | L3G4200D::L3G4200DConfig gyro_sleep_config{ |
||
144 | /* ctrl reg 1 */ L3G4200D::DR_100_HZ | L3G4200D::BW_25 | L3G4200D::PD,
|
||
145 | /* ctrl reg 2 */ gyro_run_config.ctrl2,
|
||
146 | /* ctrl reg 3 */ gyro_run_config.ctrl3,
|
||
147 | /* ctrl reg 4 */ gyro_run_config.ctrl4,
|
||
148 | /* ctrl reg 5 */ gyro_run_config.ctrl5
|
||
149 | }; |
||
150 | L3G4200D l3g4200d; |
||
151 | |||
152 | INA219::Driver ina219; |
||
153 | |||
154 | LTC4412wEN<(uintptr_t)GPIOC, GPIOC_PATH_DCSTAT, (uintptr_t)GPIOC, GPIOC_PATH_DCEN> ltc4412; |
||
155 | |||
156 | AT24 at24c01; |
||
157 | fileSystemIo::FSIODiWheelDrive memory; |
||
158 | |||
159 | MotorIncrements increments; |
||
160 | MotorControl motorcontrol; |
||
161 | DistControl distcontrol; |
||
162 | Odometry odometry; |
||
163 | SerialCanMux sercanmux1; |
||
164 | |||
165 | DiWheelDrive robot; |
||
166 | |||
167 | 9c46b728 | galberding | |
168 | |||
169 | 58fe0e0b | Thomas Schöpping | UserThread userThread; |
170 | 2330e415 | Georg Alberding | int rpmForward[2] = {20,20}; |
171 | int rpmSoftLeft[2] = {10,20}; |
||
172 | int rpmHardLeft[2] = {5,20}; |
||
173 | c76baf23 | Georg Alberding | int rpmSoftRight[2] = {rpmSoftLeft[1],rpmSoftLeft[0]}; |
174 | int rpmHardRight[2] = {rpmHardLeft[1],rpmHardLeft[0]}; |
||
175 | 22b85da1 | galberding | |
176 | 58fe0e0b | Thomas Schöpping | |
177 | 12463563 | galberding | // Line Following thresholds set due to calibration
|
178 | 88afb834 | galberding | // MaxDelta: 18676, FL: 4289, FR: 22965
|
179 | 22b85da1 | galberding | // Thresh FL: 5241, FR: 25528
|
180 | int threshProxyL = 5241; |
||
181 | int threshProxyR = 25528; |
||
182 | 1b3adcdd | galberding | int threshWhite = 51056; |
183 | 22b85da1 | galberding | |
184 | // PID for line following:
|
||
185 | float K_p = 0.0f; |
||
186 | float K_i = 0.0f; |
||
187 | float K_d = 0.0f; |
||
188 | |||
189 | // Integral part
|
||
190 | int accumHist = 0; |
||
191 | int oldError = 0; |
||
192 | |||
193 | bfffb0bd | galberding | // Struct for saving motor gains
|
194 | |||
195 | 9c46b728 | galberding | int resetProtect = 1; |
196 | motorGains motorConfigGains; |
||
197 | motorGains stopGains; |
||
198 | |||
199 | types::position startPos; |
||
200 | types::position endPos; |
||
201 | bfffb0bd | galberding | |
202 | 9c46b728 | galberding | // Line Following strategy
|
203 | // 0 EDGE_RIGHT
|
||
204 | // 1 EDGE_LEFT
|
||
205 | // 2 FUZZY
|
||
206 | // 3 DOCKING
|
||
207 | int lfStrategy = 3; |
||
208 | bfffb0bd | galberding | |
209 | d607fcef | galberding | // CAN communication line:
|
210 | // Will be set to true when message was received
|
||
211 | bool msgReceived = false; |
||
212 | 22b85da1 | galberding | // Debugging stuff --------------
|
213 | int forwardSpeed = 10; |
||
214 | bfffb0bd | galberding | int enableRecord = 0; |
215 | |||
216 | d607fcef | galberding | bool triggerCan = false; |
217 | uint8_t strategyTest = 0;
|
||
218 | |||
219 | bfffb0bd | galberding | |
220 | 88afb834 | galberding | |
221 | // Buffer for sensor values
|
||
222 | struct sensorRecord
|
||
223 | { |
||
224 | int FL = 0; |
||
225 | int FR = 0; |
||
226 | int delta = 0; |
||
227 | int error = 0; |
||
228 | }; |
||
229 | |||
230 | int sensSamples = 0; |
||
231 | sensorRecord senseRec[1000];
|
||
232 | 22b85da1 | galberding | sensorRecord maxDist; |
233 | 88afb834 | galberding | |
234 | 22b85da1 | galberding | // -----------------------------
|
235 | 58fe0e0b | Thomas Schöpping | public:
|
236 | Global() : |
||
237 | HW_I2C1(&I2CD1), HW_I2C2(&I2CD2), |
||
238 | HW_PCA9544(&HW_I2C2, 0x07u),
|
||
239 | V_I2C2{{VI2CDriver(&HW_PCA9544, 0),
|
||
240 | VI2CDriver(&HW_PCA9544, 1),
|
||
241 | VI2CDriver(&HW_PCA9544, 2),
|
||
242 | VI2CDriver(&HW_PCA9544, 3)}
|
||
243 | }, |
||
244 | vcnl4020{{/* front left */ VCNL4020(&V_I2C2[0], &vcnl4020_config), |
||
245 | /* left wheel */ VCNL4020(&V_I2C2[1], &vcnl4020_config), |
||
246 | /* right wheel */ VCNL4020(&V_I2C2[2], &vcnl4020_config), |
||
247 | /* front right */ VCNL4020(&V_I2C2[3], &vcnl4020_config)} |
||
248 | }, |
||
249 | hmc5883l(&HW_I2C1, &hmc5883l_config), |
||
250 | HW_SPI1_ACCEL(&SPID1, &accel_spi1_config), HW_SPI1_GYRO(&SPID1, &gyro_spi1_config), |
||
251 | lis331dlh(&HW_SPI1_ACCEL), |
||
252 | l3g4200d(&HW_SPI1_GYRO), |
||
253 | ina219(HW_I2C2, 0x40u),
|
||
254 | ltc4412(), |
||
255 | at24c01(0x400u / 0x08u, 0x08u, 500u, &HW_I2C2), |
||
256 | memory(at24c01, /*BMSV*/ 1, /*bmsv*/ 3, /*HMV*/ 1, /*hmv*/ 0), // bmsv changed von 2 auf 3 |
||
257 | increments(&QEID3, &QEID4), |
||
258 | motorcontrol(&PWMD2, &increments, GPIOB, GPIOB_POWER_EN, &memory), |
||
259 | distcontrol(&motorcontrol, &increments), |
||
260 | b4885314 | Thomas Schöpping | odometry(&increments, &l3g4200d), |
261 | 58fe0e0b | Thomas Schöpping | sercanmux1(&SD1, &CAND1, CAN::DI_WHEEL_DRIVE_ID), |
262 | robot(&CAND1), |
||
263 | userThread() |
||
264 | { |
||
265 | return;
|
||
266 | } |
||
267 | |||
268 | ~Global() |
||
269 | { |
||
270 | return;
|
||
271 | } |
||
272 | }; |
||
273 | |||
274 | #endif /* AMIRO_GLOBAL_HPP_ */ |