LightLib
PROS library for VEX V5: EKF/MCL localization, RAMSETE path following, high-level chassis API
Loading...
Searching...
No Matches
asyncMotionProfileController.hpp
Go to the documentation of this file.
1/*
2 * This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 */
6#pragma once
7
16#include <atomic>
17#include <iostream>
18#include <map>
19
20#include "squiggles.hpp"
21
22namespace okapi {
23class AsyncMotionProfileController : public AsyncPositionController<std::string, PathfinderPoint> {
24 public:
37 const PathfinderLimits &ilimits,
38 const std::shared_ptr<ChassisModel> &imodel,
39 const ChassisScales &iscales,
41 const std::shared_ptr<Logger> &ilogger = Logger::getDefaultLogger());
42
44
46
48
60 void generatePath(std::initializer_list<PathfinderPoint> iwaypoints, const std::string &ipathId);
61
78 void generatePath(std::initializer_list<PathfinderPoint> iwaypoints,
79 const std::string &ipathId,
80 const PathfinderLimits &ilimits);
81
90 bool removePath(const std::string &ipathId);
91
97 std::vector<std::string> getPaths();
98
105 void setTarget(std::string ipathId) override;
106
115 void setTarget(std::string ipathId, bool ibackwards, bool imirrored = false);
116
121 void controllerSet(std::string ivalue) override;
122
128 std::string getTarget() override;
129
135 std::string getProcessValue() const override;
136
141 void waitUntilSettled() override;
142
151 void moveTo(std::initializer_list<PathfinderPoint> iwaypoints,
152 bool ibackwards = false,
153 bool imirrored = false);
154
164 void moveTo(std::initializer_list<PathfinderPoint> iwaypoints,
165 const PathfinderLimits &ilimits,
166 bool ibackwards = false,
167 bool imirrored = false);
168
176 PathfinderPoint getError() const override;
177
186 bool isSettled() override;
187
192 void reset() override;
193
198 void flipDisable() override;
199
206 void flipDisable(bool iisDisabled) override;
207
213 bool isDisabled() const override;
214
219 void tarePosition() override;
220
227 void setMaxVelocity(std::int32_t imaxVelocity) override;
228
234
239
248 void storePath(const std::string &idirectory, const std::string &ipathId);
249
257 void loadPath(const std::string &idirectory, const std::string &ipathId);
258
265 void forceRemovePath(const std::string &ipathId);
266
267 protected:
268 std::shared_ptr<Logger> logger;
269 std::map<std::string, std::vector<squiggles::ProfilePoint>> paths{};
271 std::shared_ptr<ChassisModel> model;
275
276 // This must be locked when accessing the current path
278
279 std::string currentPath{""};
280 std::atomic_bool isRunning{false};
281 std::atomic_int direction{1};
282 std::atomic_bool mirrored{false};
283 std::atomic_bool disabled{false};
284 std::atomic_bool dtorCalled{false};
286
287 static void trampoline(void *context);
288 void loop();
289
293 virtual void executeSinglePath(const std::vector<squiggles::ProfilePoint> &path,
294 std::unique_ptr<AbstractRate> rate);
295
302 QAngularSpeed convertLinearToRotational(QSpeed linear) const;
303
304 std::string getPathErrorMessage(const std::vector<PathfinderPoint> &points,
305 const std::string &ipathId,
306 int length);
307
316 static std::string makeFilePath(const std::string &directory, const std::string &filename);
317
318 void internalStorePath(std::ostream &file, const std::string &ipathId);
319 void internalLoadPath(std::istream &file, const std::string &ipathId);
320 void internalLoadPathfinderPath(std::istream &leftFile,
321 std::istream &rightFile,
322 const std::string &ipathId);
323
324 static constexpr double DT = 0.01;
325};
326} // namespace okapi
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 unti...
bool isDisabled() const override
Returns whether the controller is currently disabled.
virtual void executeSinglePath(const std::vector< squiggles::ProfilePoint > &path, std::unique_ptr< AbstractRate > rate)
Follow the supplied path.
void waitUntilSettled() override
Blocks the current task until the controller has settled.
std::string getProcessValue() const override
This is overridden to return the current path.
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 getPathErrorMessage(const std::vector< PathfinderPoint > &points, const std::string &ipathId, int length)
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 flipDisable(bool iisDisabled) override
Sets whether the controller is off or on.
void internalStorePath(std::ostream &file, const std::string &ipathId)
std::map< std::string, std::vector< squiggles::ProfilePoint > > paths
void internalLoadPathfinderPath(std::istream &leftFile, std::istream &rightFile, const std::string &ipathId)
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.
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 unti...
std::vector< std::string > getPaths()
Gets the identifiers of all paths saved in this AsyncMotionProfileController.
bool removePath(const std::string &ipathId)
Removes a path and frees the memory it used.
void flipDisable() override
Changes whether the controller is off or on.
static std::string makeFilePath(const std::string &directory, const std::string &filename)
Joins and escapes a directory and file name.
QAngularSpeed convertLinearToRotational(QSpeed linear) const
Converts linear chassis speed to rotational motor speed.
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.
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 setMaxVelocity(std::int32_t imaxVelocity) override
This implementation does nothing because the maximum velocity is configured using PathfinderLimits el...
void reset() override
Resets the controller so it can start from 0 again properly.
std::string getTarget() override
Gets the last set target, or the default target if none was set.
CrossplatformThread * getThread() const
PathfinderPoint getError() const override
Returns the last error of the controller.
bool isSettled() override
Returns whether the controller has settled at the target.
void forceRemovePath(const std::string &ipathId)
Attempts to remove a path without stopping execution.
void storePath(const std::string &idirectory, const std::string &ipathId)
Saves a generated path to a file.
AsyncMotionProfileController(AsyncMotionProfileController &&other)=delete
static void trampoline(void *context)
void setTarget(std::string ipathId) override
Executes a path with the given ID.
void internalLoadPath(std::istream &file, const std::string &ipathId)
AsyncMotionProfileController & operator=(AsyncMotionProfileController &&other)=delete
void startThread()
Starts the internal thread.
void tarePosition() override
This implementation does nothing because the API always requires the starting position to be specifie...
static std::shared_ptr< Logger > getDefaultLogger()
Utility class for holding an AbstractTimer, AbstractRate, and SettledUtil together in one class since...
Definition timeUtil.hpp:18
A simple structure representing the full ratio between motor and wheel.