Skip to content

IO Control

Tom Tzook edited this page Nov 13, 2020 · 5 revisions

Input/Output devices are electronic components connected to the robot which can be used to move parts of the robot or to get information from the environment. Devices like sensors and motor controllers conform to this category.

Ports API

Ports

The flashlib.core.io provides an API to access and control different IO ports which are available on the robot. Those ports can be used to interface with different IO devices. For example we can use a digital input port to read information from a switch or a button.

There are several types of ports available. Each is represented by a different interface and provides different functionality. Using those interfaces, we can implement communication with different devices.

Acquiring a Port

Each port is represented as an instance of a different class (depending on the type and implementation). Acquiring a port is done via the IoInterface component which is provided from RobotControl and is universally accessible via RunningRobot.getControl().getIoInterface().

Each port is identified by an instance of IoChannel, which serve as identifiers for the port, and are unique for each type, i.e. usage of the same IoChannel to create two ports of the same type will resolve to the same physical port. However, whether or not the channels are unique across different types depends on the implementation and platform used.

IoInterface exports different methods, each can create a port of a different type. For example, creating a Pwm port:

IoChannel channel = ...;
IoInterface ioInterface = RunningRobot.getControl().getIoInterface();

Pwm pwm = ioInterface.newPwm(channel);

To acquire an IoChannel instance, it is necessary to be familiar with the specific implementation used for IoInterface. Due to this limitation, there is no universal method of acquiring IoChannel.

Analog Input

The analog input port is represented by AnalogInput interface and can be used to read analog data.

AnalogInput port = ioInterface.newAnalogInput(channel);

// returns the voltage (in volts) currently read by the port.
double voltage = port.getVoltage();

// returns an integer representation of the voltage in the port.
// the precision of the value depends on the implementation and platform and can be
// accessed via port.getMaxValue();
int value = port.getValue();

// closes the port
port.close();

Analog Output

The analog output port is represented by AnalogOutput interface and can be used to write analog data.

AnalogOutput port = ioInterface.newAnalogOutput(channel);

// sets the output voltage (in volts) from the port.
// the maximum output depends on the implementation and platform and can be accessed via
// port.getMaxVoltage();
port.setVoltage(3.0);

// returns the voltage (in volts) last set as output.
double voltage = port.getVoltage();

// sets the output as an integer representation of the voltage in the port.
// the precision of the value depends on the implementation and platform and can be
// accessed via port.getMaxValue();
port.setValue(10);

// closes the port
port.close();

Digital Input

The digital input port is represented by DigitalInput interface and can be used to read digital data.

DigitalInput port = ioInterface.newDigitalInput(channel);

// returns the digital value read by the port: true = HIGH, false = LOW
boolean isHigh = port.get();

// closes the port
port.close();

Digital Output

The digital output port is represented by DigitalOutput interface and can be used to write digital data.

DigitalInput port = ioInterface.newDigitalInput(channel);

// sets the digital output of the port: true = HIGH, false = LOW
port.set(true);

// returns the digital value last set
boolean isHigh = port.get();

// closes the port
port.close();

Pwm

The PWM, or Pulse Width Modulation, port is represented by Pwm interface and can be used to output pulse width modulated digital data.

Pwm port = ioInterface.newPwm(channel);

// sets the duty cycle output of the PWM in percentage (0.5 = 50%). 
port.setDuty(0.5);

// gets the last set duty cycle
port.getDuty();

// closes the port
port.close();

Interfacing with Devices

Using the IoInterface we can create ports which will then can be used to interface with different devices. However, interfacing is not so straightforward, as it depends on the specific device, as each device uses different ports and expects or returns different types of data.

Before we start implementing, we can choose a interface from flashlib.core.io that describes the device. The interfaces contain methods which are recommended for devices of such types.

  • Accelerometer: a single-axis accelerometer
  • Accelerometer3: a 3-axis accelerometer
  • Encoder: an incremental encoder
  • Gyro: a single axis gyroscope
  • RangeFinder: a sensor which returns distance (like Ultrasonic)
  • Solenoid: a solenoid valve
  • DoubleSolenoid: a double solenoid valve
  • PositionController: an actuator with accurate position motion (like servo)
  • SpeedController: an actuator focused on fast rotation (like electric motor)

Built-In

flashlib.core.io provides several classes which interface with different devices:

  • AnalogAccelerometer: a single-axis acceleromter connected via an analog input port.
  • AnalogGyro: a single-axis gyro connected via an analog input port.
  • AnalogRangeFinder: a range-finder connected via an analog input port.
  • PulseEncoder: an incremental encoder connected via digital input port and communicated with pulses.
  • PulseWidthRangeFinder: a range-finder connected via a digital input port and communicates with pulse modulation.
  • Ultrasonic: an ultrasonic range-finder connected with a digital input port to receive distance pulses and with a digital output port to sends pings.
  • PwmSpeedController: a SpeedController controlled via a PWM port.
  • PwmPositionController: a PositionController controlled via a PWM port.

Speed Controllers

Speed controllers are devices which allow control over motors and are represented with SpeedController interface.

SpeedController controller = ...;

// sets the speed of the actuator controlled by the device.
// the speed value is made up of 2 properties:
// - direction of motion (clockwise or counter-clockwise) defined by the sign of the value. 
// + = clockwise, - = counter-clockwise
// - power/speed of motion defined in percentage by the absolute value (0.5 = 50%).
controller.set(0.5);

// stops the motion of the speed controller
controller.stop();

Position Controllers

Position controllers are devices which allow control over position-accurate motors and are represented with PositionController interface.

PositionController controller = ...;

// sets the position of the actuator controlled by the device.
// the position value is a percentage indicating the target position (0.5 = 50% = center position).
// some controllers have circular position motion (like servos). In such cases, 1.0 position is the same as 0.0.
controller.set(0.5);

// stops the motion of the speed controller
controller.stop();