1919import time
2020
2121import nxt .error
22+ import nxt .motor
2223import nxt .sensor
2324from nxt .telegram import Opcode , Telegram
2425
@@ -272,6 +273,15 @@ def find_modules(self, pattern="*.*"):
272273
273274 get_sensor = nxt .sensor .get_sensor
274275
276+ def get_motor (self , port ):
277+ """Return a motor object connected to one of the brick output port.
278+
279+ :param nxt.motor.Port port: Output port identifier.
280+ :return: The motor object.
281+ :rtype: nxt.motor.Motor
282+ """
283+ return nxt .motor .Motor (self , port )
284+
275285 def _cmd (self , tgram ):
276286 """Send a message to the NXT brick and read reply.
277287
@@ -332,27 +342,30 @@ def play_tone(self, frequency_hz, duration_ms):
332342 self ._cmd (tgram )
333343
334344 def set_output_state (
335- self , port , power , mode , regulation , turn_ratio , run_state , tacho_limit
345+ self , port , power , mode , regulation_mode , turn_ratio , run_state , tacho_limit
336346 ):
337347 """Set output port state on the brick.
338348
339- :param int port: Output port constant .
349+ :param nxt.motor.Port port: Output port identifier .
340350 :param int power: Motor speed or power level (-100 to 100).
341- :param int mode: Motor power mode.
342- :param int regulation : Motor regulation mode.
351+ :param nxt.motor.Mode mode: Motor power mode.
352+ :param nxt.motor.RegulationMode regulation_mode : Motor regulation mode.
343353 :param int turn_ratio: Turn ratio (-100 to 100). Negative value shift power to
344354 the left motor.
345- :param int run_state: Motor run state.
355+ :param nxt.motor.RunState run_state: Motor run state.
346356 :param int tacho_limit: Number of degrees the motor should rotate relative to
347357 the current position.
358+
359+ .. warning:: This is a low level function, prefer to use
360+ :meth:`nxt.motor.Motor`, you can get one from :meth:`get_motor`.
348361 """
349362 tgram = Telegram (Opcode .DIRECT_SET_OUT_STATE , reply_req = False )
350- tgram .add_u8 (port )
363+ tgram .add_u8 (port . value )
351364 tgram .add_s8 (power )
352- tgram .add_u8 (mode )
353- tgram .add_u8 (regulation )
365+ tgram .add_u8 (mode . value )
366+ tgram .add_u8 (regulation_mode . value )
354367 tgram .add_s8 (turn_ratio )
355- tgram .add_u8 (run_state )
368+ tgram .add_u8 (run_state . value )
356369 tgram .add_u32 (tacho_limit )
357370 self ._cmd (tgram )
358371
@@ -372,18 +385,19 @@ def set_input_mode(self, port, sensor_type, sensor_mode):
372385 def get_output_state (self , port ):
373386 """Get output port state from the brick.
374387
375- :param int port: Output port constant .
376- :return: A tuple with `port`, `power`, `mode`, `regulation `, `turn_ratio`,
388+ :param nxt.motor.Port port: Output port identifier .
389+ :return: A tuple with `port`, `power`, `mode`, `regulation_mode `, `turn_ratio`,
377390 `run_state`, `tacho_limit`, `tacho_count`, `block_tacho_count`, and
378391 `rotation_count`.
379- :rtype: (int, int, int, int, int, int, int, int, int, int)
392+ :rtype: (nxt.motor.Port, int, nxt.motor.Mode, nxt.motor.RegulationMode, int,
393+ nxt.motor.RunState, int, int, int, int)
380394
381395 Return value details:
382396
383- - **port** Output port constant .
397+ - **port** Output port identifier .
384398 - **power** Motor speed or power level (-100 to 100).
385399 - **mode** Motor power mode.
386- - **regulation ** Motor regulation mode.
400+ - **regulation_mode ** Motor regulation mode.
387401 - **turn_ratio** Turn ratio (-100 to 100). Negative value shift power to the
388402 left motor.
389403 - **run_state** Motor run state.
@@ -392,16 +406,19 @@ def get_output_state(self, port):
392406 "block" start.
393407 - **rotation_count** Number of degrees the motor rotated relative to the program
394408 start.
409+
410+ .. warning:: This is a low level function, prefer to use
411+ :meth:`nxt.motor.Motor`, you can get one from :meth:`get_motor`.
395412 """
396413 tgram = Telegram (Opcode .DIRECT_GET_OUT_STATE )
397- tgram .add_u8 (port )
414+ tgram .add_u8 (port . value )
398415 tgram = self ._cmd (tgram )
399- port = tgram .parse_u8 ()
416+ port = nxt . motor . Port ( tgram .parse_u8 () )
400417 power = tgram .parse_s8 ()
401- mode = tgram .parse_u8 ()
402- regulation = tgram .parse_u8 ()
418+ mode = nxt . motor . Mode ( tgram .parse_u8 () )
419+ regulation_mode = nxt . motor . RegulationMode ( tgram .parse_u8 () )
403420 turn_ratio = tgram .parse_s8 ()
404- run_state = tgram .parse_u8 ()
421+ run_state = nxt . motor . RunState ( tgram .parse_u8 () )
405422 tacho_limit = tgram .parse_u32 ()
406423 tacho_count = tgram .parse_s32 ()
407424 block_tacho_count = tgram .parse_s32 ()
@@ -410,7 +427,7 @@ def get_output_state(self, port):
410427 port ,
411428 power ,
412429 mode ,
413- regulation ,
430+ regulation_mode ,
414431 turn_ratio ,
415432 run_state ,
416433 tacho_limit ,
@@ -494,12 +511,15 @@ def message_write(self, inbox, message):
494511 def reset_motor_position (self , port , relative ):
495512 """Reset block or program motor position for a brick output port.
496513
497- :param int port: Output port constant .
514+ :param nxt.motor.Port port: Output port identifier .
498515 :param bool relative: If ``True``, reset block position, if ``False``, reset
499516 program position.
517+
518+ .. warning:: This is a low level function, prefer to use
519+ :meth:`nxt.motor.Motor`, you can get one from :meth:`get_motor`.
500520 """
501521 tgram = Telegram (Opcode .DIRECT_RESET_POSITION )
502- tgram .add_u8 (port )
522+ tgram .add_u8 (port . value )
503523 tgram .add_bool (relative )
504524 self ._cmd (tgram )
505525
0 commit comments