Skip to content

Commit 244c5e6

Browse files
committed
Add Rapbit32 motor work
1 parent 59101d2 commit 244c5e6

File tree

6 files changed

+220
-88
lines changed

6 files changed

+220
-88
lines changed

ports/esp32/boards/Rapbit32/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```bash
2+
make deploy BOARD="Rapbit32" PORT="/dev/ttyS12" BAUD=921600 USER_C_MODULES=/home/max/micropython/ports/esp32/boards/Rapbit32/usermod.cmake
3+
```
Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
#include "esp_log.h"
2+
3+
// Include MicroPython API.
4+
#include "py/runtime.h"
5+
#include "py/mphal.h"
6+
7+
#include "mphalport.h"
8+
9+
#include "driver/mcpwm.h"
10+
#include "soc/mcpwm_reg.h"
11+
#include "soc/mcpwm_struct.h"
12+
13+
#define M1A_PIN 5
14+
#define M1B_PIN 4
15+
#define M2A_PIN 18
16+
#define M2B_PIN 19
17+
18+
#define FORWARD 1
19+
#define BACKWARD 2
20+
#define TURN_LEFT 3
21+
#define TURN_RIGHT 4
22+
23+
static mcpwm_config_t configs_mcpwm = {
24+
.frequency = 1E3, // 1 kHz
25+
.cmpr_a = 0.0f,
26+
.cmpr_b = 0.0f,
27+
.duty_mode = MCPWM_DUTY_MODE_0,
28+
.counter_mode = MCPWM_UP_COUNTER,
29+
};
30+
31+
void _wheel(int speed_left, int speed_right) {
32+
int dir1 = speed_left > 0 ? 2 : speed_left < 0 ? 1 : 0;
33+
if (speed_left < 0) {
34+
speed_left = speed_left * -1;
35+
}
36+
if (dir1 == 0) {
37+
mcpwm_set_signal_high(MCPWM_UNIT_0, MCPWM_TIMER_0, MCPWM_OPR_A);
38+
mcpwm_set_signal_high(MCPWM_UNIT_0, MCPWM_TIMER_0, MCPWM_OPR_B);
39+
} else if (dir1 == 1) {
40+
mcpwm_set_signal_high(MCPWM_UNIT_0, MCPWM_TIMER_0, MCPWM_OPR_A);
41+
mcpwm_set_duty(MCPWM_UNIT_0, MCPWM_TIMER_0, MCPWM_OPR_B, 100 - speed_left);
42+
mcpwm_set_duty_type(MCPWM_UNIT_0, MCPWM_TIMER_0, MCPWM_OPR_B, MCPWM_DUTY_MODE_0);
43+
} else if (dir1 == 2) {
44+
mcpwm_set_duty(MCPWM_UNIT_0, MCPWM_TIMER_0, MCPWM_OPR_A, 100 - speed_left);
45+
mcpwm_set_signal_high(MCPWM_UNIT_0, MCPWM_TIMER_0, MCPWM_OPR_B);
46+
mcpwm_set_duty_type(MCPWM_UNIT_0, MCPWM_TIMER_0, MCPWM_OPR_A, MCPWM_DUTY_MODE_0);
47+
}
48+
49+
int dir2 = speed_right > 0 ? 1 : speed_right < 0 ? 2 : 0;
50+
if (speed_right < 0) {
51+
speed_right = speed_right * -1;
52+
}
53+
if (dir2 == 0) {
54+
mcpwm_set_signal_high(MCPWM_UNIT_1, MCPWM_TIMER_1, MCPWM_OPR_A);
55+
mcpwm_set_signal_high(MCPWM_UNIT_1, MCPWM_TIMER_1, MCPWM_OPR_B);
56+
} else if (dir2 == 1) {
57+
mcpwm_set_signal_high(MCPWM_UNIT_1, MCPWM_TIMER_1, MCPWM_OPR_A);
58+
mcpwm_set_duty(MCPWM_UNIT_1, MCPWM_TIMER_1, MCPWM_OPR_B, 100 - speed_right);
59+
mcpwm_set_duty_type(MCPWM_UNIT_1, MCPWM_TIMER_1, MCPWM_OPR_B, MCPWM_DUTY_MODE_0);
60+
} else if (dir2 == 2) {
61+
mcpwm_set_duty(MCPWM_UNIT_1, MCPWM_TIMER_1, MCPWM_OPR_A, 100 - speed_right);
62+
mcpwm_set_signal_high(MCPWM_UNIT_1, MCPWM_TIMER_1, MCPWM_OPR_B);
63+
mcpwm_set_duty_type(MCPWM_UNIT_1, MCPWM_TIMER_1, MCPWM_OPR_A, MCPWM_DUTY_MODE_0);
64+
}
65+
}
66+
67+
void _sleep(uint32_t time) {
68+
mp_hal_delay_ms(time * 1000);
69+
}
70+
71+
STATIC mp_obj_t motor_init() {
72+
static bool init = false;
73+
if (!init) {
74+
mcpwm_gpio_init(MCPWM_UNIT_0, MCPWM0A, M1A_PIN);
75+
mcpwm_gpio_init(MCPWM_UNIT_0, MCPWM0B, M1B_PIN);
76+
mcpwm_gpio_init(MCPWM_UNIT_1, MCPWM1A, M2A_PIN);
77+
mcpwm_gpio_init(MCPWM_UNIT_1, MCPWM1B, M2B_PIN);
78+
79+
/*
80+
mcpwm_group_set_resolution(MCPWM_CH, ...);
81+
mcpwm_timer_set_resolution(MCPWM_CH, MCPWM_TIMER, ...);
82+
*/
83+
84+
mcpwm_init(MCPWM_UNIT_0, MCPWM_TIMER_0, &configs_mcpwm);
85+
mcpwm_init(MCPWM_UNIT_1, MCPWM_TIMER_1, &configs_mcpwm);
86+
}
87+
88+
return mp_const_none;
89+
}
90+
MP_DEFINE_CONST_FUN_OBJ_0(motor_init_obj, motor_init);
91+
92+
STATIC mp_obj_t motor_wheel(mp_obj_t speed_left_obj, mp_obj_t speed_right_obj) {
93+
mp_int_t speed_left = mp_obj_get_int(speed_left_obj);
94+
mp_int_t speed_right = mp_obj_get_int(speed_right_obj);
95+
96+
_wheel(speed_left, speed_right);
97+
98+
return mp_const_none;
99+
}
100+
MP_DEFINE_CONST_FUN_OBJ_2(motor_wheel_obj, motor_wheel);
101+
102+
STATIC mp_obj_t motor_forward(mp_obj_t speed_obj, mp_obj_t time_obj) {
103+
mp_int_t speed = mp_obj_get_int(speed_obj);
104+
mp_int_t time = mp_obj_get_int(time_obj);
105+
106+
_wheel(speed, speed);
107+
_sleep(time);
108+
_wheel(0, 0);
109+
110+
return mp_const_none;
111+
}
112+
MP_DEFINE_CONST_FUN_OBJ_2(motor_forward_obj, motor_forward);
113+
114+
STATIC mp_obj_t motor_backward(mp_obj_t speed_obj, mp_obj_t time_obj) {
115+
mp_int_t speed = mp_obj_get_int(speed_obj);
116+
mp_int_t time = mp_obj_get_int(time_obj);
117+
118+
_wheel(speed * -1, speed * -1);
119+
_sleep(time);
120+
_wheel(0, 0);
121+
122+
return mp_const_none;
123+
}
124+
MP_DEFINE_CONST_FUN_OBJ_2(motor_backward_obj, motor_backward);
125+
126+
STATIC mp_obj_t motor_turn_left(mp_obj_t speed_obj, mp_obj_t time_obj) {
127+
mp_int_t speed = mp_obj_get_int(speed_obj);
128+
mp_int_t time = mp_obj_get_int(time_obj);
129+
130+
_wheel(0, speed);
131+
_sleep(time);
132+
_wheel(0, 0);
133+
134+
return mp_const_none;
135+
}
136+
MP_DEFINE_CONST_FUN_OBJ_2(motor_turn_left_obj, motor_turn_left);
137+
138+
STATIC mp_obj_t motor_turn_right(mp_obj_t speed_obj, mp_obj_t time_obj) {
139+
mp_int_t speed = mp_obj_get_int(speed_obj);
140+
mp_int_t time = mp_obj_get_int(time_obj);
141+
142+
_wheel(speed, 0);
143+
_sleep(time);
144+
_wheel(0, 0);
145+
146+
return mp_const_none;
147+
}
148+
MP_DEFINE_CONST_FUN_OBJ_2(motor_turn_right_obj, motor_turn_right);
149+
150+
STATIC mp_obj_t motor_move(mp_obj_t dir_obj, mp_obj_t speed_obj) {
151+
mp_int_t dir = mp_obj_get_int(dir_obj);
152+
mp_int_t speed = mp_obj_get_int(speed_obj);
153+
154+
if (dir == FORWARD) {
155+
_wheel(speed, speed);
156+
} else if (dir == BACKWARD) {
157+
_wheel(speed * -1, speed * -1);
158+
} else if (dir == TURN_LEFT) {
159+
_wheel(0, speed);
160+
} else if (dir == TURN_RIGHT) {
161+
_wheel(speed, 0);
162+
}
163+
164+
return mp_const_none;
165+
}
166+
MP_DEFINE_CONST_FUN_OBJ_2(motor_move_obj, motor_move);
167+
168+
STATIC mp_obj_t motor_stop() {
169+
_wheel(0, 0);
170+
171+
return mp_const_none;
172+
}
173+
MP_DEFINE_CONST_FUN_OBJ_0(motor_stop_obj, motor_stop);
174+
175+
STATIC const mp_rom_map_elem_t motor_module_globals_table[] = {
176+
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_motor) },
177+
{ MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&motor_init_obj) },
178+
{ MP_ROM_QSTR(MP_QSTR_wheel), MP_ROM_PTR(&motor_wheel_obj) },
179+
{ MP_ROM_QSTR(MP_QSTR_forward), MP_ROM_PTR(&motor_forward_obj) },
180+
{ MP_ROM_QSTR(MP_QSTR_backward), MP_ROM_PTR(&motor_backward_obj) },
181+
{ MP_ROM_QSTR(MP_QSTR_turn_left), MP_ROM_PTR(&motor_turn_left_obj) },
182+
{ MP_ROM_QSTR(MP_QSTR_turn_right), MP_ROM_PTR(&motor_turn_right_obj) },
183+
{ MP_ROM_QSTR(MP_QSTR_move), MP_ROM_PTR(&motor_move_obj) },
184+
{ MP_ROM_QSTR(MP_QSTR_stop), MP_ROM_PTR(&motor_stop_obj) },
185+
186+
{ MP_ROM_QSTR(MP_QSTR_FORWARD), MP_ROM_INT(FORWARD) },
187+
{ MP_ROM_QSTR(MP_QSTR_BACKWARD), MP_ROM_INT(BACKWARD) },
188+
{ MP_ROM_QSTR(MP_QSTR_TURN_LEFT), MP_ROM_INT(TURN_LEFT) },
189+
{ MP_ROM_QSTR(MP_QSTR_TURN_RIGHT), MP_ROM_INT(TURN_RIGHT) },
190+
};
191+
STATIC MP_DEFINE_CONST_DICT(motor_module_globals, motor_module_globals_table);
192+
193+
const mp_obj_module_t motor_user_cmodule = {
194+
.base = { &mp_type_module },
195+
.globals = (mp_obj_dict_t *)&motor_module_globals,
196+
};
197+
198+
MP_REGISTER_MODULE(MP_QSTR_motor, motor_user_cmodule, 1);
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
"""
21
from machine import Pin
32
import display
43
import buzzer
4+
import motor
55

6+
motor.init()
67
Pin(25, Pin.OUT).value(0) # LED2
78
Pin(23, Pin.OUT).value(0) # LDE1
9+
buzzer.off()
810
display.fill(0)
911
display.show()
10-
buzzer.off()
11-
"""

ports/esp32/boards/Rapbit32/modules/motor.py

Lines changed: 0 additions & 85 deletions
This file was deleted.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
sasda
2+
dsfsdfsdf
3+
sasdafddf
4+
wadSSA;DeviceASDAS
5+
DeviceASDASSA'DSA;DeviceASDASDASD
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
add_library(usermod_motor INTERFACE)
2+
3+
target_sources(usermod_motor INTERFACE
4+
${CMAKE_CURRENT_LIST_DIR}/modmotor.c
5+
)
6+
7+
target_include_directories(usermod_motor INTERFACE
8+
${CMAKE_CURRENT_LIST_DIR}
9+
)
10+
11+
target_link_libraries(usermod INTERFACE usermod_motor)

0 commit comments

Comments
 (0)