LightLib
PROS library for VEX V5: EKF/MCL localization, RAMSETE path following, high-level chassis API
Loading...
Searching...
No Matches
okapi::AsyncMotionProfileController Class Reference

#include <asyncMotionProfileController.hpp>

Inheritance diagram for okapi::AsyncMotionProfileController:
okapi::AsyncPositionController< std::string, PathfinderPoint > okapi::AsyncController< Input, Output > okapi::ClosedLoopController< Input, Output > okapi::ControllerOutput< Input >

Public Member Functions

 AsyncMotionProfileController (const TimeUtil &itimeUtil, const PathfinderLimits &ilimits, const std::shared_ptr< ChassisModel > &imodel, const ChassisScales &iscales, const AbstractMotor::GearsetRatioPair &ipair, const std::shared_ptr< Logger > &ilogger=Logger::getDefaultLogger())
 An Async Controller which generates and follows 2D motion profiles.
 
 AsyncMotionProfileController (AsyncMotionProfileController &&other)=delete
 
AsyncMotionProfileControlleroperator= (AsyncMotionProfileController &&other)=delete
 
 ~AsyncMotionProfileController () override
 
void generatePath (std::initializer_list< PathfinderPoint > iwaypoints, const std::string &ipathId)
 Generates a path which intersects the given waypoints and saves it internally with a key of pathId.
 
void generatePath (std::initializer_list< PathfinderPoint > iwaypoints, const std::string &ipathId, const PathfinderLimits &ilimits)
 Generates a path which intersects the given waypoints and saves it internally with a key of pathId.
 
bool removePath (const std::string &ipathId)
 Removes a path and frees the memory it used.
 
std::vector< std::string > getPaths ()
 Gets the identifiers of all paths saved in this AsyncMotionProfileController.
 
void setTarget (std::string ipathId) override
 Executes a path with the given ID.
 
void setTarget (std::string ipathId, bool ibackwards, bool imirrored=false)
 Executes a path with the given ID.
 
void controllerSet (std::string ivalue) override
 Writes the value of the controller output.
 
std::string getTarget () override
 Gets the last set target, or the default target if none was set.
 
std::string getProcessValue () const override
 This is overridden to return the current path.
 
void waitUntilSettled () override
 Blocks the current task until the controller has settled.
 
void moveTo (std::initializer_list< PathfinderPoint > iwaypoints, bool ibackwards=false, bool imirrored=false)
 Generates a new path from the position (typically the current position) to the target and blocks until the controller has settled.
 
void moveTo (std::initializer_list< PathfinderPoint > iwaypoints, const PathfinderLimits &ilimits, bool ibackwards=false, bool imirrored=false)
 Generates a new path from the position (typically the current position) to the target and blocks until the controller has settled.
 
PathfinderPoint getError () const override
 Returns the last error of the controller.
 
bool isSettled () override
 Returns whether the controller has settled at the target.
 
void reset () override
 Resets the controller so it can start from 0 again properly.
 
void flipDisable () override
 Changes whether the controller is off or on.
 
void flipDisable (bool iisDisabled) override
 Sets whether the controller is off or on.
 
bool isDisabled () const override
 Returns whether the controller is currently disabled.
 
void tarePosition () override
 This implementation does nothing because the API always requires the starting position to be specified.
 
void setMaxVelocity (std::int32_t imaxVelocity) override
 This implementation does nothing because the maximum velocity is configured using PathfinderLimits elsewhere.
 
void startThread ()
 Starts the internal thread.
 
CrossplatformThreadgetThread () const
 
void storePath (const std::string &idirectory, const std::string &ipathId)
 Saves a generated path to a file.
 
void loadPath (const std::string &idirectory, const std::string &ipathId)
 Loads a path from a directory on the SD card containing a path CSV file.
 
void forceRemovePath (const std::string &ipathId)
 Attempts to remove a path without stopping execution.
 
- Public Member Functions inherited from okapi::ClosedLoopController< Input, Output >
virtual ~ClosedLoopController ()=default
 
virtual void setTarget (Input itarget)=0
 Sets the target for the controller.
 
- Public Member Functions inherited from okapi::ControllerOutput< Input >
virtual void controllerSet (Input ivalue)=0
 Writes the value of the controller output.
 

Protected Member Functions

void loop ()
 
virtual void executeSinglePath (const std::vector< squiggles::ProfilePoint > &path, std::unique_ptr< AbstractRate > rate)
 Follow the supplied path.
 
QAngularSpeed convertLinearToRotational (QSpeed linear) const
 Converts linear chassis speed to rotational motor speed.
 
std::string getPathErrorMessage (const std::vector< PathfinderPoint > &points, const std::string &ipathId, int length)
 
void internalStorePath (std::ostream &file, const std::string &ipathId)
 
void internalLoadPath (std::istream &file, const std::string &ipathId)
 
void internalLoadPathfinderPath (std::istream &leftFile, std::istream &rightFile, const std::string &ipathId)
 

Static Protected Member Functions

static void trampoline (void *context)
 
static std::string makeFilePath (const std::string &directory, const std::string &filename)
 Joins and escapes a directory and file name.
 

Protected Attributes

std::shared_ptr< Loggerlogger
 
std::map< std::string, std::vector< squiggles::ProfilePoint > > paths {}
 
PathfinderLimits limits
 
std::shared_ptr< ChassisModelmodel
 
ChassisScales scales
 
AbstractMotor::GearsetRatioPair pair
 
TimeUtil timeUtil
 
CrossplatformMutex currentPathMutex
 
std::string currentPath {""}
 
std::atomic_bool isRunning {false}
 
std::atomic_int direction {1}
 
std::atomic_bool mirrored {false}
 
std::atomic_bool disabled {false}
 
std::atomic_bool dtorCalled {false}
 
CrossplatformThreadtask {nullptr}
 

Static Protected Attributes

static constexpr double DT = 0.01
 

Detailed Description

Definition at line 23 of file asyncMotionProfileController.hpp.

Constructor & Destructor Documentation

◆ AsyncMotionProfileController() [1/2]

okapi::AsyncMotionProfileController::AsyncMotionProfileController ( const TimeUtil itimeUtil,
const PathfinderLimits ilimits,
const std::shared_ptr< ChassisModel > &  imodel,
const ChassisScales iscales,
const AbstractMotor::GearsetRatioPair ipair,
const std::shared_ptr< Logger > &  ilogger = Logger::getDefaultLogger() 
)

An Async Controller which generates and follows 2D motion profiles.

Throws a std::invalid_argument exception if the gear ratio is zero.

Parameters
itimeUtilThe TimeUtil.
ilimitsThe default limits.
imodelThe chassis model to control.
iscalesThe chassis dimensions.
ipairThe gearset.
iloggerThe logger this instance will log to.

◆ AsyncMotionProfileController() [2/2]

okapi::AsyncMotionProfileController::AsyncMotionProfileController ( AsyncMotionProfileController &&  other)
delete

◆ ~AsyncMotionProfileController()

okapi::AsyncMotionProfileController::~AsyncMotionProfileController ( )
override

Member Function Documentation

◆ controllerSet()

void okapi::AsyncMotionProfileController::controllerSet ( std::string  ivalue)
override

Writes the value of the controller output.

This method might be automatically called in another thread by the controller. This just calls setTarget().

◆ convertLinearToRotational()

QAngularSpeed okapi::AsyncMotionProfileController::convertLinearToRotational ( QSpeed  linear) const
protected

Converts linear chassis speed to rotational motor speed.

Parameters
linearchassis frame speed
Returns
motor frame speed

◆ executeSinglePath()

virtual void okapi::AsyncMotionProfileController::executeSinglePath ( const std::vector< squiggles::ProfilePoint > &  path,
std::unique_ptr< AbstractRate rate 
)
protectedvirtual

Follow the supplied path.

Must follow the disabled lifecycle.

◆ flipDisable() [1/2]

void okapi::AsyncMotionProfileController::flipDisable ( )
overridevirtual

Changes whether the controller is off or on.

Turning the controller on after it was off will NOT cause the controller to move to its last set target.

Implements okapi::ClosedLoopController< Input, Output >.

◆ flipDisable() [2/2]

void okapi::AsyncMotionProfileController::flipDisable ( bool  iisDisabled)
overridevirtual

