-
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 * fix autonomous: SQUASH ME * pretty much working + squash me * Add message validation back in * Run clang-format * Use correct json message names * MissionControl: Accept both floats and unsigned numbers in autonomous JavaScript truncates any value such as 0.0 or 1.0 to 0 and 1, respectively. This fails validation because we looks for floats and those come across as unsigned ints. To fix this, we check if the incoming number is either a float or an unsigned number. Both are accepted. * Change rot order * format * wip: Debugging and whatnot * Improve logging, add slow distance threshold to dtwcommand * Move autonomous constants to Constants.cpp to match new impl * format * Remove extraneous logging --------- Co-authored-by: Abhay D <[email protected]>
- Loading branch information
1 parent
862ae89
commit 8902619
Showing
16 changed files
with
310 additions
and
108 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
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,84 @@ | ||
#include "AutonomousTask.h" | ||
|
||
#include "../Constants.h" | ||
#include "../commands/DriveToWaypointCommand.h" | ||
#include "../utils/transform.h" | ||
#include "../world_interface/world_interface.h" | ||
|
||
#include <loguru.hpp> | ||
|
||
using namespace std::chrono_literals; | ||
using namespace Constants::autonomous; | ||
|
||
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_THRESHOLD, DONE_THRESHOLD, | ||
CLOSE_TO_TARGET_DUR_VAL); | ||
|
||
DiffDriveKinematics diffDriveKinematics(Constants::EFF_WHEEL_BASE); | ||
|
||
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)) { | ||
LOG_SCOPE_F(INFO, "AutoNav"); | ||
auto gpsData = latestGPS.getData(); | ||
navtypes::pose_t latestPos(gpsData.x(), gpsData.y(), latestHeading.getData()); | ||
cmd.setState(latestPos, robot::types::dataclock::now()); | ||
commands::command_t output = cmd.getOutput(); | ||
auto scaledVels = diffDriveKinematics.ensureWithinWheelSpeedLimit( | ||
DiffDriveKinematics::PreferredVelPreservation::PreferThetaVel, output.xVel, | ||
output.thetaVel, Constants::MAX_WHEEL_VEL); | ||
navtypes::point_t relTarget = util::toTransform(latestPos) * _waypoint_coords; | ||
LOG_F(INFO, "Relative Target: (%lf, %lf)", relTarget(0), relTarget(1)); | ||
LOG_F(INFO, "thetaVel: %lf", scaledVels(2)); | ||
LOG_F(INFO, "xVel: %lf", scaledVels(0)); | ||
robot::setCmdVel(scaledVels(2), scaledVels(0)); | ||
} | ||
|
||
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
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
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
Oops, something went wrong.