Skip to content
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions configs/dev.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
},
"pathing": {
"laps": 2,
"upload_distance_buffer_m": 50.0,
"dubins": {
"turning_radius": 30.0,
"point_separation": 20.0
Expand Down
1 change: 1 addition & 0 deletions configs/jetson.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
},
"pathing": {
"laps": 10,
"upload_distance_buffer_m": 50.0,
"dubins": {
"turning_radius": 5.0,
"point_separation": 20.0
Expand Down
5 changes: 5 additions & 0 deletions include/core/mission_state.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ class MissionState {
void setInitPath(const MissionPath& init_path);
MissionPath getInitPath();

void setNextWaypointPath(const MissionPath& next_waypoint_path);
MissionPath getNextWaypointPath();

void setCoveragePath(const MissionPath& coverage_path);
MissionPath getCoveragePath();

Expand Down Expand Up @@ -130,6 +133,8 @@ class MissionState {

std::mutex init_path_mut; // for reading/writing the initial path
MissionPath init_path;
std::mutex next_waypoint_path_mut; // for reading/writing the next waypoint path
MissionPath next_waypoint_path;
std::mutex coverage_path_mut; // for reading/writing the coverage path
MissionPath coverage_path;
std::mutex airdrop_path_mut;
Expand Down
6 changes: 3 additions & 3 deletions include/network/mavlink.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ class MavlinkClient {
mavsdk::Telemetry::RcStatus get_conn_status();
bool armAndHover(std::shared_ptr<MissionState> state);
bool startMission();
bool clearMission();

/*
* Triggers a relay on the ArduPilot
Expand All @@ -92,10 +93,9 @@ class MavlinkClient {
*/
bool triggerRelay(int relay_number, bool state);

// Safety Functions
void KILL_THE_PLANE_DO_NOT_CALL_THIS_ACCIDENTALLY();

// rtl
void rtl();
void returnToLaunch();

private:
mavsdk::Mavsdk mavsdk;
Expand Down
21 changes: 18 additions & 3 deletions include/pathing/static.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -302,11 +302,26 @@ class AirdropApproachPathing {
*/
RRTPoint getCurrentLoc(std::shared_ptr<MissionState> state);

MissionPath generateInitialPath(std::shared_ptr<MissionState> state);
/**
* Calculates the approximate angle of exit out of a waypoint path
*
* @param path ==> the waypoint path
* @param state ==> mission state
* @return angle of exit (+x-axis is 0 deg, ccw is positive)
*/
double calculateFinalAngle(const MissionPath& path,
const std::optional<CartesianConverter<GPSProtoVec>> cartesianConverter);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cartesianConverter can be a reference to avoid a copy


std::vector<GPSCoord> generateInitialPath(std::shared_ptr<MissionState> state);

std::vector<GPSCoord>
generateNextWaypointPath(std::shared_ptr<MissionState> state, double start_angle);

MissionPath generateSearchPath(std::shared_ptr<MissionState> state);
std::vector<GPSCoord>
generateSearchPath(std::shared_ptr<MissionState> state, double start_angle);

MissionPath generateAirdropApproach(std::shared_ptr<MissionState> state, const GPSCoord &goal);
std::vector<GPSCoord>
generateAirdropApproach(std::shared_ptr<MissionState> state, const GPSCoord &goal);
Comment thread
AskewParity marked this conversation as resolved.

std::pair<double, double> estimateAreaCoveredAndPathLength(const std::vector<XYZCoord> &goals,
const Environment &airspace);
Expand Down
3 changes: 1 addition & 2 deletions include/ticks/path_gen.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ class PathGenTick : public Tick {
void init() override;
Tick* tick() override;
private:
std::future<MissionPath> init_path;
std::future<MissionPath> coverage_path;
std::future<void> paths_future;

void startPathGeneration();
};
Expand Down
1 change: 1 addition & 0 deletions include/utilities/obc_config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ struct AirdropApproachConfig {

struct PathingConfig {
int laps;
double upload_distance_buffer_m;
DubinsConfig dubins;
RRTConfig rrt;
AirdropCoverageConfig coverage;
Expand Down
10 changes: 10 additions & 0 deletions src/core/mission_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,16 @@ MissionPath MissionState::getInitPath() {
return this->init_path;
}

void MissionState::setNextWaypointPath(const MissionPath& next_waypoint_path) {
Lock lock(this->next_waypoint_path_mut);
this->next_waypoint_path = next_waypoint_path;
}

MissionPath MissionState::getNextWaypointPath() {
Lock lock(this->next_waypoint_path_mut);
return this->next_waypoint_path;
}

void MissionState::setCoveragePath(const MissionPath& coverage_path) {
Lock lock(this->coverage_path_mut);
this->coverage_path = coverage_path;
Expand Down
2 changes: 1 addition & 1 deletion src/network/gcs_routes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -573,7 +573,7 @@ DEF_GCS_HANDLE(Post, camera, runpipeline) {

DEF_GCS_HANDLE(Post, rtl) {
LOG_REQUEST("POST", "/rtl");
state->getMav()->rtl();
state->getMav()->returnToLaunch();
LOG_RESPONSE(INFO, "RTL activated", OK);
}

Expand Down
19 changes: 17 additions & 2 deletions src/network/mavlink.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ MavlinkClient::MavlinkClient(OBCConfig config)
});

this->telemetry->subscribe_heading([this](mavsdk::Telemetry::Heading heading) {
VLOG_F(DEBUG, "Heading: %d", heading.heading_deg);
VLOG_F(DEBUG, "Heading: %f deg", heading.heading_deg);
Lock lock(this->data_mut);
this->data.heading_deg = heading.heading_deg;
});
Expand Down Expand Up @@ -511,12 +511,27 @@ bool MavlinkClient::startMission() {
return true;
}

/**
* Clears the existng mission
*/
bool MavlinkClient::clearMission() {
LOG_F(INFO, "Sending clear mission command");
auto start_result = this->mission->clear_mission();
if (start_result != mavsdk::MissionRaw::Result::Success) {
LOG_S(ERROR) << "FAIL: Mission could not be cleared " << start_result;
return false;
}

LOG_F(INFO, "Mission Cleared!");
return true;
}

void MavlinkClient::KILL_THE_PLANE_DO_NOT_CALL_THIS_ACCIDENTALLY() {
LOG_F(ERROR, "KILLING THE PLANE: SETTING AFS_TERMINATE TO 1");
auto result = this->param->set_param_int("AFS_TERMINATE", 1);
LOG_S(ERROR) << "KILL RESULT: " << result;
}

void MavlinkClient::rtl() {
void MavlinkClient::returnToLaunch() {
this->action->return_to_launch();
}
74 changes: 65 additions & 9 deletions src/pathing/static.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,23 @@ RRTPoint getCurrentLoc(std::shared_ptr<MissionState> state) {
return RRTPoint(start_xyz, start_angle);
}

MissionPath generateInitialPath(std::shared_ptr<MissionState> state) {
double calculateFinalAngle(
const MissionPath& path,
const std::optional<CartesianConverter<GPSProtoVec>> cartesianConverter) {

const auto& coords = path.get();
if (coords.size() < 2) {
return 0.0; // should probably either have angle between now and point
// or use assert for force size >=2
}

XYZCoord pt1 = cartesianConverter->toXYZ(coords[coords.size() - 2]);
XYZCoord pt2 = cartesianConverter->toXYZ(coords[coords.size() - 1]);
Comment on lines +626 to +627

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should cartesianConverter be optional at all? By the time PathGenTick is running the mission upload must have occured, so the converter should exist.

I think its fine to keep it optional for our sanity, but then we should check the optional has a value before dereferencing it here


return std::atan2(pt2.y - pt1.y, pt2.x - pt1.x);
}

std::vector<GPSCoord> generateInitialPath(std::shared_ptr<MissionState> state) {
// first waypoint is start

// the other waypoitns is the goals
Expand Down Expand Up @@ -653,13 +669,53 @@ MissionPath generateInitialPath(std::shared_ptr<MissionState> state) {
output_coords.push_back(state->getCartesianConverter()->toLatLng(waypoint));
}

return MissionPath(MissionPath::Type::FORWARD, output_coords);
return output_coords;
}

std::vector<GPSCoord>
generateNextWaypointPath(std::shared_ptr<MissionState> state, double start_angle) {
if (state->mission_params.getWaypoints().size() < 1) {
loguru::set_thread_name("Static Pathing");
LOG_F(ERROR, "Not enough waypoints to generate a path, requires >=1, num waypoints: %s",
std::to_string(state->mission_params.getWaypoints().size()).c_str());
return {};
}

std::vector<XYZCoord> goals = state->mission_params.getWaypoints();

if (state->config.pathing.rrt.generate_deviations) {
Environment mapping_bounds(state->mission_params.getAirdropBoundary(), {}, {}, goals, {});
goals = generateRankedNewGoalsList(goals, mapping_bounds)[0];
}

RRTPoint start(goals.back(), start_angle);

// add buffer to the start point so that we dont loopty loop
double buffer_m = state->config.pathing.upload_distance_buffer_m;
if (buffer_m > 0.0) {
start.coord.x += buffer_m * std::cos(start_angle);
start.coord.y += buffer_m * std::sin(start_angle);
}

RRT rrt(start, goals, SEARCH_RADIUS, state->mission_params.getFlightBoundary(), state->config,
{}, {});

rrt.run();

std::vector<XYZCoord> path = rrt.getPointsToGoal();

std::vector<GPSCoord> output_coords;
for (const XYZCoord &waypoint : path) {
output_coords.push_back(state->getCartesianConverter()->toLatLng(waypoint));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unsafe optional reference with cartesianConverter (i feel like i have something against cartesian converter or smth idk why i keep spotting these

}

return output_coords;
}

MissionPath generateSearchPath(std::shared_ptr<MissionState> state) {
std::vector<GPSCoord> generateSearchPath(std::shared_ptr<MissionState> state, double start_angle) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing a bounds check for the waypoints? I might be misinterpreting it but why is there a bounds check in generateNextWaypointPath to check that we have at least 1 waypoint but not here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The number of waypoints must be greater than 1 for the code to execute generate SearchPath.

std::vector<GPSCoord> gps_coords;
if (state->config.pathing.coverage.method == AirdropCoverageMethod::Enum::FORWARD) {
RRTPoint start(state->mission_params.getWaypoints().back(), 0);
RRTPoint start(state->mission_params.getWaypoints().back(), start_angle);
double scan_radius = state->config.pathing.coverage.camera_vision_m;

ForwardCoveragePathing pathing(start, scan_radius,
Expand All @@ -670,19 +726,19 @@ MissionPath generateSearchPath(std::shared_ptr<MissionState> state) {
gps_coords.push_back(state->getCartesianConverter()->toLatLng(coord));
}

return MissionPath(MissionPath::Type::FORWARD, gps_coords);
return gps_coords;
} else { // hover
HoverCoveragePathing pathing(state);

for (const auto &coord : pathing.run()) {
gps_coords.push_back(state->getCartesianConverter()->toLatLng(coord));
}
return MissionPath(MissionPath::Type::HOVER, gps_coords,
state->config.pathing.coverage.hover.hover_time_s);
return gps_coords;
}
}

MissionPath generateAirdropApproach(std::shared_ptr<MissionState> state, const GPSCoord &goal) {
std::vector<GPSCoord>
generateAirdropApproach(std::shared_ptr<MissionState> state, const GPSCoord &goal) {
std::shared_ptr<MavlinkClient> mav = state->getMav();
/*
Note: this function was neutered right before we attempted to fly at the 2024 competition
Expand Down Expand Up @@ -725,5 +781,5 @@ MissionPath generateAirdropApproach(std::shared_ptr<MissionState> state, const G
// gps_path.push_back(goal);
// gps_path.push_back(goal);

return MissionPath(MissionPath::Type::FORWARD, gps_path);
return gps_path;
}
3 changes: 2 additions & 1 deletion src/ticks/airdrop_prep.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ Tick* AirdropPrepTick::tick() {
target.coordinate().latitude(), target.coordinate().longitude(),
target.coordinate().altitude());

state->setAirdropPath(generateAirdropApproach(state, target.coordinate()));
state->setAirdropPath(MissionPath(MissionPath::Type::FORWARD,
generateAirdropApproach(state, target.coordinate())));

LOG_F(INFO, "Generated approach path");

Expand Down
17 changes: 1 addition & 16 deletions src/ticks/fly_search.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,32 +23,17 @@ std::chrono::milliseconds FlySearchTick::getWait() const {
}

void FlySearchTick::init() {
// TODO: can we delete the following camera lines?
Comment thread
AskewParity marked this conversation as resolved.
Outdated
this->state->getCamera()->startStreaming();
this->airdrop_boundary = this->state->mission_params.getAirdropBoundary();
this->last_photo_time = getUnixTime_ms();

// note: I didn't get around to testing if 1 would be a better value than 0
// to see if the mission start can be forced.
if (!this->state->getMav()->setMissionItem(1)) {
LOG_F(ERROR, "Failed to reset Mission");
}

this->mission_started = this->state->getMav()->startMission();

// I have another one here because idk how startmIssion behaves exactly
if (!this->state->getMav()->setMissionItem(1)) {
LOG_F(ERROR, "Failed to reset Mission");
}

LOG_F(INFO, "Total Waypoint #: %zu", this->state->getMav()->totalWaypoints());
}

Tick* FlySearchTick::tick() {
if (!this->mission_started) {
this->mission_started = this->state->getMav()->startMission();
return nullptr;
}

Comment thread
AskewParity marked this conversation as resolved.
bool isMissionFinished = state->getMav()->isMissionFinished();

if (isMissionFinished) {
Expand Down
39 changes: 2 additions & 37 deletions src/ticks/fly_waypoints.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,9 @@ FlyWaypointsTick::FlyWaypointsTick(std::shared_ptr<MissionState> state, Tick* ne
: Tick(state, TickID::FlyWaypoints), next_tick(next_tick) {}

void FlyWaypointsTick::init() {
// note: I didn't get around to testing if 1 would be a better value than 0
// to see if the mission start can be forced.
if (!this->state->getMav()->setMissionItem(1)) {
LOG_F(ERROR, "Failed to reset Mission");
}

this->mission_started = this->state->getMav()->startMission();
state->getCamera()->startStreaming();
this->last_photo_time = getUnixTime_ms();

// I have another one here because idk how startmIssion behaves exactly
if (!this->state->getMav()->setMissionItem(1)) {
LOG_F(ERROR, "Failed to reset Mission");
}

state->decrementLapsRemaining();

LOG_F(INFO, "Started FlyWaypointsTick, Laps Remaining: %d", state->getLapsRemaining());
}

Expand Down Expand Up @@ -80,32 +67,10 @@ Tick* FlyWaypointsTick::tick() {
return nullptr;
}


if (state->getLapsRemaining() > 0) {
// regenerate path
std::future<MissionPath> init_path;
init_path = std::async(std::launch::async, generateInitialPath, this->state);
auto init_status = init_path.wait_for(std::chrono::milliseconds(0));
int count_ms = 2500;
const int wait_time_ms = 100;

while (init_status != std::future_status::ready && count_ms > 0) {
LOG_F(WARNING, "Waiting for path to be generated...");
std::this_thread::sleep_for(std::chrono::milliseconds(wait_time_ms));
init_status = init_path.wait_for(std::chrono::milliseconds(0));
count_ms -= wait_time_ms;
}

if (count_ms <= 0) {
LOG_F(ERROR, "Path generation took too long. Trying Again...");
return nullptr;
}

state->setInitPath(init_path.get());

return new MavUploadTick(
this->state, new FlyWaypointsTick(this->state, new FlySearchTick(this->state)),
state->getInitPath(), false);
state->getNextWaypointPath(), false);
}

return new MavUploadTick(
Expand Down
1 change: 1 addition & 0 deletions src/ticks/mav_upload.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ std::chrono::milliseconds MavUploadTick::getWait() const {
}

void MavUploadTick::init() {
this->state->getMav()->clearMission();
this->mav_uploaded = std::async(std::launch::async,
&MavlinkClient::uploadMissionUntilSuccess,
this->state->getMav(),
Expand Down
Loading