Skip to content

Commit 5dfe8b9

Browse files
committed
First commit of motor_move() API
1 parent 5497666 commit 5dfe8b9

File tree

3 files changed

+82
-18
lines changed

3 files changed

+82
-18
lines changed

neobot/neosoco.py

Lines changed: 54 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
# Free Software Foundation, Inc., 59 Temple Place, Suite 330,
1717
# Boston, MA 02111-1307 USA
1818

19+
20+
import time
21+
1922
from neobot.runner import Runner
2023
from neobot.util import Util
2124
from neobot.model import Robot
@@ -25,14 +28,16 @@
2528
class Neosoco(Robot):
2629
ID = "kr.neobot.physical.neosoco"
2730

28-
OUTPUT_1 = 0x00400000
29-
OUTPUT_2 = 0x00400001
30-
OUTPUT_3 = 0x00400002
31-
INPUT_1 = 0x00400003
32-
INPUT_2 = 0x00400004
33-
INPUT_3 = 0x00400005
34-
REMOCTL = 0x00400006
35-
BATTERY = 0x00400007
31+
OUTPUT_1 = 0x00400000
32+
OUTPUT_2 = 0x00400001
33+
OUTPUT_3 = 0x00400002
34+
INPUT_1 = 0x00400003
35+
INPUT_2 = 0x00400004
36+
INPUT_3 = 0x00400005
37+
REMOCTL = 0x00400006
38+
BATTERY = 0x00400007
39+
LEFT_MOTOR = 0x00400008
40+
RIGHT_MOTOR = 0x00400009
3641

3742
LED_OFF = 0
3843
LED_BLUE = 1
@@ -280,6 +285,23 @@ class Neosoco(Robot):
280285
"digital_output": IO_MODE_DIGITAL_OUTPUT,
281286
"digital output": IO_MODE_DIGITAL_OUTPUT
282287
}
288+
_MOTOR_PERCENT_CVT = {
289+
'100': 15,
290+
'90': 14,
291+
'80': 13,
292+
'70': 12,
293+
'60': 10,
294+
'50': 9,
295+
'40': 7,
296+
'30': 5,
297+
'20': 3,
298+
'10': 2,
299+
'0': 0
300+
}
301+
_MOTOR_DIR = {
302+
'forward': 16,
303+
'backward': 32
304+
}
283305
_robots = {}
284306

285307
def __init__(self, index=0, port_name=None):
@@ -404,6 +426,30 @@ def led_on(self, port: str, brightness: str):
404426
else:
405427
raise TypeError
406428

429+
def motor_move(self, direction: str):
430+
if isinstance(direction, str):
431+
if direction.lower() =='forward':
432+
self.write(Neosoco.LEFT_MOTOR, self._MOTOR_DIR['forward']+self._MOTOR_PERCENT_CVT['60'])
433+
self.write(Neosoco.RIGHT_MOTOR, self._MOTOR_DIR['forward']+self._MOTOR_PERCENT_CVT['60'])
434+
elif direction.lower() =='backward':
435+
self.write(Neosoco.LEFT_MOTOR, self._MOTOR_DIR['backward']+self._MOTOR_PERCENT_CVT['60'])
436+
self.write(Neosoco.RIGHT_MOTOR, self._MOTOR_DIR['backward']+self._MOTOR_PERCENT_CVT['60'])
437+
elif direction.lower() =='left':
438+
self.write(Neosoco.LEFT_MOTOR, 0)
439+
self.write(Neosoco.RIGHT_MOTOR, self._MOTOR_DIR['forward']+self._MOTOR_PERCENT_CVT['60'])
440+
elif direction.lower() =='right':
441+
self.write(Neosoco.LEFT_MOTOR, self._MOTOR_DIR['forward']+self._MOTOR_PERCENT_CVT['60'])
442+
self.write(Neosoco.RIGHT_MOTOR, 0)
443+
elif direction.lower() =='stop':
444+
self.write(Neosoco.LEFT_MOTOR, 0)
445+
self.write(Neosoco.RIGHT_MOTOR, 0)
446+
else:
447+
Util.print_error('Wrong value of direction')
448+
raise ValueError
449+
else:
450+
raise TypeError
451+
time.sleep(0.1) # Since brocast from cotroller is per 100ms
452+
407453
def wheels(self, left_velocity, right_velocity=None):
408454
self.write(Neosoco.LINE_TRACER_MODE, Neosoco.LINE_TRACER_MODE_OFF)
409455
if isinstance(left_velocity, (int, float)):

