# -*- coding: utf-8 -*- ############################################################# # This file was automatically generated on 2013-02-19. # # # # Bindings Version 2.0.4 # # # # If you have a bugfix for this file and want to commit it, # # please fix the bug in the generator. You can find a link # # to the generator git on tinkerforge.com # ############################################################# try: from collections import namedtuple except ImportError: try: from .ip_connection import namedtuple except ValueError: from ip_connection import namedtuple try: from .ip_connection import Device, IPConnection, Error except ValueError: from ip_connection import Device, IPConnection, Error GetProtocol1BrickletName = namedtuple('Protocol1BrickletName', ['protocol_version', 'firmware_version', 'name']) GetIdentity = namedtuple('Identity', ['uid', 'connected_uid', 'position', 'hardware_version', 'firmware_version', 'device_identifier']) class BrickDC(Device): """ Device for controlling DC motors """ DEVICE_IDENTIFIER = 11 CALLBACK_UNDER_VOLTAGE = 21 CALLBACK_EMERGENCY_SHUTDOWN = 22 CALLBACK_VELOCITY_REACHED = 23 CALLBACK_CURRENT_VELOCITY = 24 FUNCTION_SET_VELOCITY = 1 FUNCTION_GET_VELOCITY = 2 FUNCTION_GET_CURRENT_VELOCITY = 3 FUNCTION_SET_ACCELERATION = 4 FUNCTION_GET_ACCELERATION = 5 FUNCTION_SET_PWM_FREQUENCY = 6 FUNCTION_GET_PWM_FREQUENCY = 7 FUNCTION_FULL_BRAKE = 8 FUNCTION_GET_STACK_INPUT_VOLTAGE = 9 FUNCTION_GET_EXTERNAL_INPUT_VOLTAGE = 10 FUNCTION_GET_CURRENT_CONSUMPTION = 11 FUNCTION_ENABLE = 12 FUNCTION_DISABLE = 13 FUNCTION_IS_ENABLED = 14 FUNCTION_SET_MINIMUM_VOLTAGE = 15 FUNCTION_GET_MINIMUM_VOLTAGE = 16 FUNCTION_SET_DRIVE_MODE = 17 FUNCTION_GET_DRIVE_MODE = 18 FUNCTION_SET_CURRENT_VELOCITY_PERIOD = 19 FUNCTION_GET_CURRENT_VELOCITY_PERIOD = 20 FUNCTION_GET_PROTOCOL1_BRICKLET_NAME = 241 FUNCTION_GET_CHIP_TEMPERATURE = 242 FUNCTION_RESET = 243 FUNCTION_GET_IDENTITY = 255 DRIVE_MODE_DRIVE_BRAKE = 0 DRIVE_MODE_DRIVE_COAST = 1 def __init__(self, uid, ipcon): """ Creates an object with the unique device ID *uid* and adds it to the IP Connection *ipcon*. """ Device.__init__(self, uid, ipcon) self.api_version = (2, 0, 0) self.response_expected[BrickDC.FUNCTION_SET_VELOCITY] = BrickDC.RESPONSE_EXPECTED_FALSE self.response_expected[BrickDC.FUNCTION_GET_VELOCITY] = BrickDC.RESPONSE_EXPECTED_ALWAYS_TRUE self.response_expected[BrickDC.FUNCTION_GET_CURRENT_VELOCITY] = BrickDC.RESPONSE_EXPECTED_ALWAYS_TRUE self.response_expected[BrickDC.FUNCTION_SET_ACCELERATION] = BrickDC.RESPONSE_EXPECTED_FALSE self.response_expected[BrickDC.FUNCTION_GET_ACCELERATION] = BrickDC.RESPONSE_EXPECTED_ALWAYS_TRUE self.response_expected[BrickDC.FUNCTION_SET_PWM_FREQUENCY] = BrickDC.RESPONSE_EXPECTED_FALSE self.response_expected[BrickDC.FUNCTION_GET_PWM_FREQUENCY] = BrickDC.RESPONSE_EXPECTED_ALWAYS_TRUE self.response_expected[BrickDC.FUNCTION_FULL_BRAKE] = BrickDC.RESPONSE_EXPECTED_FALSE self.response_expected[BrickDC.FUNCTION_GET_STACK_INPUT_VOLTAGE] = BrickDC.RESPONSE_EXPECTED_ALWAYS_TRUE self.response_expected[BrickDC.FUNCTION_GET_EXTERNAL_INPUT_VOLTAGE] = BrickDC.RESPONSE_EXPECTED_ALWAYS_TRUE self.response_expected[BrickDC.FUNCTION_GET_CURRENT_CONSUMPTION] = BrickDC.RESPONSE_EXPECTED_ALWAYS_TRUE self.response_expected[BrickDC.FUNCTION_ENABLE] = BrickDC.RESPONSE_EXPECTED_FALSE self.response_expected[BrickDC.FUNCTION_DISABLE] = BrickDC.RESPONSE_EXPECTED_FALSE self.response_expected[BrickDC.FUNCTION_IS_ENABLED] = BrickDC.RESPONSE_EXPECTED_ALWAYS_TRUE self.response_expected[BrickDC.FUNCTION_SET_MINIMUM_VOLTAGE] = BrickDC.RESPONSE_EXPECTED_TRUE self.response_expected[BrickDC.FUNCTION_GET_MINIMUM_VOLTAGE] = BrickDC.RESPONSE_EXPECTED_ALWAYS_TRUE self.response_expected[BrickDC.FUNCTION_SET_DRIVE_MODE] = BrickDC.RESPONSE_EXPECTED_FALSE self.response_expected[BrickDC.FUNCTION_GET_DRIVE_MODE] = BrickDC.RESPONSE_EXPECTED_ALWAYS_TRUE self.response_expected[BrickDC.FUNCTION_SET_CURRENT_VELOCITY_PERIOD] = BrickDC.RESPONSE_EXPECTED_TRUE self.response_expected[BrickDC.FUNCTION_GET_CURRENT_VELOCITY_PERIOD] = BrickDC.RESPONSE_EXPECTED_ALWAYS_TRUE self.response_expected[BrickDC.CALLBACK_UNDER_VOLTAGE] = BrickDC.RESPONSE_EXPECTED_ALWAYS_FALSE self.response_expected[BrickDC.CALLBACK_EMERGENCY_SHUTDOWN] = BrickDC.RESPONSE_EXPECTED_ALWAYS_FALSE self.response_expected[BrickDC.CALLBACK_VELOCITY_REACHED] = BrickDC.RESPONSE_EXPECTED_ALWAYS_FALSE self.response_expected[BrickDC.CALLBACK_CURRENT_VELOCITY] = BrickDC.RESPONSE_EXPECTED_ALWAYS_FALSE self.response_expected[BrickDC.FUNCTION_GET_PROTOCOL1_BRICKLET_NAME] = BrickDC.RESPONSE_EXPECTED_ALWAYS_TRUE self.response_expected[BrickDC.FUNCTION_GET_CHIP_TEMPERATURE] = BrickDC.RESPONSE_EXPECTED_ALWAYS_TRUE self.response_expected[BrickDC.FUNCTION_RESET] = BrickDC.RESPONSE_EXPECTED_FALSE self.response_expected[BrickDC.FUNCTION_GET_IDENTITY] = BrickDC.RESPONSE_EXPECTED_ALWAYS_TRUE self.callback_formats[BrickDC.CALLBACK_UNDER_VOLTAGE] = 'H' self.callback_formats[BrickDC.CALLBACK_EMERGENCY_SHUTDOWN] = '' self.callback_formats[BrickDC.CALLBACK_VELOCITY_REACHED] = 'h' self.callback_formats[BrickDC.CALLBACK_CURRENT_VELOCITY] = 'h' def set_velocity(self, velocity): """ Sets the velocity of the motor. Whereas -32767 is full speed backward, 0 is stop and 32767 is full speed forward. Depending on the acceleration (see :func:`SetAcceleration`), the motor is not immediately brought to the velocity but smoothly accelerated. The velocity describes the duty cycle of the PWM with which the motor is controlled, e.g. a velocity of 3277 sets a PWM with a 10% duty cycle. You can not only control the duty cycle of the PWM but also the frequency, see :func:`SetPWMFrequency`. The default velocity is 0. """ self.ipcon.send_request(self, BrickDC.FUNCTION_SET_VELOCITY, (velocity,), 'h', '') def get_velocity(self): """ Returns the velocity as set by :func:`SetVelocity`. """ return self.ipcon.send_request(self, BrickDC.FUNCTION_GET_VELOCITY, (), '', 'h') def get_current_velocity(self): """ Returns the *current* velocity of the motor. This value is different from :func:`GetVelocity` whenever the motor is currently accelerating to a goal set by :func:`SetVelocity`. """ return self.ipcon.send_request(self, BrickDC.FUNCTION_GET_CURRENT_VELOCITY, (), '', 'h') def set_acceleration(self, acceleration): """ Sets the acceleration of the motor. It is given in *velocity/s*. An acceleration of 10000 means, that every second the velocity is increased by 10000 (or about 30% duty cycle). For example: If the current velocity is 0 and you want to accelerate to a velocity of 16000 (about 50% duty cycle) in 10 seconds, you should set an acceleration of 1600. If acceleration is set to 0, there is no speed ramping, i.e. a new velocity is immediately given to the motor. The default acceleration is 10000. """ self.ipcon.send_request(self, BrickDC.FUNCTION_SET_ACCELERATION, (acceleration,), 'H', '') def get_acceleration(self): """ Returns the acceleration as set by :func:`SetAcceleration`. """ return self.ipcon.send_request(self, BrickDC.FUNCTION_GET_ACCELERATION, (), '', 'H') def set_pwm_frequency(self, frequency): """ Sets the frequency (in Hz) of the PWM with which the motor is driven. The possible range of the frequency is 1-20000Hz. Often a high frequency is less noisy and the motor runs smoother. However, with a low frequency there are less switches and therefore fewer switching losses. Also with most motors lower frequencies enable higher torque. If you have no idea what all this means, just ignore this function and use the default frequency, it will very likely work fine. The default frequency is 15 kHz. """ self.ipcon.send_request(self, BrickDC.FUNCTION_SET_PWM_FREQUENCY, (frequency,), 'H', '') def get_pwm_frequency(self): """ Returns the PWM frequency (in Hz) as set by :func:`SetPWMFrequency`. """ return self.ipcon.send_request(self, BrickDC.FUNCTION_GET_PWM_FREQUENCY, (), '', 'H') def full_brake(self): """ Executes an active full brake. .. warning:: This function is for emergency purposes, where an immediate brake is necessary. Depending on the current velocity and the strength of the motor, a full brake can be quite violent. Call :func:`SetVelocity` with 0 if you just want to stop the motor. """ self.ipcon.send_request(self, BrickDC.FUNCTION_FULL_BRAKE, (), '', '') def get_stack_input_voltage(self): """ Returns the stack input voltage in mV. The stack input voltage is the voltage that is supplied via the stack, i.e. it is given by a Step-Down or Step-Up Power Supply. """ return self.ipcon.send_request(self, BrickDC.FUNCTION_GET_STACK_INPUT_VOLTAGE, (), '', 'H') def get_external_input_voltage(self): """ Returns the external input voltage in mV. The external input voltage is given via the black power input connector on the DC Brick. If there is an external input voltage and a stack input voltage, the motor will be driven by the external input voltage. If there is only a stack voltage present, the motor will be driven by this voltage. .. warning:: This means, if you have a high stack voltage and a low external voltage, the motor will be driven with the low external voltage. If you then remove the external connection, it will immediately be driven by the high stack voltage. """ return self.ipcon.send_request(self, BrickDC.FUNCTION_GET_EXTERNAL_INPUT_VOLTAGE, (), '', 'H') def get_current_consumption(self): """ Returns the current consumption of the motor in mA. """ return self.ipcon.send_request(self, BrickDC.FUNCTION_GET_CURRENT_CONSUMPTION, (), '', 'H') def enable(self): """ Enables the driver chip. The driver parameters can be configured (velocity, acceleration, etc) before it is enabled. """ self.ipcon.send_request(self, BrickDC.FUNCTION_ENABLE, (), '', '') def disable(self): """ Disables the driver chip. The configurations are kept (velocity, acceleration, etc) but the motor is not driven until it is enabled again. """ self.ipcon.send_request(self, BrickDC.FUNCTION_DISABLE, (), '', '') def is_enabled(self): """ Returns *true* if the driver chip is enabled, *false* otherwise. """ return self.ipcon.send_request(self, BrickDC.FUNCTION_IS_ENABLED, (), '', '?') def set_minimum_voltage(self, voltage): """ Sets the minimum voltage in mV, below which the :func:`UnderVoltage` callback is triggered. The minimum possible value that works with the DC Brick is 5V. You can use this function to detect the discharge of a battery that is used to drive the motor. If you have a fixed power supply, you likely do not need this functionality. The default value is 5V. """ self.ipcon.send_request(self, BrickDC.FUNCTION_SET_MINIMUM_VOLTAGE, (voltage,), 'H', '') def get_minimum_voltage(self): """ Returns the minimum voltage as set by :func:`SetMinimumVoltage` """ return self.ipcon.send_request(self, BrickDC.FUNCTION_GET_MINIMUM_VOLTAGE, (), '', 'H') def set_drive_mode(self, mode): """ Sets the drive mode. Possible modes are: * 0 = Drive/Brake * 1 = Drive/Coast These modes are different kinds of motor controls. In Drive/Brake mode, the motor is always either driving or braking. There is no freewheeling. Advantages are: A more linear correlation between PWM and velocity, more exact accelerations and the possibility to drive with slower velocities. In Drive/Coast mode, the motor is always either driving or freewheeling. Advantages are: Less current consumption and less demands on the motor and driver chip. The default value is 0 = Drive/Brake. """ self.ipcon.send_request(self, BrickDC.FUNCTION_SET_DRIVE_MODE, (mode,), 'B', '') def get_drive_mode(self): """ Returns the drive mode, as set by :func:`SetDriveMode`. """ return self.ipcon.send_request(self, BrickDC.FUNCTION_GET_DRIVE_MODE, (), '', 'B') def set_current_velocity_period(self, period): """ Sets a period in ms with which the :func:`CurrentVelocity` callback is triggered. A period of 0 turns the callback off. The default value is 0. """ self.ipcon.send_request(self, BrickDC.FUNCTION_SET_CURRENT_VELOCITY_PERIOD, (period,), 'H', '') def get_current_velocity_period(self): """ Returns the period as set by :func:`SetCurrentVelocityPeriod`. """ return self.ipcon.send_request(self, BrickDC.FUNCTION_GET_CURRENT_VELOCITY_PERIOD, (), '', 'H') def get_protocol1_bricklet_name(self, port): """ Returns the firmware and protocol version and the name of the Bricklet for a given port. This functions sole purpose is to allow automatic flashing of v1.x.y Bricklet plugins. .. versionadded:: 2.0.0~(Firmware) """ return GetProtocol1BrickletName(*self.ipcon.send_request(self, BrickDC.FUNCTION_GET_PROTOCOL1_BRICKLET_NAME, (port,), 'c', 'B 3B 40s')) def get_chip_temperature(self): """ Returns the temperature in °C/10 as measured inside the microcontroller. The value returned is not the ambient temperature! The temperature is only proportional to the real temperature and it has an accuracy of +-15%. Practically it is only useful as an indicator for temperature changes. .. versionadded:: 1.1.3~(Firmware) """ return self.ipcon.send_request(self, BrickDC.FUNCTION_GET_CHIP_TEMPERATURE, (), '', 'h') def reset(self): """ Calling this function will reset the Brick. Calling this function on a Brick inside of a stack will reset the whole stack. After a reset you have to create new device objects, calling functions on the existing ones will result in undefined behavior! .. versionadded:: 1.1.3~(Firmware) """ self.ipcon.send_request(self, BrickDC.FUNCTION_RESET, (), '', '') def get_identity(self): """ Returns the UID, the UID where the Brick is connected to, the position, the hardware and firmware version as well as the device identifier. The position can be '0'-'8' (stack position). The device identifiers can be found :ref:`here `. .. versionadded:: 2.0.0~(Firmware) """ return GetIdentity(*self.ipcon.send_request(self, BrickDC.FUNCTION_GET_IDENTITY, (), '', '8s 8s c 3B 3B H')) def register_callback(self, id, callback): """ Registers a callback with ID *id* to the function *callback*. """ self.registered_callbacks[id] = callback DC = BrickDC # for backward compatibility