-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create AutonomousTask to handle autonomous navigation
Use DriveToWaypointCommand functionality to execute an autonomous task. Add necessary constants to Constants.h Start autonomous task from MissionControlProtocol.cpp
- Loading branch information
1 parent
6c18297
commit 037fdfc
Showing
7 changed files
with
170 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
#include "AutonomousTask.h" | ||
|
||
#include "../Constants.h" | ||
#include "../Globals.h" | ||
#include "../commands/DriveToWaypointCommand.h" | ||
#include "../world_interface/world_interface.h" | ||
|
||
using namespace std::chrono_literals; | ||
|
||
namespace autonomous { | ||
|
||
AutonomousTask::AutonomousTask(){}; | ||
|
||
AutonomousTask::~AutonomousTask() { | ||
if (_autonomous_task_thread.joinable()) { | ||
_autonomous_task_thread.join(); | ||
} | ||
} | ||
|
||
void AutonomousTask::start(const navtypes::point_t& waypointCoords) { | ||
if (_autonomous_task_thread.joinable()) { | ||
kill(); | ||
} | ||
|
||
_waypoint_coords = waypointCoords; | ||
_kill_called = false; | ||
_autonomous_task_thread = std::thread(&autonomous::AutonomousTask::navigate, this); | ||
} | ||
|
||
void AutonomousTask::navigate() { | ||
commands::DriveToWaypointCommand cmd(_waypoint_coords, THETA_KP, DRIVE_VEL, SLOW_DRIVE_VEL, | ||
DONE_THRESHOLD, CLOSE_TO_TARGET_DUR_VAL); | ||
|
||
auto sleepUntil = std::chrono::steady_clock().now(); | ||
while (!cmd.isDone()) { | ||
auto latestGPS = robot::readGPS(); | ||
auto latestHeading = robot::readIMUHeading(); | ||
|
||
if (latestGPS.isFresh(2000ms) && latestHeading.isFresh(2000ms)) { | ||
auto gpsData = latestGPS.getData(); | ||
navtypes::pose_t latestPos(gpsData.x(), gpsData.y(), latestHeading); | ||
cmd.setState(latestPos, robot::types::dataclock::now()); | ||
commands::command_t output = cmd.getOutput(); | ||
robot::setCmdVel(output.thetaVel, output.xVel); | ||
} | ||
|
||
std::unique_lock autonomousTaskLock(_autonomous_task_mutex); | ||
sleepUntil += 500ms; | ||
// Wait 500ms or return out of while loop if kill called | ||
if (_autonomous_task_cv.wait_until(autonomousTaskLock, sleepUntil, | ||
[&] { return _kill_called; })) { | ||
return; | ||
} | ||
} | ||
} | ||
|
||
void AutonomousTask::kill() { | ||
{ | ||
std::lock_guard lock(_autonomous_task_mutex); | ||
_kill_called = true; | ||
} | ||
|
||
_autonomous_task_cv.notify_all(); | ||
|
||
if (_autonomous_task_thread.joinable()) { | ||
_autonomous_task_thread.join(); | ||
} | ||
} | ||
|
||
} // namespace autonomous |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
#pragma once | ||
|
||
#include "../navtypes.h" | ||
|
||
#include <cmath> | ||
#include <condition_variable> | ||
#include <mutex> | ||
#include <thread> | ||
|
||
namespace autonomous { | ||
|
||
/* | ||
* @brief This class facilitates autonomous navigation to a specified waypoint. | ||
*/ | ||
class AutonomousTask { | ||
public: | ||
/* | ||
* @brief Constructs a new AutonomousTask | ||
*/ | ||
AutonomousTask(); | ||
|
||
/* | ||
* @brief Destructs AutonomousTask object, joins _autonomous_task_thread | ||
*/ | ||
~AutonomousTask(); | ||
|
||
/* | ||
* @brief Starts an autonomous task to the provided waypoint | ||
* | ||
* @param navtypes::pose_t The waypoint to navigate to | ||
*/ | ||
void start(const navtypes::point_t&); | ||
|
||
/* | ||
* @brief Halts autonomous navigation by exiting the navigate() function and joining the | ||
* autonomous task thread | ||
*/ | ||
void kill(); | ||
|
||
private: | ||
/* | ||
* @brief Handles navigation to waypoint, called by start() | ||
*/ | ||
void navigate(); | ||
|
||
navtypes::point_t _waypoint_coords; | ||
std::mutex _autonomous_task_mutex; | ||
std::thread _autonomous_task_thread; | ||
std::condition_variable _autonomous_task_cv; | ||
bool _kill_called; | ||
}; | ||
|
||
} // namespace autonomous |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters