Skip to content

Commit

Permalink
plugin: position_velocity: Initial import.
Browse files Browse the repository at this point in the history
Also it fix ignore mask in setpoint_position.
Issues #33, #61.
  • Loading branch information
vooon committed Jul 24, 2014
1 parent 18cae6f commit a725c91
Show file tree
Hide file tree
Showing 5 changed files with 180 additions and 22 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ add_library(mavros_plugins
src/plugins/setpoint_position.cpp
src/plugins/safety_area.cpp
src/plugins/3dr_radio.cpp
src/plugins/setpoint_velocity.cpp
)
add_dependencies(mavros_plugins
mavros_generate_messages_cpp
Expand Down
5 changes: 4 additions & 1 deletion mavros_plugins.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,16 @@
<description>Send to FCU vision position estimates.</description>
</class>
<class name="setpoint_position" type="mavplugin::SetpointPositionPlugin" base_class_type="mavplugin::MavRosPlugin">
<description>Send to FCU external setpoint.</description>
<description>Send to FCU external position setpoint.</description>
</class>
<class name="safety_area" type="mavplugin::SafetyAreaPlugin" base_class_type="mavplugin::MavRosPlugin">
<description>Send to FCU safety allowed area.</description>
</class>
<class name="3dr_radio" type="mavplugin::TDRRadioPlugin" base_class_type="mavplugin::MavRosPlugin">
<description>Publish 3DR Radio modem status.</description>
</class>
<class name="setpoint_velocity" type="mavplugin::SetpointVelocityPlugin" base_class_type="mavplugin::MavRosPlugin">
<description>Send to FCU external velocity setpoint.</description>
</class>
</library>

60 changes: 60 additions & 0 deletions src/plugins/setpoint_mixin.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/**
* @brief Mixin for setpoint plugins
* @file setpoint_mixin.h
* @author Vladimir Ermakov <vooon341@gmail.com>
*
* @addtogroup plugin
* @{
*/
/*
* Copyright 2014 Vladimir Ermakov.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/

#pragma once

#include <mavros/mavros_plugin.h>

namespace mavplugin {

/**
* @brief This mixin adds local_ned_position_setpoint_extarnal()
*
* @note derived class should provide UAS pointer in uas member.
*/
template <class Derived>
class LocalNEDPositionSetpointExternalMixin {
public:
void local_ned_position_setpoint_external(uint32_t time_boot_ms, uint8_t coordinate_frame,
uint16_t type_mask,
float x, float y, float z,
float vx, float vy, float vz,
float afx, float afy, float afz) {
UAS *_uas = static_cast<Derived *>(this)->uas;
mavlink_message_t msg;
mavlink_msg_local_ned_position_setpoint_external_pack_chan(UAS_PACK_CHAN(_uas), &msg,
time_boot_ms, // why it not usec timestamp?
UAS_PACK_TGT(_uas),
coordinate_frame,
type_mask,
x, y, z,
vz, vy, vz,
afx, afy, afz);
_uas->mav_link->send_message(&msg);
}
};

}; // namespace mavplugin
27 changes: 6 additions & 21 deletions src/plugins/setpoint_position.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,17 @@
#include <tf/transform_listener.h>
#include <geometry_msgs/PoseStamped.h>

#include "setpoint_mixin.h"