Sets whether the controller is off or on.

Turning the controller on after it was off will NOT cause the controller to move to its last set target, unless it was reset in that time.

Parameters
iisDisabledwhether the controller is disabled

Implements okapi::ClosedLoopController< Input, Output >.

◆ forceRemovePath()

void okapi::AsyncMotionProfileController::forceRemovePath ( const std::string &  ipathId)

Attempts to remove a path without stopping execution.

If that fails, disables the controller and removes the path.

Parameters
ipathIdThe path ID that will be removed

◆ generatePath() [1/2]

void okapi::AsyncMotionProfileController::generatePath ( std::initializer_list< PathfinderPoint iwaypoints,
const std::string &  ipathId 
)

Generates a path which intersects the given waypoints and saves it internally with a key of pathId.

Call executePath() with the same pathId to run it.

If the waypoints form a path which is impossible to achieve, an instance of std::runtime_error is thrown (and an error is logged) which describes the waypoints. If there are no waypoints, no path is generated.

Parameters
iwaypointsThe waypoints to hit on the path.
ipathIdA unique identifier to save the path with.

◆ generatePath() [2/2]

void okapi::AsyncMotionProfileController::generatePath ( std::initializer_list< PathfinderPoint iwaypoints,
const std::string &  ipathId,
const PathfinderLimits ilimits 
)

Generates a path which intersects the given waypoints and saves it internally with a key of pathId.

Call executePath() with the same pathId to run it.

If the waypoints form a path which is impossible to achieve, an instance of std::runtime_error is thrown (and an error is logged) which describes the waypoints. If there are no waypoints, no path is generated.

NOTE: The waypoints are expected to be in the okapi::State::FRAME_TRANSFORMATION format where +x is forward, +y is right, and 0 theta is measured from the +x axis to the +y axis.

Parameters
iwaypointsThe waypoints to hit on the path.
ipathIdA unique identifier to save the path with.
ilimitsThe limits to use for this path only.

◆ getError()

PathfinderPoint okapi::AsyncMotionProfileController::getError ( ) const
overridevirtual

Returns the last error of the controller.

Does not update when disabled. This implementation always returns zero since the robot is assumed to perfectly follow the path. Subclasses can override this to be more accurate using odometry information.

Returns
the last error

Implements okapi::ClosedLoopController< Input, Output >.

◆ getPathErrorMessage()

std::string okapi::AsyncMotionProfileController::getPathErrorMessage ( const std::vector< PathfinderPoint > &  points,
const std::string &  ipathId,
int  length 
)
protected

◆ getPaths()

std::vector< std::string > okapi::AsyncMotionProfileController::getPaths ( )

Gets the identifiers of all paths saved in this AsyncMotionProfileController.

Returns
The identifiers of all paths

◆ getProcessValue()

std::string okapi::AsyncMotionProfileController::getProcessValue ( ) const
overridevirtual

This is overridden to return the current path.

Returns
The most recent value of the process variable.

Implements okapi::ClosedLoopController< Input, Output >.

◆ getTarget()

std::string okapi::AsyncMotionProfileController::getTarget ( )
overridevirtual

Gets the last set target, or the default target if none was set.

Returns
the last target

Implements okapi::ClosedLoopController< Input, Output >.

◆ getThread()

CrossplatformThread * okapi::AsyncMotionProfileController::getThread ( ) const
Returns
The underlying thread handle.

◆ internalLoadPath()

void okapi::AsyncMotionProfileController::internalLoadPath ( std::istream &  file,
const std::string &  ipathId 
)
protected

◆ internalLoadPathfinderPath()

void okapi::AsyncMotionProfileController::internalLoadPathfinderPath ( std::istream &  leftFile,
std::istream &  rightFile,
const std::string &  ipathId 
)
protected

◆ internalStorePath()

void okapi::AsyncMotionProfileController::internalStorePath ( std::ostream &  file,
const std::string &  ipathId 
)
protected

◆ isDisabled()

bool okapi::AsyncMotionProfileController::isDisabled ( ) const
overridevirtual

Returns whether the controller is currently disabled.

Returns
whether the controller is currently disabled

