Skip to content

Commit

Permalink
Pass mypy and link issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Avasam committed Aug 26, 2024
1 parent 4241688 commit e14cbb0
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 150 deletions.
285 changes: 143 additions & 142 deletions examples/drive_around_table.py
Original file line number Diff line number Diff line change
@@ -1,142 +1,143 @@
#!python
# -*- coding: utf-8 -*-

# flake8: noqa

# This is a port of %% Example 3: Drive Around Table
# from the Matlab rwth libraries
# In this little demo, our bot drives a square on the floor around a well known table.

from jaraco.nxt import Connection
from jaraco.nxt.messages import (
SetOutputState,
OutputPort,
ResetMotorPosition,
GetOutputState,
RunState,
RegulationMode,
)
import time

# Constants
table_length = 2850 # in degrees of motor rotations :-)
quarter_turn_ticks = 245 # in motor degrees, how much is a 90° turn of the bot?

power_mul = 1 # my motors are upside down

port = 3


def initialize():
# Initialize Motors...
# we send this in case they should still be spinning or something...
connection.send(SetOutputState(OutputPort.all))
# and we also "clean up" before we start:
ports = (OutputPort.b, OutputPort.c)
for port in ports:
connection.send(ResetMotorPosition(port))


# todo - port this to the library
def wait_for_motor(conn, port):
cmd = GetOutputState(port)
status = RunState.running
while status == RunState.running:
conn.send(cmd)
status = conn.receive().run_state
print('run_state is', status)
time.sleep(0.1)
print('exiting wait for motor')


def run():
# Start the engines, main loop begins (repeated 4 times)
# 4 times because we got 4 equal sides of the table :-)
for j in range(4):
j += 1

# Build Drive commands
cmds = [
SetOutputState(
OutputPort.b,
motor_on=True,
regulation_mode=RegulationMode.motor_sync,
run_state=RunState.running,
set_power=power_mul * 75,
tacho_limit=table_length,
turn_ratio=0, # straight ahead
),
SetOutputState(
OutputPort.c,
motor_on=True,
regulation_mode=RegulationMode.motor_sync,
run_state=RunState.running,
set_power=power_mul * 75,
tacho_limit=table_length,
turn_ratio=0, # straight ahead
),
]
list(map(connection.send, cmds))

# let the robot start:
time.sleep(1)

# Check for the end end of table
wait_for_motor(connection, OutputPort.b)

# give it a little time to correct its mistakes (hey synchronisation
# mode :-)
time.sleep(2)

# apparently we've stopped!
# then release the motors
initialize()
# if we don't do that, syncing again doesn't work

# and again, if we don't rest relative counters, synced turning etc doesnt work...

# Now please turn

# Build Drive commands
cmds = [
SetOutputState(
OutputPort.b,
motor_on=True,
regulation_mode=RegulationMode.motor_sync,
run_state=RunState.running,
set_power=power_mul * 75, # slower is more accurate
tacho_limit=quarter_turn_ticks,
turn_ratio=100, # turn right
),
SetOutputState(
OutputPort.c,
motor_on=True,
regulation_mode=RegulationMode.motor_sync,
run_state=RunState.running,
set_power=power_mul * 75, # slower is more accurate
tacho_limit=quarter_turn_ticks,
turn_ratio=100, # turn right
),
]

list(map(connection.send, cmds))

# leave the bot time to start turning
time.sleep(1)

# Check for the end of rotation
wait_for_motor(connection, OutputPort.b)

# give it a little time to correct its mistakes (hey synchronisation
# mode :-)
time.sleep(2)

# apparently we've stopped!
# then release the motors
initialize()


if __name__ == '__main__':
connection = Connection(port)
initialize()
run()
#!python
# -*- coding: utf-8 -*-

# flake8: noqa

# This is a port of %% Example 3: Drive Around Table
# from the Matlab rwth libraries
# In this little demo, our bot drives a square on the floor around a well known table.

from jaraco.nxt import Connection
from jaraco.nxt.messages import (
SetOutputState,
OutputPort,
ResetMotorPosition,
GetOutputState,
RunState,
RegulationMode,
)
import time

# Constants
table_length = 2850 # in degrees of motor rotations :-)
quarter_turn_ticks = 245 # in motor degrees, how much is a 90° turn of the bot?

power_mul = 1 # my motors are upside down

port = 3


