Skip to content

Commit b673a73

Browse files
authored
feat(robot): improve rotation of the robot with the knob (#37)
* better driving rotation * wip * drive with angular velocity * reduce velocity angular * renameing * revert
1 parent 23b2f06 commit b673a73

File tree

2 files changed

+21
-12
lines changed

2 files changed

+21
-12
lines changed

sketches/controller/main.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
import espnow
33
import struct
44
import sys
5+
from time import sleep
56

67
from modulino import ModulinoKnob, ModulinoButtons
78

9+
810
ALVIK_MAC = "74:4d:bd:a2:08:74"
911

1012
sta = network.WLAN(network.STA_IF) # Or network.AP_IF
@@ -73,9 +75,11 @@ def lift():
7375
buttons.on_button_c_press = lambda: go_backward()
7476

7577
knob.on_press = lambda: lift()
76-
knob.on_rotate_clockwise = lambda steps, value: turn_right()
77-
knob.on_rotate_counter_clockwise = lambda steps, value: turn_left()
78+
knob.on_rotate_clockwise = lambda _, __: turn_right()
79+
knob.on_rotate_counter_clockwise = lambda _, __: turn_left()
7880

7981
while True:
8082
knob.update()
8183
buttons.update()
84+
85+
sleep(0.1)

sketches/robot/main.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
print("ImportError: ModulinoPixels not installed")
1515
sys.exit(-1)
1616

17-
VELOCITY = 13 # cm/s (max is 13cm/s)
18-
ANGULAR_VELOCITY = 250 # (max 320.8988764044944)
17+
LINEAR_VELOCITY = 10 # (max 13cm/s)
18+
ANGULAR_VELOCITY = 75 # (max 320.8988764044944)
1919
FREEZE_FOR_SECONDS = 5
2020
REVERT_CONTROLLER_FOR_SECONDS = 10
2121

@@ -43,9 +43,12 @@
4343
isPlayingReverted = False # if true the controller action are reverted
4444
lifState = 0 # 0 = down, 1 = up
4545

46-
46+
lin = 0
47+
ang = 0
4748
def receiveAndExecuteFromEspNow():
4849
global lifState
50+
global lin
51+
global ang
4952
_, msg = e.recv(
5053
timeout_ms=0
5154
) # TODO: See ESPNow.irecv() for a memory-friendly alternative.
@@ -55,18 +58,19 @@ def receiveAndExecuteFromEspNow():
5558
return
5659
unpacked_message = struct.unpack("B", msg)
5760
msg_type = unpacked_message[0]
61+
5862
if int(msg_type) == STOP:
59-
a.drive(0, 0)
63+
lin, ang = 0, 0
6064
elif int(msg_type) == GO_FORWARD:
61-
v = VELOCITY if isPlayingReverted else -VELOCITY
62-
a.drive(v, 0)
65+
lin = LINEAR_VELOCITY if isPlayingReverted else -LINEAR_VELOCITY
66+
ang = 0
6367
elif int(msg_type) == GO_BACKWARD:
64-
v = -VELOCITY if isPlayingReverted else VELOCITY
65-
a.drive(v, 0)
68+
lin = -LINEAR_VELOCITY if isPlayingReverted else LINEAR_VELOCITY
69+
ang = 0
6670
elif int(msg_type) == TURN_LEFT:
67-
a.drive(0, ANGULAR_VELOCITY)
71+
ang = -ANGULAR_VELOCITY if isPlayingReverted else ANGULAR_VELOCITY
6872
elif int(msg_type) == TURN_RIGHT:
69-
a.drive(0, -ANGULAR_VELOCITY)
73+
ang = ANGULAR_VELOCITY if isPlayingReverted else -ANGULAR_VELOCITY
7074
elif int(msg_type) == LIFT:
7175
if lifState == 0:
7276
liftUp()
@@ -77,6 +81,7 @@ def receiveAndExecuteFromEspNow():
7781
else:
7882
print("unknown command type ", msg_type)
7983

84+
a.drive(lin, ang)
8085

8186
def liftUp():
8287
a.set_servo_positions(180, 0)

0 commit comments

Comments
 (0)