ReduxLib C++ 2023.2.0
|
#include <Canandcolor.h>
Protected Attributes | |
redux::frames::Frame< double > | proximity {0.0, 0_ms} |
redux::frames::Frame< CanandcolorColor > | color {CanandcolorColor{0.0, 0.0, 0.0, 0.0}, 0_ms} |
redux::frames::Frame< CanandcolorDigoutState > | digout {CanandcolorDigoutState{0}, 0_ms} |
redux::frames::Frame< CanandcolorStatus > | status {CanandcolorStatus{0, 0, false, 0_degC}, 0_ms} |
Additional Inherited Members | |
![]() | |
void | SetSettingById (uint8_t settingId, uint8_t *value, uint8_t length) |
void | HandleSettingRecv (uint8_t settingId, uint64_t settingValue) |
std::optional< uint64_t > | ConfirmSetSetting (uint8_t settingIdx, uint8_t *payload, uint8_t length, units::second_t timeout) |
std::optional< uint64_t > | FetchSetting (uint8_t settingIdx, units::second_t timeout) |
bool | SendCANMessage (uint8_t apiIndex, uint8_t *data, uint8_t length) |
template<std::size_t len> requires (len < 8U) | |
void | SendCANMessage (uint8_t msgId, std::span< std::byte, len > data) |
void | SendSettingCommand (uint8_t settingCmdIdx) |
Class for the CAN interface of the Canandcolor.
Operations that receive data from the device (proximity, color, faults, temperature) generally do not block. The object receives data asynchronously from the CAN packet receive thread and reads thus return the last data received.
Operations that set settings or change offsets will generally wait for up to 50ms by default as they will usually wait for a confirmation packet to be received in response – unless the blocking timeout is set to zero, in which case the operations will not block.
Example code:
Canandcolor canandcolor{0}; // device id 0 // Reading the Canandcolor canandcolor.GetProximity(); // returns a proximity value [0..1). Larger values are distances closer to the sensor. // these are all bounded [0..1) canandcolor.GetRed(); canandcolor.GetGreen(); canandcolor.GetBlue(); canandcolor.GetWhite(); // Changing configuration CanandcolorSettings settings{}; settings.SetProximityFramePeriod(10_ms); // set the proximity frame period to be sent every 10 ms settings.SetColorFramePeriod(40_ms); // set the position frame period to be sent every 40 ms settings.SetColorIntegrationPeriod(CanandcolorColorIntegration.kPeriod40Ms); // set the color sensor integration to happen over 80 milliseconds canandcolor.SetSettings(settings, 50_ms); // apply the new settings to the device, with maximum 50 ms timeout per settings op // Faults canandcolor.ClearStickyFaults(); // clears all sticky faults (including the power cycle flag). This call does not block. // The power cycle flag will always be true on boot until the sticky faults have been cleared, // so if this is true the device has rebooted sometime between clearStickyFaults and now. CanandcolorFaults faults = canandcolor.GetStickyFaults(); // fetches faults fmt::print("Canandcolor rebooted: {}\n", faults.powerCycle); // Timestamped data FrameData<Double> proxFrame = canandcolor.GetProximityFrame().GetFrameData(); // gets current proximity + timestamp together proxFrame.GetValue(); // fetched position in rotations proxFrame.GetTimestamp(); // timestamp of the previous position
redux::sensors::canandcolor::Canandcolor::Canandcolor | ( | int | canID | ) |
Instantiates a new Canandcolor object. This object will be constant with respect to whatever CAN id assigned to it, so if a device changes id it may change which device this object reads from.
canID | the device id to use [0..63] |
void redux::sensors::canandcolor::Canandcolor::ClearAllDigoutSlots | ( | CanandcolorDigitalOutput | digout | ) |
Clears all configured "slots" on the specified digital output.
digout | the digital output to clear slots on |
void redux::sensors::canandcolor::Canandcolor::ClearStickyFaults | ( | ) |
Clears sticky faults.
It is recommended to clear this during initialization, so one can check if the device loses power during operation later.
This call does not block, so it may take up to the next status frame (default every 1000 ms) for the sticky faults to be updated. To check for validity, use CanandcolorFaults::FaultsValid() for faults returned by GetStickyFaults()
CanandcolorFaults redux::sensors::canandcolor::Canandcolor::GetActiveFaults | ( | ) |
Returns an object representing currently active faults. Active faults are only active for as long as the error state exists.
|
overridevirtual |
Returns the reference to the CanandAddress representing the combination of CAN bus and CAN device ID that this CanandDevice refers to.
Implementing device subclasses should likely construct a new CanandAddress in their constructor and return it here.
Implements redux::canand::CanandDevice.
double redux::sensors::canandcolor::Canandcolor::GetBlue | ( | ) |
Blue intensity, normalized [0..1) where 0 is none and 1 is as bright as possible.
CanandcolorColor redux::sensors::canandcolor::Canandcolor::GetColor | ( | ) |
Returns a CanandcolorColor object which can also convert to HSV.
|
inline |
Returns the color reading frame, which includes CAN timestamp data.
|
inline |
Returns the digital output state frame, which includes CAN timestamp data.
std::optional< CanandcolorDigoutSlot > redux::sensors::canandcolor::Canandcolor::GetDigoutSlot | ( | CanandcolorDigitalOutput | digout, |
uint8_t | slotIndex, | ||
units::second_t | timeout = 50_ms |
||
) |
Fetches a digout slot's configuration.
These condition slots are used to determine the value of the digital outputs. For more information on how these slots work, see https://docs.reduxrobotics.com/canspec/Canandcolor.html#digout1-config
digout | digital output associated with the slot to fetch from |
slotIndex | The index of the slot to fetch from (0-15) |
timeout | The timeout to wait for the slot to be retrieved (default 50_ms, set to 0_s to not block) |
CanandcolorDigoutState redux::sensors::canandcolor::Canandcolor::GetDigoutState | ( | ) |
Returns a CanandcolorDigoutState object representing the current state of the digital outputs.
double redux::sensors::canandcolor::Canandcolor::GetGreen | ( | ) |
Green intensity, normalized [0..1) where 0 is none and 1 is as bright as possible.
double redux::sensors::canandcolor::Canandcolor::GetProximity | ( | ) |
Gets the currently sensed proximity normalized between [0..1]. Proximity decreases as objects get further away from the sensor face and increases as they approach the sensor. Proximities that are too close will saturate at 1.0.
Note that proximity is not given a unit as different materials and sensor configurations can greatly vary how proximity translates to actual distance. It is generally presumed that users will have to finetune specific thresholds for applications anyway and units may not be meaningful or accurate.
|
inline |
Returns the proximity reading frame.
double redux::sensors::canandcolor::Canandcolor::GetRed | ( | ) |
Red intensity, normalized [0..1) where 0 is none and 1 is as bright as possible.
std::optional< CanandcolorSettings > redux::sensors::canandcolor::Canandcolor::GetSettings | ( | units::second_t | timeout = 500_ms | ) |
Fetches the Canandcolor's current configuration in a blocking manner. This function will block for at least about 0.2-0.3 seconds waiting for the device to reply, so it is best to put this in a teleop or autonomous init function, rather than the main loop.
timeout | maximum number of seconds to wait for settings before giving up |
CanandcolorSettings redux::sensors::canandcolor::Canandcolor::GetSettingsAsync | ( | ) |
Non-blockingly returns a CanandcolorSettings object of the most recent known settings values received from the device.
Most users will probably want to use Canandcolor::GetSettings instead.
One can call this after a Canandcolor::StartFetchSettings() call, and use CanandcolorSettings::AllSettingsReceived() to check if/when all values have been seen. As an example:
// somewhere in an init function Canandcolor canandcolor{0}; canandcolor.StartFetchSettings(); // ... // somewhere in a loop function if (canandcolor.GetSettingsAsync().AllSettingsReceived()) { // do something with the settings object fmt::print("Canandcolor lamp enable: {}\n", canandcolor.GetSettingsAsync().GetLampEnable()); }
If this is called after Canandcolor::SetSettings(CanandcolorSettings), this method will return a settings object where only the fields where the device has echoed the new values back will be populated. To illustrate this, consider the following:
// somewhere in a loop canandcolor.SetSettings(new CanandcolorSettings().setProximityFramePeriod(0.100)); canandcolor.GetSettingsAsync().GetProximityFramePeriod(); // will likely return nullopt as the device hasn't confirmed the previous transaction // after up to ~300 ms... canandcolor.GetSettingsAsync().GetProximityFramePeriod(); // will likely return 100 ms
|
inline |
Returns the current status frame, which includes CAN timestamp data. FrameData objects are immutable.
CanandcolorFaults redux::sensors::canandcolor::Canandcolor::GetStickyFaults | ( | ) |
Returns sticky faults. Sticky faults are the active faults, except once set they do not become unset until ClearStickyFaults() is called.
units::celsius_t redux::sensors::canandcolor::Canandcolor::GetTemperature | ( | ) |
Get onboard device temperature readings in degrees Celsius.
double redux::sensors::canandcolor::Canandcolor::GetWhite | ( | ) |
White intensity, normalized [0..1) This can be used as a proxy for proximity at ranges too close for the normmal proximity sensor to give useful values.
|
overridevirtual |
A callback called when a Redux CAN message is received and should be parsed. Subclasses of CanandDevice should override this to update their internal state accordingly.
HandleMessage will be called on all Redux CAN packets received by the vendordep that match the CanandAddress returned by CanandDevice::GetAddress().
msg | a reference to a CanandMessage representing the received message. The message may not have lifetime outside the function call. |
Implements redux::canand::CanandDevice.
bool redux::sensors::canandcolor::Canandcolor::IsPresent | ( | units::second_t | timeout = 2000_ms | ) |
Checks whether or not the device has sent a message within the last timeout seconds.
timeout | window to check for message updates in seconds (default 2000 ms) |
std::optional< CanandcolorSettings > redux::sensors::canandcolor::Canandcolor::ResetFactoryDefaults | ( | units::second_t | timeout = 500_ms | ) |
Resets the Canandcolor to factory defaults.
timeout | how long to wait for the new settings to be confirmed by the device in seconds (suggested at least 0.35 seconds) |
bool redux::sensors::canandcolor::Canandcolor::SetDigoutSlot | ( | CanandcolorDigitalOutput | digout, |
uint8_t | slotIndex, | ||
CanandcolorDigoutSlot & | slotConfig, | ||
units::second_t | timeout = 50_ms |
||
) |
Sets an indexed slot on a Canandcolor digital output.
These condition slots are used to determine the value of the digital outputs. For more information on how these slots work, see https://docs.reduxrobotics.com/canspec/Canandcolor.html#digout1-config
digout | The digital output associated with the slot to write |
slotIndex | The index of the slot to write to (0-15) |
slotConfig | The actual slot data (see CanandcolorDigoutSlot) |
timeout | The timeout to wait for a confirmation message (default 50_ms, set to 0_s to not block) |
void redux::sensors::canandcolor::Canandcolor::SetLampLED | ( | bool | lamp | ) |
Sets whether or not the onboard lamp LED is to be powered.
The LED can also be physically turned off regardless of setting with the onboard switch.
The LED is useful for measuring the color of objects that do not themselves emit light (e.g. most game pieces) or for using the white channel to estimate very close proximity.
By factory default the lamp is enabled.
lamp | whether to enable or disable the lamp LED |
void redux::sensors::canandcolor::Canandcolor::SetPartyMode | ( | uint8_t | level | ) |
Controls "party mode" – an device identification tool that blinks the onboard LED various colors at a user-specified strobe period. The strobe period of the LED will be (50 milliseconds * level). Setting this to 0 disables party mode.
This function does not block.
level | the party level value to set. |
bool redux::sensors::canandcolor::Canandcolor::SetSettings | ( | CanandcolorSettings & | settings, |
units::second_t | timeout = 50_ms |
||
) |
Applies the settings from a CanandcolorSettings object to the device. For more information, see the CanandcolorSettings class documentation.
settings | the CanandcolorSettings to update the device with |
timeout | maximum time in seconds to wait for each setting to be confirmed (default 50 ms). Set to 0 to not check (and not block). |
void redux::sensors::canandcolor::Canandcolor::StartFetchSettings | ( | ) |
Tells the Canandcolor to begin transmitting its settings; once they are all transmitted (after ~200-300ms), the values can be retrieved from Canandcolor::GetSettingsAsync()
|
protected |
internal Frame variable holding current color state
|
protected |
internal Frame variable holding current digital output state
|
protected |
internal Frame variable holding current proximity state
|
protected |
internal Frame variable holding current status value state