namespace mavplugin {

/**
* @brief Setpoint position plugin
*
* Send setpoint positions to FCU controller.
*/
class SetpointPositionPlugin : public MavRosPlugin {
class SetpointPositionPlugin : public MavRosPlugin,
private LocalNEDPositionSetpointExternalMixin<SetpointPositionPlugin> {
public:
SetpointPositionPlugin()
{ };
Expand Down Expand Up @@ -81,6 +84,7 @@ class SetpointPositionPlugin : public MavRosPlugin {
}

private:
friend class LocalNEDPositionSetpointExternalMixin;
UAS *uas;

ros::NodeHandle pos_nh;
Expand All @@ -92,25 +96,6 @@ class SetpointPositionPlugin : public MavRosPlugin {
boost::thread tf_thread;
double tf_rate;

/* -*- low-level send -*- */

void local_ned_position_setpoint_external(uint32_t time_boot_ms, uint8_t coordinate_frame,
uint16_t type_mask,
float x, float y, float z,
float vx, float vy, float vz,
float afx, float afy, float afz) {
mavlink_message_t msg;
mavlink_msg_local_ned_position_setpoint_external_pack_chan(UAS_PACK_CHAN(uas), &msg,
time_boot_ms, // why it not usec timestamp?
UAS_PACK_TGT(uas),
coordinate_frame,
type_mask,
x, y, z,
vz, vy, vz,
afx, afy, afz);
uas->mav_link->send_message(&msg);
}

/* -*- mid-level helpers -*- */

/**
Expand All @@ -127,7 +112,7 @@ class SetpointPositionPlugin : public MavRosPlugin {
/* Documentation start from bit 1 instead 0,
* but implementation PX4 Firmware #1151 starts from 0
*/
uint16_t ignore_all_except_xyz = (4<<6)|(4<<3);
uint16_t ignore_all_except_xyz = (7<<6)|(7<<3);

// TODO: check conversion. Issue #49.
local_ned_position_setpoint_external(stamp.toNSec() / 1000000,
Expand Down
109 changes: 109 additions & 0 deletions src/plugins/setpoint_velocity.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/**
* @brief SetpointVelocity plugin
* @file setpoint_velocity.cpp
* @author Nuno Marques
* @author Vladimir Ermakov <vooon341@gmail.com>
*
* @addtogroup plugin
* @{
*/
/*
* Copyright 2014 Nuno Marques.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/

#include <mavros/mavros_plugin.h>
#include <pluginlib/class_list_macros.h>

#include <geometry_msgs/Twist.h>

#include "setpoint_mixin.h"

namespace mavplugin {

/**
* @brief Setpoint velocity plugin
*
* Send setpoint velocities to FCU controller.
*/
class SetpointVelocityPlugin : public MavRosPlugin,
private LocalNEDPositionSetpointExternalMixin<SetpointVelocityPlugin> {
public:
SetpointVelocityPlugin()
{ };

void initialize(UAS &uas_,
ros::NodeHandle &nh,
diagnostic_updater::Updater &diag_updater)
{
uas = &uas_;
sp_nh = ros::NodeHandle(nh, "setpoint");

//cmd_vel usually is the topic used for velocity control in many controllers / planners
vel_sub = sp_nh.subscribe("cmd_vel", 10, &SetpointVelocityPlugin::vel_cb, this);
}

const std::string get_name() const {
return "SetpointVelocity";
}

const std::vector<uint8_t> get_supported_messages() const {
return { /* Rx disabled */ };
}

void message_rx_cb(const mavlink_message_t *msg, uint8_t sysid, uint8_t compid) {
}

private:
friend class LocalNEDPositionSetpointExternalMixin;
UAS *uas;

ros::NodeHandle sp_nh;
ros::Subscriber vel_sub;

/* -*- mid-level helpers -*- */

/**
* Send velocity to FCU velocity controller
*
* Note: send only VX VY VZ. ENU frame.
*/
void send_setpoint_velocity(float vx, float vy, float vz) {

/* Documentation start from bit 1 instead 0,
* but implementation PX4 Firmware #1151 starts from 0
*/
uint16_t ignore_all_except_v_xyz = (7<<6)|(7<<0);

// TODO: check conversion. Issue #49.
local_ned_position_setpoint_external(ros::Time::now().toNSec() / 1000000,
MAV_FRAME_LOCAL_NED,
ignore_all_except_v_xyz,
0.0, 0.0, 0.0,
vy, vx, -vz,
0.0, 0.0, 0.0);
}

/* -*- callbacks -*- */

void vel_cb(const geometry_msgs::Twist::ConstPtr &req) {
send_setpoint_velocity(req->linear.x, req->linear.y, req->linear.z);
}
};

}; // namespace mavplugin

PLUGINLIB_EXPORT_CLASS(mavplugin::SetpointVelocityPlugin, mavplugin::MavRosPlugin)

0 comments on commit a725c91

Please sign in to comment.