Skip to content

Commit e06eafd

Browse files
authored
Open Bot Brain primitives for motors + color sensor (#285)
* Add open bot brain Zephyr primitives * Move some motor code into a separate file * Refactor motor code a bit more by creating a Motor class that does most of the work * Only use one interrupt for the motor encoders instead of two, this also resolves the conflict with motor c and the other motors! So now you can control all 4 motors at the same time! * Enable CONFIG_STM32_ENABLE_DEBUG_SLEEP_STOP to allow flashing the board at all times without reset (Increases power consumption so should be disabled if power consumption is important, for example when just using open bot brain in a classroom) * Clean up color sensor code a bit * Rename colour_sensor to color_sensor + add abort primitive + set heartbeat interval to 990ms instead of 1000 * Minor cleanup * Move sensor setup into separate function * Move most of the color sensor code into the uart_sensor files * Fix case where invalid data was read from the sensor * Move uart sensor data into a struct By doing so you could theoretically now have multiple uart sensors working at the same time. * Misc type conversions * Invert motor direction to match the forward direction used by Lego themselves * Update motor primitives to all have the port and speed first and other arguments later + add stop_motor Also removed the brake argument on drive_motor, to stop the motor you now use the stop_motor primitive. * Put open bot brain primitives in an ifdef for the board * Use DT_FOREACH_PROP_ELEM_SEP to get all pwm specs * Don't hardcode the number of motors * Run clang-tidy * Place mindstorms file under ifndef ARDUINO * Read debug messages in debug thread again instead of timer interrupt (Seems to work correctly now?) * clang-format * Make print_int print signed integers * Make color_sensor push signed integers * Support data messages consisting of two bytes * Fix color sensors no longer working because they send 2^0 bytes instead of 2^1 * Configure uart controllers 2, 4 and 3 for input sensors * Support using multiple sensors at the same time * Rename color_sensor to read_uart_sensor * clang-format * Use zephyr LISTIFY macro to create a list of devices * Cleanup + clang-format
1 parent 8bd9e33 commit e06eafd

File tree

9 files changed

+845
-147
lines changed

9 files changed

+845
-147
lines changed

platforms/Zephyr/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ target_sources(app PRIVATE
1919
../../src/Interpreter/instructions.cpp
2020
../../src/Interpreter/interpreter.cpp
2121
../../src/Primitives/zephyr.cpp
22+
../../src/Primitives/Mindstorms/Motor.cpp
23+
../../src/Primitives/Mindstorms/uart_sensor.cpp
2224
../../src/Memory/mem.cpp
2325
../../src/Utils/util.cpp
2426
../../src/Utils/util_arduino.cpp
Lines changed: 159 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -1,132 +1,170 @@
11
#include "../app.overlay"
22

33
/ {
4-
zephyr,user {
5-
warduino-gpios =
6-
<&gpioa 0 0>,
7-
<&gpioa 1 0>,
8-
<&gpioa 2 0>,
9-
<&gpioa 3 0>,
10-
<&gpioa 4 0>,
11-
<&gpioa 5 0>,
12-
<&gpioa 6 0>,
13-
<&gpioa 7 0>,
14-
<&gpioa 8 0>,
15-
<&gpioa 9 0>,
16-
<&gpioa 10 0>,
17-
<&gpioa 11 0>,
18-
<&gpioa 12 0>,
19-
<&gpioa 15 0>,
20-
<&gpiob 0 0>,
21-
<&gpiob 1 0>,
22-
<&gpiob 2 0>,
23-
<&gpiob 3 0>,
24-
<&gpiob 4 0>,
25-
<&gpiob 5 0>,
26-
<&gpiob 6 0>,
27-
<&gpiob 7 0>,
28-
<&gpiob 8 0>,
29-
<&gpiob 9 0>,
30-
<&gpiob 12 0>,
31-
<&gpiob 13 0>,
32-
<&gpiob 14 0>,
33-
<&gpiob 15 0>,
34-
<&gpioc 0 0>,
35-
<&gpioc 1 0>,
36-
<&gpioc 2 0>,
37-
<&gpioc 3 0>,
38-
<&gpioc 4 0>,
39-
<&gpioc 5 0>,
40-
<&gpioc 6 0>,
41-
<&gpioc 7 0>,
42-
<&gpioc 8 0>,
43-
<&gpioc 9 0>,
44-
<&gpioc 12 0>,
45-
<&gpioc 15 0>,
46-
<&gpiod 0 0>,
47-
<&gpiod 1 0>,
48-
<&gpiod 2 0>,
49-
<&gpiod 3 0>,
50-
<&gpiod 4 0>,
51-
<&gpiod 7 0>,
52-
<&gpiod 8 0>,
53-
<&gpiod 9 0>,
54-
<&gpiod 10 0>,
55-
<&gpiod 11 0>,
56-
<&gpiod 12 0>,
57-
<&gpiod 13 0>,
58-
<&gpiod 14 0>,
59-
<&gpiod 15 0>,
60-
<&gpioe 0 0>,
61-
<&gpioe 1 0>,
62-
<&gpioe 2 0>,
63-
<&gpioe 3 0>,
64-
<&gpioe 4 0>,
65-
<&gpioe 7 0>,
66-
<&gpioe 8 0>,
67-
<&gpioe 10 0>,
68-
<&gpioe 11 0>,
69-
<&gpioe 12 0>,
70-
<&gpioe 13 0>,
71-
<&gpioe 14 0>,
72-
<&gpioe 15 0>,
73-
<&gpiof 3 0>,
74-
<&gpiof 4 0>,
75-
<&gpiof 5 0>,
76-
<&gpiof 6 0>,
77-
<&gpiof 7 0>,
78-
<&gpiof 8 0>,
79-
<&gpiof 9 0>,
80-
<&gpiof 10 0>,
81-
<&gpiof 11 0>,
82-
<&gpiof 12 0>,
83-
<&gpiof 13 0>,
84-
<&gpiof 14 0>,
85-
<&gpiog 2 0>,
86-
<&gpiog 3 0>,
87-
<&gpiog 4 0>,
88-
<&gpiog 5 0>,
89-
<&gpiog 6 0>,
90-
<&gpiog 7 0>,
91-
<&gpiog 8 0>,
92-
<&gpiog 11 0>,
93-
<&gpiog 12 0>,
94-
<&gpiog 15 0>,
95-
<&gpiob 10 0>,
96-
<&gpiob 11 0>,
97-
<&gpioc 10 0>,
98-
<&gpioc 11 0>,
99-
<&gpiog 13 0>,
100-
<&gpiog 14 0>,
101-
<&gpiod 5 0>,
102-
<&gpiod 6 0>,
103-
<&gpiof 0 0>,
104-
<&gpiof 1 0>,
105-
<&gpiog 9 0>,
106-
<&gpiog 10 0>;
4+
zephyr,user {
5+
warduino-gpios =
6+
<&gpioa 0 0>,
7+
<&gpioa 1 0>,
8+
<&gpioa 2 0>,
9+
<&gpioa 3 0>,
10+
<&gpioa 4 0>,
11+
<&gpioa 5 0>,
12+
<&gpioa 6 0>,
13+
<&gpioa 7 0>,
14+
<&gpioa 8 0>,
15+
<&gpioa 9 0>,
16+
<&gpioa 10 0>,
17+
<&gpioa 11 0>,
18+
<&gpioa 12 0>,
19+
<&gpioa 15 0>,
20+
<&gpiob 0 0>,
21+
<&gpiob 1 0>,
22+
<&gpiob 2 0>,
23+
<&gpiob 3 0>,
24+
<&gpiob 4 0>,
25+
<&gpiob 5 0>,
26+
<&gpiob 6 0>,
27+
<&gpiob 7 0>,
28+
<&gpiob 8 0>,
29+
<&gpiob 9 0>,
30+
<&gpiob 12 0>,
31+
<&gpiob 13 0>,
32+
<&gpiob 14 0>,
33+
<&gpiob 15 0>,
34+
<&gpioc 0 0>,
35+
<&gpioc 1 0>,
36+
<&gpioc 2 0>,
37+
<&gpioc 3 0>,
38+
<&gpioc 4 0>,
39+
<&gpioc 5 0>,
40+
<&gpioc 6 0>,
41+
<&gpioc 7 0>,
42+
<&gpioc 8 0>,
43+
<&gpioc 9 0>,
44+
<&gpioc 12 0>,
45+
<&gpioc 15 0>,
46+
<&gpiod 0 0>,
47+
<&gpiod 1 0>,
48+
<&gpiod 2 0>,
49+
<&gpiod 3 0>,
50+
<&gpiod 4 0>,
51+
<&gpiod 7 0>,
52+
<&gpiod 8 0>,
53+
<&gpiod 9 0>,
54+
<&gpiod 10 0>,
55+
<&gpiod 11 0>,
56+
<&gpiod 12 0>,
57+
<&gpiod 13 0>,
58+
<&gpiod 14 0>,
59+
<&gpiod 15 0>,
60+
<&gpioe 0 0>,
61+
<&gpioe 1 0>,
62+
<&gpioe 2 0>,
63+
<&gpioe 3 0>,
64+
<&gpioe 4 0>,
65+
<&gpioe 7 0>,
66+
<&gpioe 8 0>,
67+
<&gpioe 10 0>,
68+
<&gpioe 11 0>,
69+
<&gpioe 12 0>,
70+
<&gpioe 13 0>,
71+
<&gpioe 14 0>,
72+
<&gpioe 15 0>,
73+
<&gpiof 3 0>,
74+
<&gpiof 4 0>,
75+
<&gpiof 5 0>,
76+
<&gpiof 6 0>,
77+
<&gpiof 7 0>,
78+
<&gpiof 8 0>,
79+
<&gpiof 9 0>,
80+
<&gpiof 10 0>,
81+
<&gpiof 11 0>,
82+
<&gpiof 12 0>,
83+
<&gpiof 13 0>,
84+
<&gpiof 14 0>,
85+
<&gpiog 2 0>,
86+
<&gpiog 3 0>,
87+
<&gpiog 4 0>,
88+
<&gpiog 5 0>,
89+
<&gpiog 6 0>,
90+
<&gpiog 7 0>,
91+
<&gpiog 8 0>,
92+
<&gpiog 11 0>,
93+
<&gpiog 12 0>,
94+
<&gpiog 15 0>,
95+
<&gpiob 10 0>,
96+
<&gpiob 11 0>,
97+
<&gpioc 10 0>,
98+
<&gpioc 11 0>,
99+
<&gpiog 13 0>,
100+
<&gpiog 14 0>,
101+
<&gpiod 5 0>,
102+
<&gpiod 6 0>,
103+
<&gpiof 0 0>,
104+
<&gpiof 1 0>,
105+
<&gpiog 9 0>,
106+
<&gpiog 10 0>;
107107

108-
pwms =
109-
<&pwm8 3 10000 PWM_POLARITY_NORMAL>,
110-
<&pwm8 4 10000 PWM_POLARITY_NORMAL>,
111-
<&pwm8 1 10000 PWM_POLARITY_NORMAL>,
112-
<&pwm8 2 10000 PWM_POLARITY_NORMAL>;
113-
114-
warduino-uarts =
115-
<&usart1>;
116-
};
108+
pwms =
109+
<&pwm8 3 10000 PWM_POLARITY_NORMAL>,
110+
<&pwm8 4 10000 PWM_POLARITY_NORMAL>,
111+
<&pwm8 1 10000 PWM_POLARITY_NORMAL>,
112+
<&pwm8 2 10000 PWM_POLARITY_NORMAL>,
113+
<&pwm1 3 10000 PWM_POLARITY_NORMAL>,
114+
<&pwm1 4 10000 PWM_POLARITY_NORMAL>,
115+
<&pwm1 1 10000 PWM_POLARITY_NORMAL>,
116+
<&pwm1 2 10000 PWM_POLARITY_NORMAL>;
117+
118+
warduino-uarts =
119+
<&usart3>,
120+
<&uart4>,
121+
<&usart2>,
122+
<&usart1>;
123+
};
117124
};
118125

119126
&timers8 {
120-
status = "okay";
121-
pwm8: pwm {
122-
status = "okay";
123-
pinctrl-0 = <&tim8_ch3_pc8 &tim8_ch4_pc9 &tim8_ch1_pc6 &tim8_ch2_pc7>;
124-
pinctrl-names =
125-
"default";
126-
};
127+
status = "okay";
128+
129+
pwm8: pwm {
130+
status = "okay";
131+
pinctrl-0 = <&tim8_ch3_pc8 &tim8_ch4_pc9 &tim8_ch1_pc6 &tim8_ch2_pc7>;
132+
pinctrl-names =
133+
"default";
134+
};
135+
};
136+
137+
&timers1 {
138+
status = "okay";
139+
140+
pwm1: pwm {
141+
status = "okay";
142+
pinctrl-0 = <&tim1_ch3_pe13 &tim1_ch4_pe14 &tim1_ch1_pa8 &tim1_ch2_pa9>;
143+
pinctrl-names =
144+
"default";
145+
};
127146
};
128147

129148
&usart1 {
130-
pinctrl-0 = <&usart1_tx_pg9 &usart1_rx_pg10>;
131-
current-speed = <2400>;
149+
pinctrl-0 = <&usart1_tx_pg9 &usart1_rx_pg10>;
150+
current-speed = <2400>;
151+
};
152+
153+
&usart2 {
154+
pinctrl-0 = <&usart2_tx_pd5 &usart2_rx_pd6>;
155+
current-speed = <2400>;
156+
};
157+
158+
&uart4 {
159+
pinctrl-0 = <&uart4_tx_pc10 &uart4_rx_pc11>;
160+
current-speed = <2400>;
161+
status = "okay";
162+
pinctrl-names = "default";
163+
};
164+
165+
&usart3 {
166+
pinctrl-0 = <&usart3_tx_pb10 &usart3_rx_pb11>;
167+
current-speed = <2400>;
168+
status = "okay";
169+
pinctrl-names = "default";
132170
};

platforms/Zephyr/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ void startDebuggerStd() {
6464
int valread;
6565
uint8_t buffer[1024] = {0};
6666
while (true) {
67-
k_msleep(1000);
67+
k_msleep(500);
6868

6969
while ((valread = war_console_read(NULL, buffer, 1024)) > 0) {
7070
wac->handleInterrupt(valread, buffer);

platforms/Zephyr/prj.conf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,7 @@ CONFIG_CONSOLE_PUTCHAR_BUFSIZE=4096
3030

3131
CONFIG_POSIX_API=y
3232
CONFIG_MAIN_STACK_SIZE=8192
33+
34+
# Allow flashing the board without reset (increases power consumption so should only be used for development)
35+
CONFIG_STM32_ENABLE_DEBUG_SLEEP_STOP=y
36+

0 commit comments

Comments
 (0)