Implements okapi::ClosedLoopController< Input, Output >.

◆ isSettled()

bool okapi::AsyncMotionProfileController::isSettled ( )
overridevirtual

Returns whether the controller has settled at the target.

Determining what settling means is implementation-dependent.

If the controller is disabled, this method must return true.

Returns
whether the controller is settled

Implements okapi::ClosedLoopController< Input, Output >.

◆ loadPath()

void okapi::AsyncMotionProfileController::loadPath ( const std::string &  idirectory,
const std::string &  ipathId 
)

Loads a path from a directory on the SD card containing a path CSV file.

/usd/ is automatically prepended to idirectory if it is not specified.

Parameters
idirectoryThe directory that the path files are stored in
ipathIdThe path ID that the paths are stored under (and will be loaded into)

◆ loop()

void okapi::AsyncMotionProfileController::loop ( )
protected

◆ makeFilePath()

static std::string okapi::AsyncMotionProfileController::makeFilePath ( const std::string &  directory,
const std::string &  filename 
)
staticprotected

Joins and escapes a directory and file name.

Parameters
directoryThe directory path, separated by forward slashes (/) and with or without a trailing slash
filenameThe file name in the directory
Returns
the fully qualified and legal path name

◆ moveTo() [1/2]

void okapi::AsyncMotionProfileController::moveTo ( std::initializer_list< PathfinderPoint iwaypoints,
bool  ibackwards = false,
bool  imirrored = false 
)

Generates a new path from the position (typically the current position) to the target and blocks until the controller has settled.

Does not save the path which was generated.

Parameters
iwaypointsThe waypoints to hit on the path.
ibackwardsWhether to follow the profile backwards.
imirroredWhether to follow the profile mirrored.

◆ moveTo() [2/2]

void okapi::AsyncMotionProfileController::moveTo ( std::initializer_list< PathfinderPoint iwaypoints,
const PathfinderLimits ilimits,
bool  ibackwards = false,
bool  imirrored = false 
)

Generates a new path from the position (typically the current position) to the target and blocks until the controller has settled.

Does not save the path which was generated.

Parameters
iwaypointsThe waypoints to hit on the path.
ilimitsThe limits to use for this path only.
ibackwardsWhether to follow the profile backwards.
imirroredWhether to follow the profile mirrored.

◆ operator=()

AsyncMotionProfileController & okapi::AsyncMotionProfileController::operator= ( AsyncMotionProfileController &&  other)
delete

◆ removePath()

bool okapi::AsyncMotionProfileController::removePath ( const std::string &  ipathId)

Removes a path and frees the memory it used.

This function returns true if the path was either deleted or didn't exist in the first place. It returns false if the path could not be removed because it is running.

Parameters
ipathIdA unique identifier for the path, previously passed to generatePath()
Returns
True if the path no longer exists

◆ reset()

void okapi::AsyncMotionProfileController::reset ( )
overridevirtual

Resets the controller so it can start from 0 again properly.

Keeps configuration from before. This implementation also stops movement.

Implements okapi::ClosedLoopController< Input, Output >.

◆ setMaxVelocity()

void okapi::AsyncMotionProfileController::setMaxVelocity ( std::int32_t  imaxVelocity)
overridevirtual

This implementation does nothing because the maximum velocity is configured using PathfinderLimits elsewhere.

Parameters
imaxVelocityIgnored.

Implements okapi::AsyncPositionController< std::string, PathfinderPoint >.

◆ setTarget() [1/2]

void okapi::AsyncMotionProfileController::setTarget ( std::string  ipathId)
override

Executes a path with the given ID.

If there is no path matching the ID, the method will return. Any targets set while a path is being followed will be ignored.

Parameters
ipathIdA unique identifier for the path, previously passed to generatePath().

◆ setTarget() [2/2]

void okapi::AsyncMotionProfileController::setTarget ( std::string  ipathId,
bool  ibackwards,
bool  imirrored = false 
)

Executes a path with the given ID.

If there is no path matching the ID, the method will return. Any targets set while a path is being followed will be ignored.

Parameters
ipathIdA unique identifier for the path, previously passed to generatePath().
ibackwardsWhether to follow the profile backwards.
imirroredWhether to follow the profile mirrored.

