Skip to content

Commit 22c7d90

Browse files
committed
Version 1
1 parent 736558c commit 22c7d90

File tree

7 files changed

+159
-21
lines changed

7 files changed

+159
-21
lines changed

CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,14 @@ project(rpi_europe_ctrl C CXX ASM)
1212

1313
# Initialise the Raspberry Pi Pico SDK
1414
set(PICO_BOARD pico)
15+
1516
pico_sdk_init()
1617

1718
# Add executable. Default name is the project name, version 0.1
1819

1920
add_executable(rpi_europe_ctrl src/main.c)
21+
target_compile_options(rpi_europe_ctrl PRIVATE -g3 -O0)
22+
target_link_options(rpi_europe_ctrl PRIVATE -g3 -O0)
2023

2124
#pico_set_program_name(rp2040-data-logger "rp2040-data-logger")
2225
#pico_set_program_version(rp2040-data-logger "0.1")

CMakePresets.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"version": 3,
77
"configurePresets": [
88
{
9-
"name": "Pico-Debug",
9+
"name": "Debug",
1010
"generator": "Ninja",
1111
"cacheVariables": {
1212
"CMAKE_OBJECT_PATH_MAX": "512",

docs/max_doc.png

722 KB
Loading

docs/pins.txt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
GP16 => PWM2
2+
GP17 => PWM1
3+
4+
GP10 => S2A
5+
GP11 => S2B
6+
GP12 => S2C
7+
GP13 => S2D
8+
9+
GP6 S1-B
10+
GP7 S1-A
11+
GP8 S1+A
12+
GP9 S1+B
13+
14+
GP18 => LED1
15+
GP19 => LED2
16+
GP20 => LED3
17+
GP21 => LED4

rpi_europe_ctrl.run.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<component name="ProjectRunConfigurationManager">
2-
<configuration default="false" name="rpi_europe_ctrl" type="com.jetbrains.cidr.embedded.customgdbserver.type" factoryName="com.jetbrains.cidr.embedded.customgdbserver.factory" PROGRAM_PARAMS="-s scripts -f interface\cmsis-dap.cfg -f target\rp2040.cfg -c &quot;adapter speed 5000&quot;" REDIRECT_INPUT="false" ELEVATE="false" USE_EXTERNAL_CONSOLE="false" WORKING_DIR="file://C:/Program Files/Raspberry Pi/Pico SDK v1.5.0/openocd/" PASS_PARENT_ENVS_2="true" PROJECT_NAME="rpi_europe_ctrl" TARGET_NAME="rpi_europe_ctrl" CONFIG_NAME="Pico-Debug" version="1" RUN_TARGET_PROJECT_NAME="rpi_europe_ctrl" RUN_TARGET_NAME="rpi_europe_ctrl">
3-
<custom-gdb-server version="1" gdb-connect="tcp::3333" executable="C:\Program Files\Raspberry Pi\Pico SDK v1.5.0\openocd\openocd.exe" warmup-ms="0" download-type="ALWAYS" reset-cmd="monitor reset halt" reset-type="AFTER_DOWNLOAD">
2+
<configuration default="false" name="rpi_europe_ctrl" type="com.jetbrains.cidr.embedded.customgdbserver.type" factoryName="com.jetbrains.cidr.embedded.customgdbserver.factory" PROGRAM_PARAMS="-s scripts -f interface\cmsis-dap.cfg -f target\rp2040.cfg -c &quot;adapter speed 5000&quot;" REDIRECT_INPUT="false" ELEVATE="false" USE_EXTERNAL_CONSOLE="false" WORKING_DIR="file://C:/Program Files/Raspberry Pi/Pico SDK v1.5.0/openocd/" PASS_PARENT_ENVS_2="true" PROJECT_NAME="rpi_europe_ctrl" TARGET_NAME="rpi_europe_ctrl" CONFIG_NAME="Debug" version="1" RUN_TARGET_PROJECT_NAME="rpi_europe_ctrl" RUN_TARGET_NAME="rpi_europe_ctrl">
3+
<custom-gdb-server version="1" gdb-connect="tcp::3333" executable="C:\Program Files\Raspberry Pi\Pico SDK v1.5.0\openocd\openocd.exe" warmup-ms="0" download-type="ALWAYS" reset-cmd="monitor reset init" reset-type="AFTER_DOWNLOAD">
44
<debugger kind="GDB" isBundled="true" />
55
</custom-gdb-server>
66
<method v="2">

src/main.c

Lines changed: 108 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,35 +8,125 @@
88

99
#include "pico/stdlib.h"
1010
#include "hardware/pwm.h"
11+
#include "pindefs.h"
1112

12-
#define PWMA_GPIO 16
13-
#define PWMB_GPIO 17
13+
static void setupBtn(uint pin) {
14+
gpio_init(pin);
15+
gpio_set_dir(pin, GPIO_IN);
16+
gpio_pull_up(pin);
17+
}
18+
19+
static void setupLed(uint pin) {
20+
gpio_init(pin);
21+
gpio_set_drive_strength(pin, GPIO_DRIVE_STRENGTH_4MA);
22+
gpio_set_dir(pin, GPIO_OUT);
23+
}
1424

1525
int main() {
26+
setupLed(LED_MODE_1_GPIO);
27+
setupLed(LED_MODE_2_GPIO);
28+
setupLed(LED_MODE_3_GPIO);
29+
setupLed(LED_MODE_4_GPIO);
1630

1731
// Tell GPIO 0 and 1 they are allocated to the PWM
18-
gpio_set_function(PWMA_GPIO, GPIO_FUNC_PWM);
19-
gpio_set_function(PWMB_GPIO, GPIO_FUNC_PWM);
32+
gpio_set_function(PWM_MODE_GPIO, GPIO_FUNC_PWM);
33+
gpio_set_function(PWM_STEER_GPIO, GPIO_FUNC_PWM);
34+
35+
setupBtn(BTN_MODE1_GPIO);
36+
setupBtn(BTN_MODE2_GPIO);
37+
setupBtn(BTN_MODE3_GPIO);
38+
setupBtn(BTN_MODE4_GPIO);
39+
40+
setupBtn(BTN_L_GPIO);
41+
setupBtn(BTN_L_FINE_GPIO);
42+
setupBtn(BTN_R_GPIO);
43+
setupBtn(BTN_R_FINE_GPIO);
44+
2045

2146
// Find out which PWM slice is connected to GPIO 0 (it's slice 0)
22-
uint slice_num = pwm_gpio_to_slice_num(PWMA_GPIO);
47+
uint slice_num = pwm_gpio_to_slice_num(PWM_MODE_GPIO);
2348

24-
// Set period of 4 cycles (0 to 3 inclusive)
49+
// Set PWM freq 100 Hz
2550
pwm_set_clkdiv(slice_num, 125);
2651
pwm_set_wrap(slice_num, 10000);
27-
// Set channel A output high for one cycle before dropping
28-
pwm_set_chan_level(slice_num, PWM_CHAN_A, 1500);
29-
// Set initial B output high for three cycles before dropping
30-
pwm_set_chan_level(slice_num, PWM_CHAN_B, 2000);
52+
3153
// Set the PWM running
3254
pwm_set_enabled(slice_num, true);
33-
34-
// Note we could also use pwm_set_gpio_level(gpio, x) which looks up the
35-
// correct slice and channel for a given GPIO.
36-
while(1) {
37-
sleep_ms(700);
38-
pwm_set_chan_level(slice_num, PWM_CHAN_A, 1200);
39-
sleep_ms(700);
40-
pwm_set_chan_level(slice_num, PWM_CHAN_A, 1500);
55+
uint mode = 0;
56+
uint steerPwm = 0;
57+
pwm_set_chan_level(slice_num, PWM_MODE_CHAN, 0);
58+
while (1) {
59+
uint newMode = 0;
60+
if (gpio_get(BTN_MODE4_GPIO) == 0) {
61+
newMode = 4;
62+
}
63+
if (gpio_get(BTN_MODE3_GPIO) == 0) {
64+
newMode = 3;
65+
} else if (gpio_get(BTN_MODE2_GPIO) == 0) {
66+
newMode = 2;
67+
} else if (gpio_get(BTN_MODE1_GPIO) == 0) {
68+
newMode = 1;
69+
}
70+
if (newMode == 0 && mode == 0) {
71+
newMode = 1;
72+
}
73+
if (newMode != 0 && newMode != mode) {
74+
mode = newMode;
75+
uint pwm = 0;
76+
switch (mode) {
77+
case 1:
78+
gpio_put(LED_MODE_1_GPIO, true);
79+
gpio_put(LED_MODE_2_GPIO, false);
80+
gpio_put(LED_MODE_3_GPIO, false);
81+
gpio_put(LED_MODE_4_GPIO, false);
82+
pwm_set_chan_level(slice_num, PWM_MODE_CHAN, 0);
83+
break;
84+
case 2:
85+
gpio_put(LED_MODE_1_GPIO, true);
86+
gpio_put(LED_MODE_2_GPIO, true);
87+
gpio_put(LED_MODE_3_GPIO, false);
88+
gpio_put(LED_MODE_4_GPIO, false);
89+
pwm_set_chan_level(slice_num, PWM_MODE_CHAN, 1300);
90+
break;
91+
case 3:
92+
gpio_put(LED_MODE_1_GPIO, true);
93+
gpio_put(LED_MODE_2_GPIO, true);
94+
gpio_put(LED_MODE_3_GPIO, true);
95+
gpio_put(LED_MODE_4_GPIO, false);
96+
pwm_set_chan_level(slice_num, PWM_MODE_CHAN, 1500);
97+
break;
98+
case 4:
99+
gpio_put(LED_MODE_1_GPIO, true);
100+
gpio_put(LED_MODE_2_GPIO, true);
101+
gpio_put(LED_MODE_3_GPIO, true);
102+
gpio_put(LED_MODE_4_GPIO, true);
103+
pwm_set_chan_level(slice_num, PWM_MODE_CHAN, 1700);
104+
break;
105+
default:
106+
gpio_put(LED_MODE_1_GPIO, false);
107+
gpio_put(LED_MODE_2_GPIO, false);
108+
gpio_put(LED_MODE_3_GPIO, false);
109+
gpio_put(LED_MODE_4_GPIO, false);
110+
pwm_set_chan_level(slice_num, PWM_MODE_CHAN, 0);
111+
break;
112+
}
113+
}
114+
uint newSteer = 0;
115+
if (gpio_get(BTN_L_GPIO) == 0) {
116+
newSteer = 1300;
117+
} else if (gpio_get(BTN_L_FINE_GPIO) == 0) {
118+
newSteer = 1450;
119+
} else if (gpio_get(BTN_R_FINE_GPIO) == 0) {
120+
newSteer = 1550;
121+
} else if (gpio_get(BTN_R_GPIO) == 0) {
122+
newSteer = 1700;
123+
} else {
124+
newSteer = 1500;
125+
}
126+
if (newSteer != steerPwm) {
127+
steerPwm = newSteer;
128+
pwm_set_chan_level(slice_num, PWM_STEER_CHAN, steerPwm);
129+
}
41130
}
131+
42132
}

src/pindefs.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//
2+
// Created by elmot on 8 Mar 2023.
3+
//
4+
5+
#ifndef RPI_EUROPE_CTRL_PINDEFS_H
6+
#define RPI_EUROPE_CTRL_PINDEFS_H
7+
8+
#define PWM_MODE_GPIO 16
9+
#define PWM_STEER_GPIO 17
10+
11+
#define BTN_MODE1_GPIO 10
12+
#define BTN_MODE2_GPIO 11
13+
#define BTN_MODE3_GPIO 12
14+
#define BTN_MODE4_GPIO 13
15+
16+
#define BTN_L_GPIO 6
17+
#define BTN_L_FINE_GPIO 7
18+
#define BTN_R_FINE_GPIO 8
19+
#define BTN_R_GPIO 9
20+
21+
#define LED_MODE_1_GPIO 18
22+
#define LED_MODE_2_GPIO 19
23+
#define LED_MODE_3_GPIO 20
24+
#define LED_MODE_4_GPIO 21
25+
26+
#define PWM_MODE_CHAN PWM_CHAN_A
27+
#define PWM_STEER_CHAN PWM_CHAN_B
28+
#endif //RPI_EUROPE_CTRL_PINDEFS_H

0 commit comments

Comments
 (0)