Hardware access layer¶
To ensure hardware is operated correctly, each independant electronic subsystem is encapsulated in a class. This also allows us to make meaningful constructors and destructors, such as: setting up ports for inputs; turning off LEDs at shutdown; driving a motor slowly to hold the deliverer at startup.
These all get wrapped in a single Robot
instance, with members as
follows.
- struct
Actuators¶
Things which cause parts of the robot to move
- class
Interface to the two-wheeled drive system.
Inherits from Device
Public Functions
-
Drive::
Drive
(RLink &r, Configuration c = Drive::_defConfig)¶ Initialize a drive over a connection.
- Parameters
r
: the link to the robotc
: the drive geometry and speeds, used to populate Drive::maxSpeeds
-
void
Drive::
move
(move_args args)¶ Should ensure that abs(args.forward) + abs(args.steer) <= 1
- Parameters
args.forward
: non-dimensional linear speed: 1 is full speed forwards, -1 is full speed backwardsargs.steer
: non-dimensional rotational speed: 1 is full speed CCW
-
Timeout
Drive::
straight
(float dist, float speed = 1)¶ Move in a straight line, and return a timeout indicating expected completion.
-
Timeout
Drive::
turn
(float angle, float speed = 1)¶ Turn an angle on the spot, and return a timeout indicating expected completion.
-
void
Drive::
setWheelSpeeds
(float left, float right)¶ low-level motor access. Speeds should be between 1 and -1
-
void
Drive::
stop
()¶ shorthand for no motion
Public Static Functions
-
uint8_t
Drive::
convertSpeed
(float s)¶ convert floating point speed to sign/magnitude
- struct
Describes the physical configuration of the robot.
- struct
struct indicating maximum speeds, built from a Configuration
-
- class
Interface to the egg-grabbing arm of the robot.
Inherits from Device
- class
Interface to the runner holding the eggs, its indicator LEDs, the light gate that verifies the presence of an egg, and the bucket at the end of the runner which delivers the eggs into their cups.
Affectionately known as the courier, as it carries things
Inherits from Device
Public Functions
-
void
Courier::
recordEggAdded
(EggType e)¶ Indicate that a new egg has been added to the rail.
This updates the internal record of currently-held eggs, and turns on the appropriate LEDs.
-
void
Courier::
unloadEgg
()¶ Unload the egg at the bottom of the stack, updating state and LEDs.
-
int
Courier::
volume
() const¶ The number of eggs on the rail.
-
bool
Courier::
eggDetected
() const¶ if an egg is at the bottom of the courier
-
void
Sensors¶
Things which give the robot information about its surroundings
- class
Interface to the LEDs and LDR comprising the egg sensor.
Includes the algorithm for identifying eggs
Inherits from Device
- struct
Public Members
-
uint8_t
EggSensor::Reading::
r
¶ reflection from red LED
-
uint8_t
EggSensor::Reading::
b
¶ reflection from blue LED
-
uint8_t
EggSensor::Reading::
w
¶ reflection from white LED
-
uint8_t
EggSensor::Reading::
a
¶ ambient reading
-
std::array<float, EGG_TYPE_COUNT>
EggSensor::Reading::
probabilities
¶ “distances” to each egg. Lower values indicate greater likelihood
-
uint8_t
- class
Interface to the three front-mounted line sensors.
Inherits from Device
- struct
Public Members
-
bool
LineSensors::Reading::
lsl
¶ left sensor reading
-
bool
LineSensors::Reading::
lsc
¶ right sensor reading
-
bool
LineSensors::Reading::
lsr
¶ center sensor reading
-
bool
LineSensors::Reading::
lsa
¶ arm sensor reading
-
float
LineSensors::Reading::
position
¶ Line position, where between -1 and 1, with left positive.
+-Inf and NaN indicate a lost line
-
bool
- class
Interface to the limit switch bumper on the front of the robot.
Inherits from Device
- struct
For ease of debugging, some of these readings have ostream <<
overloads, to
allow:
std::cout << robot.ls.read() << std::endl
Low level¶
All of the above classes use the following utility classes to interface with the hardware.
- class
Wraps robot_link to indicate failures by throwing a LinkError object.
Inherits from robot_link
- class
Base class for all devices which require a link to the robot.
Subclassed by Arm, Bumper, Courier, Drive, EggSensor, LineSensors, Port
-
type
port::
Name
¶ An enum of port names, from
P0
toP7
, andPA0
toPA7
- class
Interface to a set on pins on a particular port.
Allows masking of pins, to allow multiple
Device
s to share a I2C port without interfering with each other’s bitsProvides operator overloading for simple use:
Port sensor(rlink, port::P2, 0xF); // bottom 4 bits of port 2 uint8_t reading = sensor; // read sensor sensor = 0x42; // write to sensor
Note that conversion to an int will return the current input, which is not necesarily the previous output
Inherits from Device
Public Functions
-
Port::
Port
(RLink &r, port::Name p, uint8_t mask = 0xFF)¶ Create a port over the connection
r
, using the port with addressp
.Optionally specify a set of bits
mask
to restrict the scope of this instance to. Throws PinsDoublyMapped if multiple instances attempt to use the same ports
-
Port::
operator uint8_t
() const¶ Read a word to the port, keeping only the bits specified in the mask.
-
void
Port::
operator=
(uint8_t val)¶ Write a word to the port, touching only the bits specified in the mask.
-
Exceptions¶
To prevent errors silently occuring without being noticed (or worse, error codes being handled as values), exceptions are used for all critical errors. These all derive from std::exception, and implement the const char* what() member to give a brief summary of the error to the programmer, to allow them to fix the appropriate electrical/network problem.
- class
Thrown when an RLink command or request goes wrong.
Contains the original error code
Inherits from exception
Subclassed by PortError
Public Functions
-
virtual const char *
LinkError::
what
() const¶ override of std::exception::what()
-
virtual const char *
- struct
Specialization of LinkError, thrown when an I2C error occurs when accessing a port.
Typically implies loss of electrical connection
Inherits from LinkError
Public Functions
-
virtual const char *
PortError::
what
() const¶ override of std::exception::what()
-
virtual const char *
- struct
Specialization of LinkError, thrown when an I2C error occurs when accessing a port.
Typically implies loss of electrical connection
Inherits from exception