MotorController
coding_wheels.c
1 #include "coding_wheels.h"
2 
3 volatile int32_t left_ticks;
4 volatile int32_t right_ticks;
5 
6 static wheel_orientation_t right_wheel_orientation;
7 static wheel_orientation_t left_wheel_orientation;
8 
9 static void ext_cb(EXTDriver* driver, expchannel_t channel);
10 
11 const EXTConfig ext_config = {
12  {
13  {EXT_CH_MODE_DISABLED, NULL},
14  {EXT_CH_MODE_DISABLED, NULL},
15  {EXT_CH_MODE_DISABLED, NULL},
16  {EXT_CH_MODE_DISABLED, NULL},
17  {EXT_CH_MODE_DISABLED, NULL},
18  {EXT_CH_MODE_DISABLED, NULL},
19  {EXT_CH_MODE_DISABLED, NULL},
20  {EXT_CH_MODE_DISABLED, NULL},
21  {EXT_CH_MODE_DISABLED, NULL},
22  {EXT_CH_MODE_DISABLED, NULL},
23  {EXT_CH_MODE_RISING_EDGE | EXT_CH_MODE_AUTOSTART | EXT_MODE_GPIOB, ext_cb}, // Coding wheel A
24  {EXT_CH_MODE_RISING_EDGE | EXT_CH_MODE_AUTOSTART | EXT_MODE_GPIOB, ext_cb}, // Coding wheel B
25  {EXT_CH_MODE_DISABLED, NULL},
26  {EXT_CH_MODE_DISABLED, NULL},
27  {EXT_CH_MODE_DISABLED, NULL},
28  {EXT_CH_MODE_DISABLED, NULL}
29  }
30 };
31 
32 /*
33  * Update the tick counters.
34  */
35 static void ext_cb(EXTDriver* driver, expchannel_t channel)
36 {
37  (void)driver;
38  if (channel == GPIOB_RCODA) {
39  if (palReadPad(GPIOB, GPIOB_RCODB) == PAL_LOW) {
40  right_wheel_orientation ? right_ticks-- : right_ticks++;
41  } else {
42  right_wheel_orientation ? right_ticks++ : right_ticks--;
43  }
44  } else if (channel == GPIOB_LCODA) {
45  if (palReadPad(GPIOB, GPIOB_LCODB) == PAL_LOW) {
46  left_wheel_orientation ? left_ticks-- : left_ticks++;
47  } else {
48  left_wheel_orientation ? left_ticks++ : left_ticks--;
49  }
50  }
51 }
52 
54 {
55  /* Initialize the tick counters with the given values */
56  left_ticks = config.initial_left_ticks;
57  right_ticks = config.initial_right_ticks;
58 
59  left_wheel_orientation = config.left_wheel_orientation;
60  right_wheel_orientation = config.right_wheel_orientation;
61 
62  /* Start the ChibiOS ext driver */
63  extStart(&EXTD1, &ext_config);
64 }
Configuration structure for coding wheels.
Definition: coding_wheels.h:22
wheel_orientation_t right_wheel_orientation
Definition: coding_wheels.h:24
void init_coding_wheels(coding_wheels_config_t config)
Perform all the initializations required by the coding wheels.
Definition: coding_wheels.c:53
wheel_orientation_t
Possible values for coding wheels orientation.
Definition: coding_wheels.h:11
wheel_orientation_t left_wheel_orientation
Definition: coding_wheels.h:26