class Tinkerforge::BrickletDCV2

Drives one brushed DC motor with up to 28V and 5A (peak)

Constants

CALLBACK_CURRENT_VELOCITY

This callback is triggered with the period that is set by BrickletDCV2#set_current_velocity_callback_configuration. The parameter is the current velocity used by the motor.

The CALLBACK_CURRENT_VELOCITY callback is only triggered after the set period if there is a change in the velocity.

CALLBACK_EMERGENCY_SHUTDOWN

This callback is triggered if either the current consumption is too high (above 5A) or the temperature of the driver chip is too high (above 175°C). These two possibilities are essentially the same, since the temperature will reach this threshold immediately if the motor consumes too much current. In case of a voltage below 3.3V (external or stack) this callback is triggered as well.

If this callback is triggered, the driver chip gets disabled at the same time. That means, BrickletDCV2#set_enabled has to be called to drive the motor again.

.. note

This callback only works in Drive/Brake mode (see BrickletDCV2#set_drive_mode). In Drive/Coast mode it is unfortunately impossible to reliably read the overcurrent/overtemperature signal from the driver chip.

CALLBACK_VELOCITY_REACHED

This callback is triggered whenever a set velocity is reached. For example: If a velocity of 0 is present, acceleration is set to 5000 and velocity to 10000, the CALLBACK_VELOCITY_REACHED callback will be triggered after about 2 seconds, when the set velocity is actually reached.

.. note

Since we can't get any feedback from the DC motor, this only works if the acceleration (see BrickletDCV2#set_motion) is set smaller or equal to the maximum acceleration of the motor. Otherwise the motor will lag behind the control value and the callback will be triggered too early.

Public Class Methods

new(uid, ipcon) click to toggle source

Creates an object with the unique device ID uid and adds it to the IP Connection ipcon.

Calls superclass method
# File lib/tinkerforge/bricklet_dc_v2.rb, line 113
def initialize(uid, ipcon)
  super uid, ipcon, DEVICE_IDENTIFIER, DEVICE_DISPLAY_NAME

  @api_version = [2, 0, 0]

  @response_expected[FUNCTION_SET_ENABLED] = RESPONSE_EXPECTED_FALSE
  @response_expected[FUNCTION_GET_ENABLED] = RESPONSE_EXPECTED_ALWAYS_TRUE
  @response_expected[FUNCTION_SET_VELOCITY] = RESPONSE_EXPECTED_FALSE
  @response_expected[FUNCTION_GET_VELOCITY] = RESPONSE_EXPECTED_ALWAYS_TRUE
  @response_expected[FUNCTION_GET_CURRENT_VELOCITY] = RESPONSE_EXPECTED_ALWAYS_TRUE
  @response_expected[FUNCTION_SET_MOTION] = RESPONSE_EXPECTED_FALSE
  @response_expected[FUNCTION_GET_MOTION] = RESPONSE_EXPECTED_ALWAYS_TRUE
  @response_expected[FUNCTION_FULL_BRAKE] = RESPONSE_EXPECTED_FALSE
  @response_expected[FUNCTION_SET_DRIVE_MODE] = RESPONSE_EXPECTED_FALSE
  @response_expected[FUNCTION_GET_DRIVE_MODE] = RESPONSE_EXPECTED_ALWAYS_TRUE
  @response_expected[FUNCTION_SET_PWM_FREQUENCY] = RESPONSE_EXPECTED_FALSE
  @response_expected[FUNCTION_GET_PWM_FREQUENCY] = RESPONSE_EXPECTED_ALWAYS_TRUE
  @response_expected[FUNCTION_GET_POWER_STATISTICS] = RESPONSE_EXPECTED_ALWAYS_TRUE
  @response_expected[FUNCTION_SET_ERROR_LED_CONFIG] = RESPONSE_EXPECTED_FALSE
  @response_expected[FUNCTION_GET_ERROR_LED_CONFIG] = RESPONSE_EXPECTED_ALWAYS_TRUE
  @response_expected[FUNCTION_SET_EMERGENCY_SHUTDOWN_CALLBACK_CONFIGURATION] = RESPONSE_EXPECTED_TRUE
  @response_expected[FUNCTION_GET_EMERGENCY_SHUTDOWN_CALLBACK_CONFIGURATION] = RESPONSE_EXPECTED_ALWAYS_TRUE
  @response_expected[FUNCTION_SET_VELOCITY_REACHED_CALLBACK_CONFIGURATION] = RESPONSE_EXPECTED_TRUE
  @response_expected[FUNCTION_GET_VELOCITY_REACHED_CALLBACK_CONFIGURATION] = RESPONSE_EXPECTED_ALWAYS_TRUE
  @response_expected[FUNCTION_SET_CURRENT_VELOCITY_CALLBACK_CONFIGURATION] = RESPONSE_EXPECTED_TRUE
  @response_expected[FUNCTION_GET_CURRENT_VELOCITY_CALLBACK_CONFIGURATION] = RESPONSE_EXPECTED_ALWAYS_TRUE
  @response_expected[FUNCTION_GET_SPITFP_ERROR_COUNT] = RESPONSE_EXPECTED_ALWAYS_TRUE
  @response_expected[FUNCTION_SET_BOOTLOADER_MODE] = RESPONSE_EXPECTED_ALWAYS_TRUE
  @response_expected[FUNCTION_GET_BOOTLOADER_MODE] = RESPONSE_EXPECTED_ALWAYS_TRUE
  @response_expected[FUNCTION_SET_WRITE_FIRMWARE_POINTER] = RESPONSE_EXPECTED_FALSE
  @response_expected[FUNCTION_WRITE_FIRMWARE] = RESPONSE_EXPECTED_ALWAYS_TRUE
  @response_expected[FUNCTION_SET_STATUS_LED_CONFIG] = RESPONSE_EXPECTED_FALSE
  @response_expected[FUNCTION_GET_STATUS_LED_CONFIG] = RESPONSE_EXPECTED_ALWAYS_TRUE
  @response_expected[FUNCTION_GET_CHIP_TEMPERATURE] = RESPONSE_EXPECTED_ALWAYS_TRUE
  @response_expected[FUNCTION_RESET] = RESPONSE_EXPECTED_FALSE
  @response_expected[FUNCTION_WRITE_UID] = RESPONSE_EXPECTED_FALSE
  @response_expected[FUNCTION_READ_UID] = RESPONSE_EXPECTED_ALWAYS_TRUE
  @response_expected[FUNCTION_GET_IDENTITY] = RESPONSE_EXPECTED_ALWAYS_TRUE

  @callback_formats[CALLBACK_EMERGENCY_SHUTDOWN] = [8, '']
  @callback_formats[CALLBACK_VELOCITY_REACHED] = [10, 's']
  @callback_formats[CALLBACK_CURRENT_VELOCITY] = [10, 's']

  @ipcon.add_device self
end

Public Instance Methods

full_brake() click to toggle source

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 BrickletDCV2#set_velocity with 0 if you just want to stop the motor.

# File lib/tinkerforge/bricklet_dc_v2.rb, line 236
def full_brake
  check_validity

  send_request FUNCTION_FULL_BRAKE, [], '', 8, ''
end
get_bootloader_mode() click to toggle source

Returns the current bootloader mode, see BrickletDCV2#set_bootloader_mode.

# File lib/tinkerforge/bricklet_dc_v2.rb, line 405
def get_bootloader_mode
  check_validity

  send_request FUNCTION_GET_BOOTLOADER_MODE, [], '', 9, 'C'
end
get_chip_temperature() click to toggle source

Returns the temperature 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 bad accuracy. Practically it is only useful as an indicator for temperature changes.

# File lib/tinkerforge/bricklet_dc_v2.rb, line 463
def get_chip_temperature
  check_validity

  send_request FUNCTION_GET_CHIP_TEMPERATURE, [], '', 10, 's'
end
get_current_velocity() click to toggle source

Returns the current velocity of the motor. This value is different from BrickletDCV2#get_velocity whenever the motor is currently accelerating to a goal set by BrickletDCV2#set_velocity.

# File lib/tinkerforge/bricklet_dc_v2.rb, line 199
def get_current_velocity
  check_validity

  send_request FUNCTION_GET_CURRENT_VELOCITY, [], '', 10, 's'
end
get_current_velocity_callback_configuration() click to toggle source

Returns the callback configuration as set by BrickletDCV2#set_current_velocity_callback_configuration.

# File lib/tinkerforge/bricklet_dc_v2.rb, line 366
def get_current_velocity_callback_configuration
  check_validity

  send_request FUNCTION_GET_CURRENT_VELOCITY_CALLBACK_CONFIGURATION, [], '', 13, 'L ?'
end
get_drive_mode() click to toggle source

Returns the drive mode, as set by BrickletDCV2#set_drive_mode.

# File lib/tinkerforge/bricklet_dc_v2.rb, line 264
def get_drive_mode
  check_validity

  send_request FUNCTION_GET_DRIVE_MODE, [], '', 9, 'C'
end
get_emergency_shutdown_callback_configuration() click to toggle source

Returns the callback configuration as set by BrickletDCV2#set_emergency_shutdown_callback_configuration.

# File lib/tinkerforge/bricklet_dc_v2.rb, line 328
def get_emergency_shutdown_callback_configuration
  check_validity

  send_request FUNCTION_GET_EMERGENCY_SHUTDOWN_CALLBACK_CONFIGURATION, [], '', 9, '?'
end
get_enabled() click to toggle source

Returns true if the driver chip is enabled, false otherwise.

# File lib/tinkerforge/bricklet_dc_v2.rb, line 168
def get_enabled
  check_validity

  send_request FUNCTION_GET_ENABLED, [], '', 9, '?'
end
get_error_led_config() click to toggle source

Returns the LED configuration as set by BrickletDCV2#set_error_led_config

# File lib/tinkerforge/bricklet_dc_v2.rb, line 313
def get_error_led_config
  check_validity

  send_request FUNCTION_GET_ERROR_LED_CONFIG, [], '', 9, 'C'
end
get_identity() click to toggle source

Returns the UID, the UID where the Bricklet is connected to, the position, the hardware and firmware version as well as the device identifier.

The position can be 'a', 'b', 'c', 'd', 'e', 'f', 'g' or 'h' (Bricklet Port). A Bricklet connected to an :ref:`Isolator Bricklet <isolator_bricklet>` is always at position 'z'.

The device identifier numbers can be found :ref:`here <device_identifier>`. |device_identifier_constant|

# File lib/tinkerforge/bricklet_dc_v2.rb, line 510
def get_identity
  send_request FUNCTION_GET_IDENTITY, [], '', 33, 'Z8 Z8 k C3 C3 S'
end
get_motion() click to toggle source

Returns the acceleration/deceleration as set by BrickletDCV2#set_motion.

# File lib/tinkerforge/bricklet_dc_v2.rb, line 222
def get_motion
  check_validity

  send_request FUNCTION_GET_MOTION, [], '', 12, 'S S'
end
get_power_statistics() click to toggle source

Returns input voltage and current usage of the driver.

# File lib/tinkerforge/bricklet_dc_v2.rb, line 292
def get_power_statistics
  check_validity

  send_request FUNCTION_GET_POWER_STATISTICS, [], '', 12, 'S S'
end
get_pwm_frequency() click to toggle source

Returns the PWM frequency as set by BrickletDCV2#set_pwm_frequency.

# File lib/tinkerforge/bricklet_dc_v2.rb, line 285
def get_pwm_frequency
  check_validity

  send_request FUNCTION_GET_PWM_FREQUENCY, [], '', 10, 'S'
end
get_spitfp_error_count() click to toggle source

Returns the error count for the communication between Brick and Bricklet.

The errors are divided into

  • ACK checksum errors,

  • message checksum errors,

  • framing errors and

  • overflow errors.

The errors counts are for errors that occur on the Bricklet side. All Bricks have a similar function that returns the errors on the Brick side.

# File lib/tinkerforge/bricklet_dc_v2.rb, line 383
def get_spitfp_error_count
  check_validity

  send_request FUNCTION_GET_SPITFP_ERROR_COUNT, [], '', 24, 'L L L L'
end
get_status_led_config() click to toggle source

Returns the configuration as set by BrickletDCV2#set_status_led_config

# File lib/tinkerforge/bricklet_dc_v2.rb, line 451
def get_status_led_config
  check_validity

  send_request FUNCTION_GET_STATUS_LED_CONFIG, [], '', 9, 'C'
end
get_velocity() click to toggle source

Returns the velocity as set by BrickletDCV2#set_velocity.

# File lib/tinkerforge/bricklet_dc_v2.rb, line 190
def get_velocity
  check_validity

  send_request FUNCTION_GET_VELOCITY, [], '', 10, 's'
end
get_velocity_reached_callback_configuration() click to toggle source

Returns the callback configuration as set by BrickletDCV2#set_velocity_reached_callback_configuration.

# File lib/tinkerforge/bricklet_dc_v2.rb, line 343
def get_velocity_reached_callback_configuration
  check_validity

  send_request FUNCTION_GET_VELOCITY_REACHED_CALLBACK_CONFIGURATION, [], '', 9, '?'
end
read_uid() click to toggle source

Returns the current UID as an integer. Encode as Base58 to get the usual string version.

# File lib/tinkerforge/bricklet_dc_v2.rb, line 494
def read_uid
  check_validity

  send_request FUNCTION_READ_UID, [], '', 12, 'L'
end
register_callback(id, &block) click to toggle source

Registers a callback with ID id to the block block.

# File lib/tinkerforge/bricklet_dc_v2.rb, line 515
def register_callback(id, &block)
  callback = block
  @registered_callbacks[id] = callback
end
reset() click to toggle source

Calling this function will reset the Bricklet. All configurations will be lost.

After a reset you have to create new device objects, calling functions on the existing ones will result in undefined behavior!

# File lib/tinkerforge/bricklet_dc_v2.rb, line 475
def reset
  check_validity

  send_request FUNCTION_RESET, [], '', 8, ''
end
set_bootloader_mode(mode) click to toggle source

Sets the bootloader mode and returns the status after the requested mode change was instigated.

You can change from bootloader mode to firmware mode and vice versa. A change from bootloader mode to firmware mode will only take place if the entry function, device identifier and CRC are present and correct.

This function is used by Brick Viewer during flashing. It should not be necessary to call it in a normal user program.

# File lib/tinkerforge/bricklet_dc_v2.rb, line 398
def set_bootloader_mode(mode)
  check_validity

  send_request FUNCTION_SET_BOOTLOADER_MODE, [mode], 'C', 9, 'C'
end
set_current_velocity_callback_configuration(period, value_has_to_change) click to toggle source

The period is the period with which the CALLBACK_CURRENT_VELOCITY callback is triggered periodically. A value of 0 turns the callback off.

If the `value has to change`-parameter is set to true, the callback is only triggered after the value has changed. If the value didn't change within the period, the callback is triggered immediately on change.

If it is set to false, the callback is continuously triggered with the period, independent of the value.

# File lib/tinkerforge/bricklet_dc_v2.rb, line 358
def set_current_velocity_callback_configuration(period, value_has_to_change)
  check_validity

  send_request FUNCTION_SET_CURRENT_VELOCITY_CALLBACK_CONFIGURATION, [period, value_has_to_change], 'L ?', 8, ''
end
set_drive_mode(mode) click to toggle source

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.

# File lib/tinkerforge/bricklet_dc_v2.rb, line 257
def set_drive_mode(mode)
  check_validity

  send_request FUNCTION_SET_DRIVE_MODE, [mode], 'C', 8, ''
end
set_emergency_shutdown_callback_configuration(enabled) click to toggle source

Enable/Disable CALLBACK_EMERGENCY_SHUTDOWN callback.

# File lib/tinkerforge/bricklet_dc_v2.rb, line 320
def set_emergency_shutdown_callback_configuration(enabled)
  check_validity

  send_request FUNCTION_SET_EMERGENCY_SHUTDOWN_CALLBACK_CONFIGURATION, [enabled], '?', 8, ''
end
set_enabled(enabled) click to toggle source

Enables/Disables the driver chip. The driver parameters can be configured (velocity, acceleration, etc) before it is enabled.

# File lib/tinkerforge/bricklet_dc_v2.rb, line 161
def set_enabled(enabled)
  check_validity

  send_request FUNCTION_SET_ENABLED, [enabled], '?', 8, ''
end
set_error_led_config(config) click to toggle source

Configures the error LED to be either turned off, turned on, blink in heartbeat mode or show an error.

If the LED is configured to show errors it has three different states:

  • Off: No error present.

  • 1s interval blinking: Input voltage too low (below 6V).

  • 250ms interval blinking: Overtemperature or overcurrent.

# File lib/tinkerforge/bricklet_dc_v2.rb, line 306
def set_error_led_config(config)
  check_validity

  send_request FUNCTION_SET_ERROR_LED_CONFIG, [config], 'C', 8, ''
end
set_motion(acceleration, deceleration) click to toggle source

Sets the acceleration and deceleration 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 and deceleration is set to 0, there is no speed ramping, i.e. a new velocity is immediately given to the motor.

# File lib/tinkerforge/bricklet_dc_v2.rb, line 215
def set_motion(acceleration, deceleration)
  check_validity

  send_request FUNCTION_SET_MOTION, [acceleration, deceleration], 'S S', 8, ''
end
set_pwm_frequency(frequency) click to toggle source

Sets the frequency of the PWM with which the motor is driven. 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.

# File lib/tinkerforge/bricklet_dc_v2.rb, line 278
def set_pwm_frequency(frequency)
  check_validity

  send_request FUNCTION_SET_PWM_FREQUENCY, [frequency], 'S', 8, ''
end
set_status_led_config(config) click to toggle source

Sets the status LED configuration. By default the LED shows communication traffic between Brick and Bricklet, it flickers once for every 10 received data packets.

You can also turn the LED permanently on/off or show a heartbeat.

If the Bricklet is in bootloader mode, the LED is will show heartbeat by default.

# File lib/tinkerforge/bricklet_dc_v2.rb, line 444
def set_status_led_config(config)
  check_validity

  send_request FUNCTION_SET_STATUS_LED_CONFIG, [config], 'C', 8, ''
end
set_velocity(velocity) click to toggle source

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 BrickletDCV2#set_motion), 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 BrickletDCV2#set_pwm_frequency.

# File lib/tinkerforge/bricklet_dc_v2.rb, line 183
def set_velocity(velocity)
  check_validity

  send_request FUNCTION_SET_VELOCITY, [velocity], 's', 8, ''
end
set_velocity_reached_callback_configuration(enabled) click to toggle source

Enable/Disable CALLBACK_VELOCITY_REACHED callback.

# File lib/tinkerforge/bricklet_dc_v2.rb, line 335
def set_velocity_reached_callback_configuration(enabled)
  check_validity

  send_request FUNCTION_SET_VELOCITY_REACHED_CALLBACK_CONFIGURATION, [enabled], '?', 8, ''
end
set_write_firmware_pointer(pointer) click to toggle source

Sets the firmware pointer for BrickletDCV2#write_firmware. The pointer has to be increased by chunks of size 64. The data is written to flash every 4 chunks (which equals to one page of size 256).

This function is used by Brick Viewer during flashing. It should not be necessary to call it in a normal user program.

# File lib/tinkerforge/bricklet_dc_v2.rb, line 417
def set_write_firmware_pointer(pointer)
  check_validity

  send_request FUNCTION_SET_WRITE_FIRMWARE_POINTER, [pointer], 'L', 8, ''
end
write_firmware(data) click to toggle source

Writes 64 Bytes of firmware at the position as written by BrickletDCV2#set_write_firmware_pointer before. The firmware is written to flash every 4 chunks.

You can only write firmware in bootloader mode.

This function is used by Brick Viewer during flashing. It should not be necessary to call it in a normal user program.

# File lib/tinkerforge/bricklet_dc_v2.rb, line 431
def write_firmware(data)
  check_validity

  send_request FUNCTION_WRITE_FIRMWARE, [data], 'C64', 9, 'C'
end
write_uid(uid) click to toggle source

Writes a new UID into flash. If you want to set a new UID you have to decode the Base58 encoded UID string into an integer first.

We recommend that you use Brick Viewer to change the UID.

# File lib/tinkerforge/bricklet_dc_v2.rb, line 486
def write_uid(uid)
  check_validity

  send_request FUNCTION_WRITE_UID, [uid], 'L', 8, ''
end