pycar package

Subpackages

Submodules

pycar.audio module

pycar.audio.get_devices()

Get the available audio devices.

Returns a tuple of the device index and its info.

This function is a generator, so you can use it like ``` for _,dev in get_devices():

print(dev.name)

```

pycar.audio.init()

Initializes PyAudio, but only if it hasn’t been initialized yet.

pycar.audio.main()

Example: listing audio devices and plotting one channel

pycar.audio.open(dev, **kwargs)

Opens an audio device as input.

dev should be the device index. Returns a PyAudio Stream object.

pycar.audio.update(frame, stream)

pycar.car module

Python abstraction of the KTY kit car

Author:

Petar V Peshev @ p.v.peshev@student.tudelft.nl

Further discussions:
  • Whether after sending command, the status should be updated by reading the status of the car or by updating

    the status field in the CarStatus object (current solution); if it is done by reading the status, then possibly the attributes of CarStatus should be protected and the update should be done through update_fields method

  • Whether the CarStatus object should be a dataclass

Issue: The abstraction does not contain a car simulation Solution: Implement a car simulation, and add a parameter to the car abstraction constructor to specify if physical

car or simulation is used

TODO: extend the car abstraction object with car simulation

class pycar.car.Car(serial_handle: SerialHandle)

Bases: object

Car abstraction object

Attributes:
_serial_handle: SerialHandle

Serial handle object for car communication [protected]

_car_status: CarStatus

Car status dataclass instance [protected]

Methods:
_send_command(command: AbstractCommand): Optional[bytes]

Sends a command to the car [protected]

enable_audio: None

Enables the audio beacon [public]

disable_audio: None

Disables the audio beacon [public]

set_bit_frequency(bit_frequency: int): None

Sets the bit frequency [public]

set_audio_code(audio_code: int): None

Sets the audio code [public]

set_direction(direction: int): None

Sets direction PWM setting [public]

set_carrier_frequency(carrier_frequency: int): None

Sets the carrier frequency [public]

set_motor_pwm(motor_pwm: int): None

Sets motor PWM setting [public]

set_repetition_counter(repetitions: int): None

Sets the repetition counter [public]

get_status(status_setting: str): bytes

Gets the car status and returns it in bytes; updates the car status attribute [public]

get_version: bytes

Gets the car firmware version and returns it in bytes; updates the car status attribute [public]

update_status: None

Updates the car status attribute with status and firmware version [public]

Property:
status: CarStatus

Returns the car status attribute

disable_audio() None

Disables the audio beacon

Returns:

None

enable_audio() None

Enables the audio beacon

Returns:

None

get_status(status_setting: str) bytes

Gets the car status; updates the car status attribute

Parameters:

status_setting (str) – Car status setting

Returns:

Car status in bytes

Return type:

bytes

get_version() bytes

Gets the car firmware version; updates the car status attribute

Returns:

The car firmware version in bytes

Return type:

bytes

set_audio_code(audio_code: int) None

Sets the audio code

Parameters:

audio_code (int) – Audio code setting

Returns:

None

set_bit_frequency(bit_frequency: int) None

Sets the bit frequency

Parameters:

bit_frequency (int) – Bit frequency setting

Returns:

None

set_carrier_frequency(carrier_frequency: int) None

Sets the carrier frequency

Parameters:

carrier_frequency (int) – Carrier frequency setting

Returns:

None

set_direction_pwm(direction_pwm: int) None

Sets direction PWM setting

Parameters:

direction_pwm (int) – Direction setting

Returns:

None

set_motor_pwm(motor_pwm: int) None

Sets motor PWM setting

Parameters:

motor_pwm (int) – Motor speed setting

Returns:

None

set_repetition_counter(repetitions: int) None

Sets the repetition counter

Parameters:

repetitions (int) – Repetition counter setting

Returns:

None

property status: CarStatus

Returns the car status attribute

Returns:

Car status attribute

Return type:

CarStatus

update_status() None

Updates the car status attribute with status and firmware version

Returns:

None

class pycar.car.CarStatus(status: bytes, version: bytes)

Bases: object

Dataclass containing status parameters of the car; the dataclass needs an initial car status in bytes to be constructed

Attributes:
motor_pwm: int

Contains the motor PWM setting

direction_pwm: int

Contains the direction PWM setting

audio_beacon: bool

Specifies if the audio beacon is enabled or not

audio_code: int

Contains the audio code

carrier_frequency: int

Contains the carrier frequency

bit_frequency: int

Contains the bit frequency

repetitions_counter: int