def initialize():
# Initialize Motors...
# we send this in case they should still be spinning or something...
connection.send(SetOutputState(OutputPort.all))
# and we also "clean up" before we start:
ports = (OutputPort.b, OutputPort.c)
for port in ports:
connection.send(ResetMotorPosition(port))


# todo - port this to the library
def wait_for_motor(conn, port):
cmd = GetOutputState(port)
status = RunState.running
while status == RunState.running:
conn.send(cmd)
status = conn.receive().run_state
print('run_state is', status)
time.sleep(0.1)
print('exiting wait for motor')


def run():
# Start the engines, main loop begins (repeated 4 times)
# 4 times because we got 4 equal sides of the table :-)
for j in range(4):
j += 1

# Build Drive commands
cmds = [
SetOutputState(
OutputPort.b,
motor_on=True,
regulation_mode=RegulationMode.motor_sync,
run_state=RunState.running,
set_power=power_mul * 75,
tacho_limit=table_length,
turn_ratio=0, # straight ahead
),
SetOutputState(
OutputPort.c,
motor_on=True,
regulation_mode=RegulationMode.motor_sync,
run_state=RunState.running,
set_power=power_mul * 75,
tacho_limit=table_length,
turn_ratio=0, # straight ahead
),
]
list(map(connection.send, cmds))

# let the robot start:
time.sleep(1)

# Check for the end end of table
wait_for_motor(connection, OutputPort.b)

# give it a little time to correct its mistakes (hey synchronisation
# mode :-)
time.sleep(2)

# apparently we've stopped!
# then release the motors
initialize()
# if we don't do that, syncing again doesn't work

# and again, if we don't rest relative counters, synced turning etc doesnt work...

# Now please turn

# Build Drive commands
cmds = [
SetOutputState(
OutputPort.b,
motor_on=True,
regulation_mode=RegulationMode.motor_sync,
run_state=RunState.running,
set_power=power_mul * 75, # slower is more accurate
tacho_limit=quarter_turn_ticks,
turn_ratio=100, # turn right
),
SetOutputState(
OutputPort.c,
motor_on=True,
regulation_mode=RegulationMode.motor_sync,
run_state=RunState.running,
set_power=power_mul * 75, # slower is more accurate
tacho_limit=quarter_turn_ticks,
turn_ratio=100, # turn right
),
]

list(map(connection.send, cmds))

# leave the bot time to start turning
time.sleep(1)

# Check for the end of rotation
wait_for_motor(connection, OutputPort.b)

# give it a little time to correct its mistakes (hey synchronisation
# mode :-)
time.sleep(2)

# apparently we've stopped!
# then release the motors
initialize()


if __name__ == '__main__':
# FIXME: ValueError: "port" must be None or a string, not <class 'int'>
connection = Connection(port) # type: ignore[arg-type]
initialize()
run()
5 changes: 3 additions & 2 deletions jaraco/nxt/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@
Requires a bluetooth connection (and utilizes serial protocol).
"""

import traceback
import logging
import traceback

import serial

from jaraco.nxt import messages

try:
import bluetooth
# Requires PyBluez or pybluez2 to be installed
import bluetooth # type: ignore[import-not-found]
except ImportError:
import types

Expand Down
4 changes: 2 additions & 2 deletions jaraco/nxt/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
% __name__
)
from jaraco.nxt import Connection
from jaraco.nxt.messages import SetOutputState, OutputPort, RunState
from jaraco.nxt.messages import OutputPort, RunState, SetOutputState


class MotorController:
Expand Down Expand Up @@ -144,7 +144,7 @@ def _get_options():


def print_voltage(controller):
from routine import get_voltage
from jaraco.nxt.routine import get_voltage

voltage = get_voltage(controller.conn)
print('Successfully connected to device; battery voltage is %f' % voltage)
Expand Down
4 changes: 4 additions & 0 deletions mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,7 @@ explicit_package_bases = True

# Disable overload-overlap due to many false-positives
disable_error_code = overload-overlap

# jaraco/jaraco.input#2
[mypy-jaraco.input.*]
ignore_missing_imports = True
5 changes: 1 addition & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ type = [
"pytest-mypy",

# local
"types-pyserial"
]

input = [
Expand All @@ -72,7 +73,3 @@ nxt-control = "jaraco.nxt.controller:serve_forever"


[tool.setuptools_scm]


[tool.pytest-enabler.mypy]
# Disabled due to jaraco/skeleton#143

0 comments on commit e14cbb0

Please sign in to comment.