Package PyMata :: Module pymata :: Class PyMata
[hide private]
[frames] | no frames]

Class PyMata

source code

This class contains the complete set of API methods that permit control of an Arduino Micro-Controller utilizing Firmata or its derivatives.

For information about the Firmata protocol, refer to: http://firmata.org/wiki/Protocol

Instance Methods [hide private]
 
__init__(self, port_id='/dev/ttyACM0', max_wait_time=30, program_if_needed=True, board_model='uno', firmata_type=('/home/abroun/dev/dawn_robotics/software/PyMata/PyMata/../Ard...)
The constructor tries to connect to an Arduino or Arduino compatible running Firmata.
source code
 
connect_to_firmata(self, port_id, max_wait_time)
This routine instantiates the entire interface.
source code
 
upload_firmata_sketch(self, port_id, board_model, firmata_type)
Uses the Ino library to upload Firmata to the Arduino attached to the given serial port
source code
 
analog_mapping_query(self)
Send an analog mapping query message via sysex.
source code
 
analog_read(self, pin)
Retrieve the last analog data value received for the specified pin.
source code
 
analog_write(self, pin, value)
Set the specified pin to the specified value.
source code
 
capability_query(self)
Send a Firmata capability query message via sysex.
source code
 
close(self)
This method will close the transport (serial port) and exit
source code
 
digital_read(self, pin)
Retrieve the last digital data value received for the specified pin.
source code
 
digital_write(self, pin, value)
Set the specified pin to the specified value.
source code
 
disable_analog_reporting(self, pin)
Disables analog reporting for a single analog pin.
source code
 
disable_digital_reporting(self, pin)
Disables digital reporting.
source code
 
enable_analog_reporting(self, pin)
Enables analog reporting.
source code
 
enable_digital_reporting(self, pin)
Enables digital reporting.
source code
 
encoder_config(self, pin_a, pin_b)
This command enables the rotary encoder (2 pin + ground) and will enable encoder reporting.
source code
 
extended_analog(self, pin, data)
This method will send an extended data analog output command to the selected pin
source code
 
get_analog_latch_data(self, pin)
A list is returned containing the latch state for the pin, the latched value, and the time stamp [pin_num, latch_state, latched_value, time_stamp] If the the latch state is LATCH_LATCHED, the table is reset (data and timestamp set to zero)
source code
 
get_analog_mapping_request_results(self)
Call this method after calling analog_mapping_query() to retrieve its results
source code
 
get_analog_response_table(self)
This method returns a list of lists representing the current pin mode and associated data values for all analog pins.
source code
 
get_capability_query_results(self)
Retrieve the data returned by a previous call to capability_query()
source code
 
get_digital_latch_data(self, pin)
A list is returned containing the latch state for the pin, the latched value, and the time stamp [pin_num, latch_state, latched_value, time_stamp] If the the latch state is LATCH_LATCHED, the table is reset (data and timestamp set to zero)
source code
 
get_digital_response_table(self)
This method returns a list of lists representing the current pin mode and associated data for all digital pins.
source code
 
get_firmata_version(self)
Retrieve the firmata version information returned by a previous call to refresh_report_version()
source code
 
get_firmata_firmware_version(self)
Retrieve the firmware id information returned by a previous call to refresh_report_firmware()
source code
 
get_pin_state_query_results(self)
This method returns the results of a previous call to pin_state_query() and then resets the pin state query data to None
source code
 
get_pymata_version(self)
Returns the PyMata version number in a list: [Major Number, Minor Number]
source code
 
get_sonar_data(self)
Retrieve Ping (HC-SR04 type) data.
source code
 
i2c_config(self, read_delay_time=0, pin_type=None, clk_pin=0, data_pin=0)
NOTE: THIS METHOD MUST BE CALLED BEFORE ANY I2C REQUEST IS MADE This method initializes Firmata for I2c operations.
source code
 
i2c_read(self, address, register, number_of_bytes, read_type)
This method requests the read of an i2c device.
source code
 
i2c_write(self, address, *args)
Write data to an i2c device.
source code
 
i2c_stop_reading(self, address)
This method stops an I2C_READ_CONTINUOUSLY operation for the i2c device address specified.
source code
 
i2c_get_read_data(self, address)
This method retrieves the i2c read data as the result of an i2c_read() command.
source code
 
pin_state_query(self, pin)
This method issues a pin state query command.
source code
 
play_tone(self, pin, tone_command, frequency, duration)
This method will call the Tone library for the selected pin.
source code
 
refresh_report_version(self)
This method will query firmata for the report version.
source code
 
refresh_report_firmware(self)
This method will query firmata to report firmware.
source code
 
reset(self, reset_sleep_time=2)
This command sends a reset message to the Arduino.
source code
 
set_analog_latch(self, pin, threshold_type, threshold_value)
This method "arms" an analog pin for its data to be latched and saved in the latching table
source code
 
set_digital_latch(self, pin, threshold_type)
This method "arms" a digital pin for its data to be latched and saved in the latching table
source code
 
set_pin_mode(self, pin, mode, pin_type)
This method sets a pin to the desired pin mode for the pin_type.
source code
 
set_sampling_interval(self, interval)
This method sends the desired sampling interval to Firmata.
source code
 
servo_config(self, pin, min_pulse=544, max_pulse=2400)
Configure a pin as a servo pin.
source code
 
sonar_config(self, trigger_pin, echo_pin, ping_interval=50)
Configure the pins, and ping interval for an HC-SR04 type device.
source code
Class Variables [hide private]
  HIGH = 1
  LOW = 0
  REPORTING_ENABLE = 1
  REPORTING_DISABLE = 0
  _command_deque = deque([])
  _command_handler = None
  _arduino = None
  _data_lock = threading.Lock()
  I2C_WRITE = 0
  I2C_READ = 8
  I2C_READ_CONTINUOUSLY = 16
  I2C_STOP_READING = 24
  I2C_READ_WRITE_MODE_MASK = 24
  TONE_TONE = 0
  TONE_NO_TONE = 1
  INPUT = None
  OUTPUT = None
  ANALOG = None
  PWM = None
  SERVO = None
  I2C = None
  TONE = None
  SONAR = None
  IGNORE = None
  ENCODER = None
  DIGITAL = None
  digital_output_port_pins = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0...
  STANDARD_FIRMATA = ('/home/abroun/dev/dawn_robotics/software/P...
  NOT_SO_STANDARD_FIRMATA = ('/home/abroun/dev/dawn_robotics/sof...
Method Details [hide private]

__init__(self, port_id='/dev/ttyACM0', max_wait_time=30, program_if_needed=True, board_model='uno', firmata_type=('/home/abroun/dev/dawn_robotics/software/PyMata/PyMata/../Ard...)
(Constructor)

source code 

The constructor tries to connect to an Arduino or Arduino compatible running Firmata. If Firmata is not found on the Arduino, then we can optionally try to program the Arduino with the Firmata sketch

Parameters:
  • port_id - Communications port specifier (COM3, /dev/ttyACM0, etc)
  • max_wait_time - Maximum time to wait when trying to connect to Firmata
  • program_if_needed - Attempt to upload the Firmata sketch if it's not found on the Arduino.
  • board_model - The type of Arduino being used. This is only needed if the board is to be programmed. Run 'ino list-models' at the command line for a list of available boards.
  • firmata_type - A directory containing the firmata sketch, and a list of supporting libs that are used when uploading firmata

connect_to_firmata(self, port_id, max_wait_time)

source code 

This routine instantiates the entire interface. It starts the operational threads for the serial interface as well as for the command handler.

Parameters:
  • port_id - Communications port specifier (COM3, /dev/ttyACM0, etc)
Returns:
True if the connection was successful and False otherwise

upload_firmata_sketch(self, port_id, board_model, firmata_type)

source code 

Uses the Ino library to upload Firmata to the Arduino attached to the given serial port

Parameters:
  • port_id - The serial port to use

analog_mapping_query(self)

source code 

Send an analog mapping query message via sysex. Client retrieves the results with a call to get_analog_mapping_request_results()

analog_read(self, pin)

source code 

Retrieve the last analog data value received for the specified pin.

Parameters:
  • pin - Selected pin
Returns:
The last value entered into the analog response table.

analog_write(self, pin, value)

source code 

Set the specified pin to the specified value.

Parameters:
  • pin - Pin number
  • value - Pin value
Returns:
No return value

capability_query(self)

source code 

Send a Firmata capability query message via sysex. Client retrieves the results with a call to get_capability_query_results() The Arduino can be rather slow in responding to this command. For the Mega 2560 R3 it has taken up to 25 seconds for a response.

close(self)

source code 

This method will close the transport (serial port) and exit

Returns:
No return value, but sys.exit(0) is called.

digital_read(self, pin)

source code 

Retrieve the last digital data value received for the specified pin. NOTE: This command will return values for digital, pwm, etc, pin types

Parameters:
  • pin - Selected pin
Returns:
The last value entered into the digital response table.

digital_write(self, pin, value)

source code 

Set the specified pin to the specified value.

Parameters:
  • pin - pin number
  • value - pin value
Returns:
No return value

disable_analog_reporting(self, pin)

source code 

Disables analog reporting for a single analog pin.

Parameters:
  • pin - Analog pin number. For example for A0, the number is 0.
Returns:
No return value

disable_digital_reporting(self, pin)

source code 

Disables digital reporting. By turning reporting off for this pin, reporting is disabled for all 8 bits in the "port" -

Parameters:
  • pin - Pin and all pins for this port
Returns:
No return value

enable_analog_reporting(self, pin)

source code 

Enables analog reporting. By turning reporting on for a single pin,

Parameters:
  • pin - Analog pin number. For example for A0, the number is 0.
Returns:
No return value

enable_digital_reporting(self, pin)

source code 

Enables digital reporting. By turning reporting on for all 8 bits in the "port" - this is part of Firmata's protocol specification.

Parameters:
  • pin - Pin and all pins for this port
Returns:
No return value

encoder_config(self, pin_a, pin_b)

source code 

This command enables the rotary encoder (2 pin + ground) and will enable encoder reporting.

NOTE: This command is not currently part of standard arduino firmata, but is provided for legacy support of CodeShield on an Arduino UNO.

Parameters:
  • pin_a - Encoder pin 1.
  • pin_b - Encoder pin 2.
Returns:
No return value

extended_analog(self, pin, data)

source code 

This method will send an extended data analog output command to the selected pin

Parameters:
  • pin - 0 - 127
  • data - 0 - 0xfffff

get_analog_latch_data(self, pin)

source code 

A list is returned containing the latch state for the pin, the latched value, and the time stamp [pin_num, latch_state, latched_value, time_stamp] If the the latch state is LATCH_LATCHED, the table is reset (data and timestamp set to zero)

Parameters:
  • pin - Pin number.
Returns:
[pin, latch_state, latch_data_value, time_stamp]

get_analog_mapping_request_results(self)

source code 

Call this method after calling analog_mapping_query() to retrieve its results

Returns:
raw data returned by firmata

get_analog_response_table(self)

source code 

This method returns a list of lists representing the current pin mode and associated data values for all analog pins. All configured pin types, both input and output will be listed. Output pin data will contain zero.

Returns:
The last update of the digital response table

get_capability_query_results(self)

source code 

Retrieve the data returned by a previous call to capability_query()

Returns:
Raw capability data returned by firmata

get_digital_latch_data(self, pin)

source code 

A list is returned containing the latch state for the pin, the latched value, and the time stamp [pin_num, latch_state, latched_value, time_stamp] If the the latch state is LATCH_LATCHED, the table is reset (data and timestamp set to zero)

Parameters:
  • pin - Pin number.
Returns:
[pin, latch_state, latch_data_value, time_stamp]

get_digital_response_table(self)

source code 

This method returns a list of lists representing the current pin mode and associated data for all digital pins. All pin types, both input and output will be listed. Output pin data will contain zero.

Returns:
The last update of the digital response table

get_firmata_version(self)

source code 

Retrieve the firmata version information returned by a previous call to refresh_report_version()

Returns:
Firmata_version list [major, minor] or None

get_firmata_firmware_version(self)

source code 

Retrieve the firmware id information returned by a previous call to refresh_report_firmware()

Returns:
Firmata_firmware list [major, minor, file_name] or None

get_pin_state_query_results(self)

source code 

This method returns the results of a previous call to pin_state_query() and then resets the pin state query data to None

Returns:
Raw pin state query data

get_pymata_version(self)

source code 

Returns the PyMata version number in a list: [Major Number, Minor Number]

get_sonar_data(self)

source code 

Retrieve Ping (HC-SR04 type) data. The data is presented as a dictionary. The 'key' is the trigger pin specified in sonar_config() and the 'data' is the current measured distance (in centimeters) for that pin. If there is no data, the value is set to IGNORE (127).

Returns:
active_sonar_map

i2c_config(self, read_delay_time=0, pin_type=None, clk_pin=0, data_pin=0)

source code 

NOTE: THIS METHOD MUST BE CALLED BEFORE ANY I2C REQUEST IS MADE This method initializes Firmata for I2c operations. It allows setting of a read time delay amount, and to optionally track the pins as I2C in the appropriate response table. To track pins: Set the pin_type to ANALOG or DIGITAL and provide the pin numbers. If using ANALOG, pin numbers use the analog number, for example A4: use 4.

Parameters:
  • read_delay_time - an optional parameter, default is 0
  • pin_type - ANALOG or DIGITAL to select response table type to track pin numbers
  • clk_pin - pin number (see comment above).
  • data_pin - pin number (see comment above).
Returns:
No Return Value

i2c_read(self, address, register, number_of_bytes, read_type)

source code 

This method requests the read of an i2c device. Results are retrieved by a call to i2c_get_read_data()

Parameters:
  • address - i2c device address
  • register - register number (can be set to zero)
  • number_of_bytes - number of bytes expected to be returned
  • read_type - I2C_READ or I2C_READ_CONTINUOUSLY

i2c_write(self, address, *args)

source code 

Write data to an i2c device.

Parameters:
  • address - i2c device address
  • args - A variable number of bytes to be sent to the device

i2c_stop_reading(self, address)

source code 

This method stops an I2C_READ_CONTINUOUSLY operation for the i2c device address specified.

Parameters:
  • address - address of i2c device

i2c_get_read_data(self, address)

source code 

This method retrieves the i2c read data as the result of an i2c_read() command.

Parameters:
  • address - i2c device address
Returns:
raw data read from device

pin_state_query(self, pin)

source code 

This method issues a pin state query command. Data returned is retrieved via a call to get_pin_state_query_results()

Parameters:
  • pin - pin number

play_tone(self, pin, tone_command, frequency, duration)

source code 

This method will call the Tone library for the selected pin. If the tone command is set to TONE_TONE, then the specified tone will be played. Else, if the tone command is TONE_NO_TONE, then any currently playing tone will be disabled. It is intended for a future release of Arduino Firmata

Parameters:
  • pin - Pin number
  • tone_command - Either TONE_TONE, or TONE_NO_TONE
  • frequency - Frequency of tone
  • duration - Duration of tone in milliseconds
Returns:
No return value

refresh_report_version(self)

source code 

This method will query firmata for the report version. Retrieve the report version via a call to get_firmata_version()

refresh_report_firmware(self)

source code 

This method will query firmata to report firmware. Retrieve the report via a call to get_firmata_firmware_version()

reset(self, reset_sleep_time=2)

source code 

This command sends a reset message to the Arduino. The response tables will be reinitialized

Returns:
No return value.

set_analog_latch(self, pin, threshold_type, threshold_value)

source code 

This method "arms" an analog pin for its data to be latched and saved in the latching table

Parameters:
  • pin - Analog pin number (value following an 'A' designator, i.e. A5 = 5
  • threshold_type - ANALOG_LATCH_GT | ANALOG_LATCH_LT | ANALOG_LATCH_GTE | ANALOG_LATCH_LTE
  • threshold_value - numerical value - between 0 and 1023
Returns:
True if successful, False if parameter data is invalid

set_digital_latch(self, pin, threshold_type)

source code 

This method "arms" a digital pin for its data to be latched and saved in the latching table

Parameters:
  • pin - Digital pin number
  • threshold_type - DIGITAL_LATCH_HIGH | DIGITAL_LATCH_LOW
Returns:
True if successful, False if parameter data is invalid

set_pin_mode(self, pin, mode, pin_type)

source code 

This method sets a pin to the desired pin mode for the pin_type. It automatically enables data reporting. NOTE: DO NOT CALL THIS METHOD FOR I2C. See i2c_config().

Parameters:
  • pin - Pin number (for analog use the analog number, for example A4: use 4)
  • mode - INPUT, OUTPUT, PWM, SERVO, ENCODER or TONE
  • pin_type - ANALOG or DIGITAL
Returns:
No return value

set_sampling_interval(self, interval)

source code 

This method sends the desired sampling interval to Firmata. Note: Standard Firmata will ignore any interval less than 10 milliseconds

Parameters:
  • interval - Integer value for desired sampling interval in milliseconds
Returns:
No return value.

servo_config(self, pin, min_pulse=544, max_pulse=2400)

source code 

Configure a pin as a servo pin. Set pulse min, max in ms.

Parameters:
  • pin - Servo Pin.
  • min_pulse - Min pulse width in ms.
  • max_pulse - Max pulse width in ms.
Returns:
No return value

sonar_config(self, trigger_pin, echo_pin, ping_interval=50)

source code 

Configure the pins, and ping interval for an HC-SR04 type device. Single pin configuration may be used. To do so, set both the trigger and echo pins to the same value. Up to a maximum of 6 SONAR devices is supported If the maximum is exceeded a message is sent to the console and the request is ignored. NOTE: data is measured in centimeters :param trigger_pin: The pin number of for the trigger (transmitter). :param echo_pin: The pin number for the received echo. :param ping_interval: Minimum interval between pings. Lowest number to use is 33 ms.Max is 127


Class Variable Details [hide private]

digital_output_port_pins

Value:
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

STANDARD_FIRMATA

Value:
('/home/abroun/dev/dawn_robotics/software/PyMata/PyMata/../ArduinoSket\
ch/Firmata/examples/StandardFirmata',
 ['/home/abroun/dev/dawn_robotics/software/PyMata/PyMata/../ArduinoSke\
tch/Firmata'])

NOT_SO_STANDARD_FIRMATA

Value:
('/home/abroun/dev/dawn_robotics/software/PyMata/PyMata/../ArduinoSket\
ch/NotSoStandardFirmata/examples/NotSoStandardFirmata',
 ['/home/abroun/dev/dawn_robotics/software/PyMata/PyMata/../ArduinoSke\
tch/AdaEncoder',
  '/home/abroun/dev/dawn_robotics/software/PyMata/PyMata/../ArduinoSke\
tch/ByteBuffer',
  '/home/abroun/dev/dawn_robotics/software/PyMata/PyMata/../ArduinoSke\
tch/cbiface',
...