Contains the repetition counter

distance_usl: int

Contains the status of the left distance sensor

distance_usr: int

Contains the status of the right distance sensor

battery: float

Contains the status of the battery

version: bytes

Contains the car firmware version

Methods:
update_fields(status: bytes, *, version: Optional[bytes] = None): None

Updates the status fields [public]

audio_beacon: bool
audio_code: int
battery: float
bit_frequency: int
carrier_frequency: int
direction_pwm: int
distance_usl: int
distance_usr: int
motor_pwm: int
repetitions_counter: int
update_fields(status: bytes, *, version: bytes | None = None) None

Updates the status fields

Parameters:
  • status (bytes) – Car status in bytes

  • version (Optional[bytes]) – Car firmware version; updated only if parsed

Returns:

None

version: bytes

pycar.car_commands module

Abstract command and command objects for car communication

Author:

Petar V Peshev @ p.v.peshev@student.tudelft.nl

class pycar.car_commands.AbstractCommand(wait_for_response: bool, command_type: str, *, value: int | str | None = None, num_bytes: int | None = None)

Bases: ABC

Abstract command object, which contains main functionality needed for car communication.

Attributes:
_wait_for_response: bool

Specifies if the car sends a response to the command [protected]

_command_type: str

Specifies the car command [protected]

_command_data: Optional[bytes]

Specifies appended data to the command [protected]

Methods:
get_command_bytes: bytes

Returns the command in bytes [public]

Property:
wait_for_response: bool

Returns if the car sends a response to the command

get_command_bytes() bytes

Returns the command in bytes.

Returns:

The command in bytes

Return type:

bytes

property wait_for_response: bool

Returns if the car sends a response to the command or not.

Returns:

If the car sends a response to the command or not

Return type:

bool

class pycar.car_commands.AudioCodeCommand(audio_code: int = 0)

Bases: AbstractCommand

Command for changing the audio code

class pycar.car_commands.BitFrequencyCommand(bit_frequency: int = 5000)

Bases: AbstractCommand

Command for changing the bit frequency

class pycar.car_commands.CarrierFrequencyCommand(carrier_frequency: int = 15000)

Bases: AbstractCommand

Command for changing the carrier frequency

class pycar.car_commands.DirectionCommand(steering_pos: int)

Bases: AbstractCommand

Command for changing direction PWM setting

class pycar.car_commands.EnableAudioCommand(enable_audio: bool)

Bases: AbstractCommand

Command for enabling audio beacon

class pycar.car_commands.MotorCommand(motor_speed: int)

Bases: AbstractCommand

Command for changing the motor PWM setting

class pycar.car_commands.RepetitionCounterCommand(repetitions: int = 32)

Bases: AbstractCommand

Command for changing repetition counter

class pycar.car_commands.StatusCommand(setting: str = '')

Bases: AbstractCommand

Command for retrieving status

class pycar.car_commands.VersionCommand

Bases: AbstractCommand

Command for retrieving firmware version

pycar.car_run module

Contains run class and method for executing the project

Author:

Petar V Peshev @ p.v.peshev@student.tudelft.nl

Idea: The students should not do anything related to creating instances for the car, serial handle, or similar

objects; they should implement only a child class to CarRun, which implements only pre_processing, signal_processing, and post_processing. However, they have to implement a structured code that they can use in these functions, e.g. classes and methods implementing algorithms that can be called in the aforementioned methods of CarRun. The students perform a test by executing: if __name__ == ‘__main__’:

run_car_test(ImplementedByStudentsCarTestRunChildClass)

TODO: implement methods, which are not defined as student implementations

class pycar.car_run.CarTestRun

Bases: ABC

Car test run abstract base class; the students should be able to run a test by implementing only the pre-processing, signal processing, and post-processing methods. The students should be isolated from creating instances for the car and serial handle objects.

Attributes:
_serial_handle: SerialHandle

Instance of the serial handle [protected]

_car: Car

Instance of the car object [protected]

Methods:
pre_processing: None

Processing executed before the test; should be implemented by students [public]

signal_processing: None

Processing executed during the test; should be implemented by students [public]

post_processing: None

Processing executed after the test; should be implemented by students [public]

save_data: None

Saves the test data [public]

abstract post_processing() None

Processing executed after the test; should be implemented by students

Returns:

None

abstract pre_processing() None

Processing executed before the test; should be implemented by students

Returns:

None

save_data() None

Saves the test data

Returns:

None

abstract signal_processing() None

Processing executed during the test; should be implemented by students