neobot/neosoco_neobot.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,9 @@ def _create_model(self):
125125
dict[Neosoco.INPUT_3] = self._input_3_device = self._add_device(Neosoco.INPUT_3, "Input3", DeviceType.SENSOR, DataType.INTEGER, 1, 0, 255, 0)
126126
dict[Neosoco.REMOCTL] = self._remoctl_device = self._add_device(Neosoco.REMOCTL, "RemoteCtl", DeviceType.SENSOR, DataType.INTEGER, 1, 0, 255, 0)
127127
dict[Neosoco.BATTERY] = self._battery_device = self._add_device(Neosoco.BATTERY, "Battery", DeviceType.SENSOR, DataType.INTEGER, 1, 0, 255, 0)
128-
128+
dict[Neosoco.LEFT_MOTOR] = self._left_motor_device = self._add_device(Neosoco.LEFT_MOTOR, "LeftMotor", DeviceType.EFFECTOR, DataType.INTEGER, 1, 0, 47, 0)
129+
dict[Neosoco.RIGHT_MOTOR] = self._right_motor_device = self._add_device(Neosoco.RIGHT_MOTOR, "RightMotor", DeviceType.EFFECTOR, DataType.INTEGER, 1, 0, 47, 0)
130+
129131
def find_device_by_id(self, device_id):
130132
return self._device_dict.get(device_id)
131133

@@ -135,7 +137,7 @@ def _run(self):
135137
if self._receive(self._connector):
136138
self._send(self._connector)
137139
self._releasing = False
138-
time.sleep(0.01)
140+
time.sleep(0.005)
139141
except:
140142
pass
141143

@@ -177,6 +179,12 @@ def _dispose(self):
177179
def _reset(self):
178180
super(NeosocoNeobot, self)._reset()
179181

182+
self._output_1 = 0
183+
self._output_2 = 0
184+
self._output_3 = 0
185+
self._left_motor = 0
186+
self._right_motor = 0
187+
180188
self._left_wheel = 0
181189
self._right_wheel = 0
182190
self._buzzer = 0
@@ -221,6 +229,8 @@ def _request_motoring_data(self):
221229
self._output_1 = self._output_1_device.read()
222230
self._output_2 = self._output_2_device.read()
223231
self._output_3 = self._output_3_device.read()
232+
self._left_motor = self._left_motor_device.read()
233+
self._right_motor = self._right_motor_device.read()
224234
self._clear_written()
225235

226236
def _color_to_rgb(self, color):
@@ -246,8 +256,8 @@ def _encode_motoring_packet(self, address):
246256
result += self._to_hex(self._output_1) # OUT1
247257
result += self._to_hex(self._output_2) # OUT2
248258
result += self._to_hex(self._output_3) # OUT3
249-
result += self._to_hex(0) # MLA
250-
result += self._to_hex(0) # MRA
259+
result += self._to_hex(self._left_motor) # MLA
260+
result += self._to_hex(self._right_motor) # MRA
251261
result += self._to_hex(0) # BUZZER
252262
result += self._to_hex(0) # FND
253263
result += self._to_hex(0) # Not Used

test.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,23 @@
33
n = Neosoco()
44

55
# case1) Turn on LED with 100% brightness during 1s
6-
# n.led_on("out1", '100')
6+
# n.led_on('out1', '100')
77
# wait(1000)
88

9-
n.set_value("out1", 255)
10-
wait(1000)
9+
# case2) Turn on LED with 100% brightness during 1s
10+
# n.set_value('out1', 255)
11+
# wait(1000)
1112

12-
# case2) Turn on LED when the distance is under 10cm
13+
# case3) Turn on LED when the distance is under 10cm
1314
# while True:
1415
# if n.get_value('in1') < 10:
15-
# n.led_on("out1", 255)
16+
# n.led_on('out1', '100')
1617
# else:
17-
# n.led_on("out1", 0)
18+
# n.set_value('out1', 0)
19+
20+
# case4) Move forth and back during 1s and stop
21+
n.motor_move('forward')
22+
wait(500)
23+
n.motor_move('backward')
24+
wait(500)
25+
n.motor_move('stop')

0 commit comments

Comments
 (0)