Rev 2 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2 | mjames | 1 | /* |
2 | ChibiOS/RT - Copyright (C) 2006,2007,2008,2009,2010, |
||
3 | 2011,2012 Giovanni Di Sirio. |
||
4 | |||
5 | This file is part of ChibiOS/RT. |
||
6 | |||
7 | ChibiOS/RT is free software; you can redistribute it and/or modify |
||
8 | it under the terms of the GNU General Public License as published by |
||
9 | the Free Software Foundation; either version 3 of the License, or |
||
10 | (at your option) any later version. |
||
11 | |||
12 | ChibiOS/RT is distributed in the hope that it will be useful, |
||
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
15 | GNU General Public License for more details. |
||
16 | |||
17 | You should have received a copy of the GNU General Public License |
||
18 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
||
19 | |||
20 | --- |
||
21 | |||
22 | A special exception to the GPL can be applied should you wish to distribute |
||
23 | a combined work that includes ChibiOS/RT, without being obliged to provide |
||
24 | the source code for any proprietary components. See the file exception.txt |
||
25 | for full details of how and when the exception can be applied. |
||
26 | */ |
||
27 | |||
28 | #include <stdio.h> |
||
29 | #include <string.h> |
||
30 | |||
31 | #include "ch.h" |
||
32 | #include "hal.h" |
||
33 | //#include "test.h" |
||
34 | #include "shell.h" |
||
35 | #include "evtimer.h" |
||
36 | #include "chprintf.h" |
||
37 | |||
38 | #if (HAL_USE_SERIAL_USB == TRUE) |
||
39 | #include "usbcfg.h" |
||
40 | #endif |
||
41 | |||
42 | #include "useAdc.h" |
||
43 | |||
44 | #include "shellCmds.h" |
||
45 | |||
6 | mjames | 46 | #include "ap_math.h" |
47 | #include "spiInterface.h" |
||
48 | #include "SSD1306.h" |
||
49 | #include "font.h" |
||
2 | mjames | 50 | |
6 | mjames | 51 | #include "vl53l0x_api.h" |
52 | #include "vl53l0x_platform.h" |
||
53 | #include "required_version.h" |
||
2 | mjames | 54 | |
6 | mjames | 55 | |
56 | static MUTEX_DECL( mutexDisplay); |
||
57 | |||
58 | unsigned lidar = 0; |
||
59 | |||
2 | mjames | 60 | /*===========================================================================*/ |
61 | /* Command line related. */ |
||
62 | /*===========================================================================*/ |
||
63 | |||
6 | mjames | 64 | #define SHELL_WA_SIZE THD_WORKING_AREA_SIZE(1024) |
2 | mjames | 65 | #define TEST_WA_SIZE THD_WORKING_AREA_SIZE(256) |
66 | |||
67 | static const ShellConfig shell_cfg1 = { |
||
68 | #if (HAL_USE_SERIAL_USB == TRUE) |
||
6 | mjames | 69 | (BaseSequentialStream*) &SDU1, |
2 | mjames | 70 | #else |
71 | (BaseSequentialStream *)&SD1, |
||
72 | #endif |
||
6 | mjames | 73 | shellCommands }; |
2 | mjames | 74 | //////// |
75 | // end of shell stuff |
||
76 | |||
77 | uint16_t sampIndex; |
||
78 | |||
6 | mjames | 79 | void setDriveA(uint8_t bit) { |
80 | if (bit) { |
||
81 | palSetPad(GPIOA, GPIOA_A1); |
||
82 | palClearPad(GPIOA, GPIOA_A2); |
||
83 | } else { |
||
2 | mjames | 84 | palClearPad(GPIOA, GPIOA_A1); |
85 | palSetPad(GPIOA, GPIOA_A2); |
||
86 | } |
||
87 | } |
||
88 | |||
6 | mjames | 89 | void setDriveB(uint8_t bit) { |
90 | if (bit) { |
||
91 | palSetPad(GPIOA, GPIOA_B1); |
||
92 | palClearPad(GPIOA, GPIOA_B2); |
||
93 | } else { |
||
2 | mjames | 94 | palClearPad(GPIOA, GPIOA_B1); |
95 | palSetPad(GPIOA, GPIOA_B2); |
||
96 | } |
||
97 | } |
||
98 | |||
99 | |||
6 | mjames | 100 | // dial settings |
101 | volatile int origin = 0; |
||
102 | ; |
||
103 | volatile int target = 0; |
||
2 | mjames | 104 | volatile int count = 0; |
105 | |||
6 | mjames | 106 | volatile bool pause; |
107 | // |
||
108 | |||
109 | |||
110 | void setPause(bool p) { |
||
111 | pause = p; |
||
112 | } |
||
113 | |||
2 | mjames | 114 | static THD_WORKING_AREA(waThread1, 512); |
6 | mjames | 115 | static void Thread1(void *arg) { |
2 | mjames | 116 | |
117 | (void) arg; |
||
6 | mjames | 118 | unsigned const fast = 10; |
119 | unsigned const slow = 30; |
||
120 | unsigned const range = slow - fast; |
||
121 | unsigned del = fast; |
||
122 | int step = 1; |
||
123 | chRegSetThreadName("Step"); |
||
2 | mjames | 124 | while (TRUE) { |
125 | |||
6 | mjames | 126 | while (pause) |
127 | chThdSleep(1000); |
||
128 | |||
129 | switch (count % 4) { |
||
130 | case 0: |
||
131 | setDriveA(1); |
||
132 | setDriveB(0); |
||
133 | break; |
||
134 | case 1: |
||
135 | setDriveA(1); |
||
136 | setDriveB(1); |
||
137 | break; |
||
138 | case 2: |
||
139 | setDriveA(0); |
||
140 | setDriveB(1); |
||
141 | break; |
||
142 | case 3: |
||
143 | setDriveA(0); |
||
144 | setDriveB(0); |
||
145 | break; |
||
2 | mjames | 146 | } |
147 | |||
148 | // all this calculates minimum distance from |
||
149 | // target or origin |
||
6 | mjames | 150 | int d1 = count - origin; |
151 | if (d1 < 0) |
||
152 | d1 = -d1; |
||
153 | int d2 = count - target; |
||
154 | if (d2 < 0) |
||
155 | d2 = -d2; |
||
156 | // finally, minimum distance |
||
157 | int dist = d1 < d2 ? d1 : d2; |
||
2 | mjames | 158 | |
6 | mjames | 159 | del = fast; |
160 | if (dist < range) // inside lower bound of distance |
||
161 | { |
||
162 | del = slow - dist; |
||
163 | } |
||
164 | chThdSleep(del); |
||
2 | mjames | 165 | |
6 | mjames | 166 | if (count < target) { |
167 | step = 1; |
||
168 | } |
||
169 | if (count > target) { |
||
170 | step = -1; |
||
171 | } |
||
172 | if (count == target) { |
||
173 | step = 0; |
||
174 | } |
||
175 | count = count + step; |
||
2 | mjames | 176 | |
6 | mjames | 177 | } |
2 | mjames | 178 | |
179 | } |
||
180 | |||
181 | /* |
||
182 | * Command Shell Thread |
||
183 | */ |
||
184 | static THD_WORKING_AREA(waThread2, 512); |
||
6 | mjames | 185 | static void Thread2(void *arg) { |
2 | mjames | 186 | thread_t *shelltp = NULL; |
187 | |||
188 | chRegSetThreadName("Shell "); |
||
6 | mjames | 189 | /* |
190 | * in this demo it just performs |
||
191 | * a shell respawn upon its termination. |
||
192 | */ |
||
193 | while (true) { |
||
194 | if (!shelltp) { |
||
195 | #if (HAL_USE_SERIAL_USB == TRUE) |
||
196 | if (SDU1.config->usbp->state == USB_ACTIVE) { |
||
197 | /* Spawns a new shell.*/ |
||
198 | shelltp = chThdCreateFromHeap(NULL, SHELL_WA_SIZE, "shell", |
||
199 | NORMALPRIO, shellThread, (void*) &shell_cfg1); |
||
200 | } |
||
201 | #else |
||
2 | mjames | 202 | shelltp = chThdCreateFromHeap(NULL, SHELL_WA_SIZE, "shell", NORMALPRIO, shellThread, (void *) &shell_cfg1); |
203 | #endif |
||
6 | mjames | 204 | } else { |
205 | /* If the previous shell exited.*/ |
||
206 | if (chThdTerminatedX(shelltp)) { |
||
207 | /* Recovers memory of the previous shell.*/ |
||
208 | chThdRelease(shelltp); |
||
209 | shelltp = NULL; |
||
210 | } |
||
211 | } |
||
212 | chThdSleepMilliseconds(500); |
||
213 | } |
||
2 | mjames | 214 | |
215 | } |
||
216 | |||
6 | mjames | 217 | static THD_WORKING_AREA(waThread3, 1024); |
218 | static void Thread3(void *arg) { |
||
2 | mjames | 219 | |
6 | mjames | 220 | VL53L0X_Dev_t MyDevice; |
221 | VL53L0X_Dev_t *pMyDevice = &MyDevice; |
||
222 | setPause(true); |
||
223 | int Status = VL53L0XdeviceSetup(pMyDevice); |
||
224 | setPause(false); |
||
225 | VL53L0X_RangingMeasurementData_t RangingMeasurementData; |
||
2 | mjames | 226 | |
6 | mjames | 227 | while(1){ |
2 | mjames | 228 | |
6 | mjames | 229 | chThdSleep(chTimeMS2I(100)); |
230 | setPause(true); |
||
231 | Status = VL53L0X_PerformSingleRangingMeasurement(pMyDevice, |
||
232 | &RangingMeasurementData); |
||
233 | setPause(false); |
||
234 | |||
235 | if (Status != VL53L0X_ERROR_NONE) continue; |
||
236 | |||
237 | |||
238 | lidar = RangingMeasurementData.RangeMilliMeter; |
||
239 | |||
240 | |||
241 | |||
242 | } |
||
243 | } |
||
2 | mjames | 244 | /* |
245 | * Application entry point. |
||
246 | */ |
||
247 | int main(void) { |
||
248 | // struct EventListener el0, el1; |
||
249 | |||
250 | /* |
||
251 | * System initializations. |
||
252 | * - HAL initialization, this also initializes the configured device drivers |
||
253 | * and performs the board-specific initializations. |
||
254 | * - Kernel initialization, the main() function becomes a thread and the |
||
255 | * RTOS is active. |
||
256 | */ |
||
257 | halInit(); |
||
258 | chSysInit(); |
||
259 | |||
260 | #if (HAL_USE_SERIAL_USB == TRUE) |
||
6 | mjames | 261 | /* |
262 | * Initializes a serial-over-USB CDC driver. |
||
263 | */ |
||
264 | sduObjectInit(&SDU1); |
||
265 | sduStart(&SDU1, &serusbcfg); |
||
2 | mjames | 266 | |
267 | #if HAL_USE_USB_DUAL_CDC == TRUE |
||
6 | mjames | 268 | sduObjectInit(&SDU2); |
269 | sduStart(&SDU2, &serusbcfg2); |
||
2 | mjames | 270 | #endif |
271 | |||
6 | mjames | 272 | /* |
273 | * Activates the USB driver and then the USB bus pull-up on D+. |
||
274 | * Note, a delay is inserted in order to not have to disconnect the cable |
||
275 | * after a reset. |
||
276 | */ |
||
277 | usbDisconnectBus(serusbcfg.usbp); |
||
278 | chThdSleepMilliseconds(1000); |
||
279 | usbStart(serusbcfg.usbp, &usbcfg); |
||
280 | usbConnectBus(serusbcfg.usbp); |
||
2 | mjames | 281 | #else |
282 | /* |
||
283 | * Initializes serial port. |
||
284 | */ |
||
285 | sdStart(&SD2, NULL); |
||
286 | #endif /* HAL_USE_SERIAL_USB */ |
||
287 | |||
6 | mjames | 288 | useAdc(); |
2 | mjames | 289 | |
290 | |||
291 | /* |
||
292 | * Shell manager initialization. |
||
293 | */ |
||
6 | mjames | 294 | // shellInit(); |
295 | /* |
||
296 | * initialise approximate maths |
||
297 | */ |
||
298 | ap_init(); |
||
2 | mjames | 299 | |
6 | mjames | 300 | chMtxLock(&mutexDisplay); |
2 | mjames | 301 | |
6 | mjames | 302 | /* start the SPI hardware for display */ |
303 | ssd1306spiInit(); |
||
304 | |||
305 | ssd1306_begin( SSD1306_SWITCHCAPVCC, 0) ; |
||
306 | |||
307 | |||
308 | clearDisplay(); |
||
309 | |||
310 | display(); |
||
311 | |||
312 | chMtxUnlock(&mutexDisplay); |
||
313 | |||
314 | |||
315 | /* |
||
2 | mjames | 316 | * Creates the PLL thread |
317 | */ |
||
318 | chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL); |
||
319 | |||
6 | mjames | 320 | chThdCreateStatic(waThread2, sizeof(waThread2), NORMALPRIO, Thread2, NULL); |
2 | mjames | 321 | |
6 | mjames | 322 | // chThdCreateStatic(waThread3, sizeof(waThread3), NORMALPRIO, Thread3, NULL); |
2 | mjames | 323 | |
6 | mjames | 324 | // reset gauge |
325 | origin = 540; |
||
326 | count = 540; |
||
327 | target = 0; |
||
328 | chThdSleepMilliseconds(1000); |
||
329 | target = 0; |
||
2 | mjames | 330 | |
6 | mjames | 331 | int frac = 0; |
2 | mjames | 332 | /* start the SPI hardware for display */ |
6 | mjames | 333 | while (1) { |
2 | mjames | 334 | |
6 | mjames | 335 | chThdSleepMilliseconds(100); |
336 | unsigned const SCALE = frac * 32 + 16; |
||
337 | frac = ++frac %10; |
||
2 | mjames | 338 | |
6 | mjames | 339 | // read the dial |
340 | adcSample(); |
||
2 | mjames | 341 | |
6 | mjames | 342 | origin = count; |
2 | mjames | 343 | |
6 | mjames | 344 | target = target +1; |
2 | mjames | 345 | |
6 | mjames | 346 | // target = lidar * 630L / 2000L; |
2 | mjames | 347 | |
6 | mjames | 348 | if (target > 540) target = 0; |
2 | mjames | 349 | |
350 | |||
6 | mjames | 351 | chMtxLock(&mutexDisplay); |
2 | mjames | 352 | |
6 | mjames | 353 | font_gotoxy(0,0); |
354 | clearDisplay(); |
||
2 | mjames | 355 | |
6 | mjames | 356 | // print_scaled_string("cm:",0,16,4,SCALE); |
2 | mjames | 357 | |
6 | mjames | 358 | |
359 | print_digits(0, 0, 4, 1, target,SCALE); |
||
360 | |||
361 | chMtxUnlock(&mutexDisplay); |
||
362 | |||
363 | display(); |
||
364 | |||
365 | |||
366 | |||
367 | } |
||
368 | |||
2 | mjames | 369 | } |