Skip to content

Commit

Permalink
commander handle shutdown command (#8000)
Browse files Browse the repository at this point in the history
  • Loading branch information
jgoppert authored and dagar committed Sep 23, 2017
1 parent d828068 commit 13e64d0
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 51 deletions.
6 changes: 6 additions & 0 deletions src/modules/commander/commander.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4217,6 +4217,12 @@ void *commander_low_prio_loop(void *arg)
/* reboot */
px4_shutdown_request(true, false);

} else if (((int)(cmd.param1)) == 2) {
answer_command(cmd, vehicle_command_s::VEHICLE_CMD_RESULT_ACCEPTED, command_ack_pub);
usleep(100000);
/* shutdown */
px4_shutdown_request(false, false);

} else if (((int)(cmd.param1)) == 3) {
answer_command(cmd, vehicle_command_s::VEHICLE_CMD_RESULT_ACCEPTED, command_ack_pub);
usleep(100000);
Expand Down
93 changes: 42 additions & 51 deletions src/modules/mavlink/mavlink_receiver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,28 @@ MavlinkReceiver::~MavlinkReceiver()
orb_unsubscribe(_control_mode_sub);
}

void MavlinkReceiver::acknowledge(uint8_t sysid, uint8_t compid, uint16_t command, int ret)
{
vehicle_command_ack_s command_ack = {
.timestamp = 0,
.result_param2 = 0,
.command = command,
.result = (ret == PX4_OK ? vehicle_command_ack_s::VEHICLE_RESULT_ACCEPTED : vehicle_command_ack_s::VEHICLE_RESULT_FAILED),
.from_external = false,
.result_param1 = 0,
.target_system = sysid,
.target_component = compid
};

if (_command_ack_pub == nullptr) {
_command_ack_pub = orb_advertise_queue(ORB_ID(vehicle_command_ack), &command_ack,
vehicle_command_ack_s::ORB_QUEUE_LENGTH);

} else {
orb_publish(ORB_ID(vehicle_command_ack), _command_ack_pub, &command_ack);
}
}

void
MavlinkReceiver::handle_message(mavlink_message_t *msg)
{
Expand Down Expand Up @@ -431,11 +453,12 @@ MavlinkReceiver::handle_message_command_long(mavlink_message_t *msg)
bool target_ok = evaluate_target_ok(cmd_mavlink.command, cmd_mavlink.target_system, cmd_mavlink.target_component);

bool send_ack = true;
int ret = 0;
int ret = PX4_OK;

if (!target_ok) {
ret = PX4_ERROR;
goto out;
acknowledge(msg->sysid, msg->compid, cmd_mavlink.command, ret);
return;
}

if (cmd_mavlink.command == MAV_CMD_REQUEST_AUTOPILOT_CAPABILITIES) {
Expand Down Expand Up @@ -508,27 +531,8 @@ MavlinkReceiver::handle_message_command_long(mavlink_message_t *msg)
}
}

out:

if (send_ack) {
vehicle_command_ack_s command_ack = {
.timestamp = 0,
.result_param2 = 0,
.command = cmd_mavlink.command,
.result = (ret == PX4_OK ? vehicle_command_ack_s::VEHICLE_RESULT_ACCEPTED : vehicle_command_ack_s::VEHICLE_RESULT_FAILED),
.from_external = false,
.result_param1 = 0,
.target_system = msg->sysid,
.target_component = msg->compid
};

if (_command_ack_pub == nullptr) {
_command_ack_pub = orb_advertise_queue(ORB_ID(vehicle_command_ack), &command_ack,
vehicle_command_ack_s::ORB_QUEUE_LENGTH);

} else {
orb_publish(ORB_ID(vehicle_command_ack), _command_ack_pub, &command_ack);
}
acknowledge(msg->sysid, msg->compid, cmd_mavlink.command, ret);
}
}

Expand All @@ -542,22 +546,28 @@ MavlinkReceiver::handle_message_command_int(mavlink_message_t *msg)
bool target_ok = evaluate_target_ok(cmd_mavlink.command, cmd_mavlink.target_system, cmd_mavlink.target_component);

bool send_ack = true;
int ret = 0;
int ret = PX4_OK;

if (!target_ok) {
ret = PX4_ERROR;
goto out;
acknowledge(msg->sysid, msg->compid, cmd_mavlink.command, ret);
return;
}

//check for MAVLINK terminate command
if (cmd_mavlink.command == MAV_CMD_PREFLIGHT_REBOOT_SHUTDOWN && ((int)cmd_mavlink.param1) == 10) {
/* This is the link shutdown command, terminate mavlink */
PX4_WARN("terminated by remote");
fflush(stdout);
usleep(50000);
if (cmd_mavlink.command == MAV_CMD_PREFLIGHT_REBOOT_SHUTDOWN) {

int cmd_id = int(cmd_mavlink.param1);

if (cmd_id == 10) {
/* This is the link shutdown command, terminate mavlink */
PX4_WARN("terminated by remote");
fflush(stdout);
usleep(50000);

/* terminate other threads and this thread */
_mavlink->_task_should_exit = true;
/* terminate other threads and this thread */
_mavlink->_task_should_exit = true;
}

} else if (cmd_mavlink.command == MAV_CMD_REQUEST_AUTOPILOT_CAPABILITIES) {
/* send autopilot version message */
Expand Down Expand Up @@ -605,27 +615,8 @@ MavlinkReceiver::handle_message_command_int(mavlink_message_t *msg)
}
}

out:

if (send_ack) {
vehicle_command_ack_s command_ack = {
.timestamp = 0,
.result_param2 = 0,
.command = cmd_mavlink.command,
.result = (ret == PX4_OK ? vehicle_command_ack_s::VEHICLE_RESULT_ACCEPTED : vehicle_command_ack_s::VEHICLE_RESULT_FAILED),
.from_external = false,
.result_param1 = 0,
.target_system = msg->sysid,
.target_component = msg->compid
};

if (_command_ack_pub == nullptr) {
_command_ack_pub = orb_advertise_queue(ORB_ID(vehicle_command_ack), &command_ack,
vehicle_command_ack_s::ORB_QUEUE_LENGTH);

} else {
orb_publish(ORB_ID(vehicle_command_ack), _command_ack_pub, &command_ack);
}
acknowledge(msg->sysid, msg->compid, cmd_mavlink.command, ret);
}
}

Expand Down
1 change: 1 addition & 0 deletions src/modules/mavlink/mavlink_receiver.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ class MavlinkReceiver

private:

void acknowledge(uint8_t sysid, uint8_t compid, uint16_t command, int ret);
void handle_message(mavlink_message_t *msg);
void handle_message_command_long(mavlink_message_t *msg);
void handle_message_command_int(mavlink_message_t *msg);
Expand Down

0 comments on commit 13e64d0

Please sign in to comment.