Returns:

None

pycar.car_run.run_car_test(car_run_class: Type[CarTestRun]) None

This function executes the actions needed for performing tests

Parameters:

car_run_class (Type[CarTestRun]) – CarTestRun child object with updated signal processing methods

Returns:

None

pycar.serial_handle module

Handle and settings for serial communication based on PySerial

Author:

Petar V Peshev @ p.v.peshev@student.tudelft.nl

Issue: serial.tools.list_ports.comports() does not recognize pseudo terminals (PTY); serial.Serial() is able to

write and read to PTY

Solution: a wrapper for serial.tools.list_ports.comports() must be implemented to discover PTY ports TODO: implement a comport wrapper to discover PTY ports

class pycar.serial_handle.SerialHandle(*, pty_port: bool = False)

Bases: object

Serial communication handle

Attributes:
_pty_port: bool

Specifies if the object is used for communication with pseudo terminal (PTY) [protected]

_connected: bool

Specifies if the object has established a connection with a port [protected]

_serial_ports: List[ListPortInfo]

Holds available serial ports [protected]

_device: Optional[Serial]

Serial object [protected]

Methods:
list_serial_devices: List[str]

Updates the serial ports attribute, and returns a list of the available serial devices’ names [public]

connect_to_device: None

Connects to a serial port device; creates a Serial object instance and updates the connected attribute. Raises value exception if the requested device is not within the serial port list attribute [public]

disconnect_from_device: None

Disconnects from the serial device; closes the serial connection, deletes the Serial object, and updates the connected attribute to not connected [public]

is_connected: bool

Specifies if a serial connection is established or not [public]

write_to_device: None

Write to a serial device [public]

write_and_read_to_device: bytes

Write to a serial device and wait for a response [public]

Property:
device: Optional[Serial]

Returns the serial device to which connection is established. If no serial connection is established, the device has a None value

connect_to_device(device: str, *, serial_settings: SerialOptions = SerialOptions()) None

Connects to a serial port device; creates a Serial object instance and updates the connected attribute. Raises value exception if the requested device is not within the serial port list attribute.

Parameters:
  • device (str) – Device to be connected to

  • serial_settings (SerialOptions) – Serial connection settings

Returns:

None

Raises:

ValueError – if the requested device is not within the serial port list attribute

property device: Serial | None

Returns the serial device to which connection is established. If no serial connection is established, the device has a None value.

Returns:

Serial device to which a serial connection is established

Return type:

Optional[Serial]

disconnect_from_device() None

Disconnects from the serial device; closes the serial connection, deletes the Serial object, and updates the connected attribute to not connected.

Returns:

None

is_connected() bool

Specifies if a serial connection is established or not.

Returns:

Whether a serial connection is established or not

Return type:

bool

list_serial_devices() List[str]

Updates the serial ports attribute, and returns a list of the available serial devices’ names.

Returns:

List of available serial devices

Return type:

List[str]

write_and_read_to_device(message: bytes | bytearray) bytes

Write to a serial device and wait for a response.

Parameters:

message (Union[bytes, bytearray]) – Message to be written to the serial device as bytes or bytearray

Returns:

Response in bytes

Return type:

bytes

Raises:

AttributeError – if a serial device is not connected

write_to_device(message: bytes | bytearray) None

Write to a serial device.

Parameters:

message (Union[bytes, bytearray]) – Message to be written to the serial device as bytes or bytearray

Returns:

None

Raises:

AttributeError – if a serial device is not connected

class pycar.serial_handle.SerialOptions

Bases: object

A dataclass structure containing serial settings. The structure is frozen and values can not be changed at runtime; default values are set to the required for the KTY kit.

Attributes:
baud_rate: int

Bits per second; default: 115200

byte_size: int

Size of a byte; default: 8

stop_bits: int

Number of stop bits; default: 1

parity: str

Enable parity bit (options: ‘Y’ and ‘N’); default: ‘N’

enable_rts: bool

Enable request to transmit; default: True

enable_dsr: bool

Enable data set ready; default: False

enable_x: bool

Enable software control flow; default: False

timeout: Optional[float]

Set a read timeout value in seconds; default: None

write_timeout: Optional[float]

Set a write timeout value in seconds; default: None

inter_byte_timeout: Optional[float]

Set inter character timeout; default: None

baud_rate = 115200
byte_size = 8
enable_dsr = False
enable_rts = True
enable_x = False
inter_byte_timeout = None
parity = 'N'
stop_bits = 1
timeout = None
write_timeout = None

Module contents