◆ startThread()

void okapi::AsyncMotionProfileController::startThread ( )

Starts the internal thread.

This should not be called by normal users. This method is called by the AsyncMotionProfileControllerBuilder when making a new instance of this class.

◆ storePath()

void okapi::AsyncMotionProfileController::storePath ( const std::string &  idirectory,
const std::string &  ipathId 
)

Saves a generated path to a file.

Paths are stored as <ipathId>.csv. An SD card must be inserted into the brain and the directory must exist. idirectory can be prefixed with /usd/, but it this is not required.

Parameters
idirectoryThe directory to store the path file in
ipathIdThe path ID of the generated path

◆ tarePosition()

void okapi::AsyncMotionProfileController::tarePosition ( )
overridevirtual

This implementation does nothing because the API always requires the starting position to be specified.

Implements okapi::AsyncPositionController< std::string, PathfinderPoint >.

◆ trampoline()

static void okapi::AsyncMotionProfileController::trampoline ( void *  context)
staticprotected

◆ waitUntilSettled()

void okapi::AsyncMotionProfileController::waitUntilSettled ( )
overridevirtual

Blocks the current task until the controller has settled.

This controller is settled when it has finished following a path. If no path is being followed, it is settled.

Implements okapi::AsyncController< Input, Output >.

Member Data Documentation

◆ currentPath

std::string okapi::AsyncMotionProfileController::currentPath {""}
protected

Definition at line 279 of file asyncMotionProfileController.hpp.

◆ currentPathMutex

CrossplatformMutex okapi::AsyncMotionProfileController::currentPathMutex
protected

Definition at line 277 of file asyncMotionProfileController.hpp.

◆ direction

std::atomic_int okapi::AsyncMotionProfileController::direction {1}
protected

Definition at line 281 of file asyncMotionProfileController.hpp.

◆ disabled

std::atomic_bool okapi::AsyncMotionProfileController::disabled {false}
protected

Definition at line 283 of file asyncMotionProfileController.hpp.

◆ DT

constexpr double okapi::AsyncMotionProfileController::DT = 0.01
staticconstexprprotected

Definition at line 324 of file asyncMotionProfileController.hpp.

◆ dtorCalled

std::atomic_bool okapi::AsyncMotionProfileController::dtorCalled {false}
protected

Definition at line 284 of file asyncMotionProfileController.hpp.

◆ isRunning

std::atomic_bool okapi::AsyncMotionProfileController::isRunning {false}
protected

Definition at line 280 of file asyncMotionProfileController.hpp.

◆ limits

PathfinderLimits okapi::AsyncMotionProfileController::limits
protected

Definition at line 270 of file asyncMotionProfileController.hpp.

◆ logger

std::shared_ptr<Logger> okapi::AsyncMotionProfileController::logger
protected

Definition at line 268 of file asyncMotionProfileController.hpp.

◆ mirrored

std::atomic_bool okapi::AsyncMotionProfileController::mirrored {false}
protected

Definition at line 282 of file asyncMotionProfileController.hpp.

◆ model

std::shared_ptr<ChassisModel> okapi::AsyncMotionProfileController::model
protected

Definition at line 271 of file asyncMotionProfileController.hpp.

◆ pair

AbstractMotor::GearsetRatioPair okapi::AsyncMotionProfileController::pair
protected

Definition at line 273 of file asyncMotionProfileController.hpp.

◆ paths

std::map<std::string, std::vector<squiggles::ProfilePoint> > okapi::AsyncMotionProfileController::paths {}
protected

Definition at line 269 of file asyncMotionProfileController.hpp.

◆ scales

ChassisScales okapi::AsyncMotionProfileController::scales
protected

Definition at line 272 of file asyncMotionProfileController.hpp.

◆ task

CrossplatformThread* okapi::AsyncMotionProfileController::task {nullptr}
protected

Definition at line 285 of file asyncMotionProfileController.hpp.

◆ timeUtil

TimeUtil okapi::AsyncMotionProfileController::timeUtil
protected

Definition at line 274 of file asyncMotionProfileController.hpp.


The documentation for this class was generated from the following file: