Aerial Robotics IITK
  • Introduction
  • Danger Zone
  • Tutorials
    • Workspace Setup
      • Installing Ubuntu
      • Basic Linux Setup
      • Spruce up your space
      • ROS Setup
      • PX4 Setup
        • PX4 Toolchain Setup
      • Ardupilot Setup
      • Installing Ground Control Station
        • QGroundControl
        • Mission Planner
      • ArduPilot Setup on Docker
      • PX4 Setup on Docker
    • How to Write a ROS Package
      • ROS Package
      • Node Handles, Parameters, and Topics
      • Coding Standards
      • Custom mavros message
      • Transformations
      • Conversions
    • Cheatsheets
      • CMakeCheatsheet
      • GitCheatsheet
      • LatexCheatsheet
      • Markdown Cheatsheet
    • Miscellaneous
      • Odroid XU4 Setup
      • Simulation using Offboard Control
        • Enable Offboard Mode in PX4
      • Writing a UDev rule
      • Sensor fusion
    • Reference wiki links
  • Concepts
    • Quaternions
      • Theory
    • Kalman Filters
    • Rotations
    • Path Planning
      • Grassfire Algorithm
      • Dijkstra Algorithm
      • A* Algorithm
      • Probabilistic Roadmap
      • RRT Algorithm
      • Visibility Graph Analysis
    • Lectures
      • Aerial Robotics
      • Avionics
      • Control Systems: Introduction
      • Control Systems: Models
      • Inter IIT Tech Meet 2018
      • Kalman Filters
      • Linux and Git
      • Git Tutorial
      • ROS
      • Rotorcraft
      • Software Training
  • Control System
    • Model Predictive Control
      • System Identification
      • Sample SysId Launch Files
      • Running MPC
        • MPC with Rotors
        • MPC with PX4 Sim
        • MPC with ROS
      • References
    • PID Controller
      • Introduction
      • Basic Theory
  • Estimation
    • Visual-Inertial Odometry
      • Hardware Requirements
      • Visual-Inertial Sensing
      • DIYing a VI-Sensor
    • Setup with VICON
    • Odometry from pose data
  • Computer Vision
    • Intel RealSense D435i setup for ROS Noetic
    • IntelRealSense D435i Calibration
    • Camera Calibration
    • ArUco ROS
  • Machine Learning
    • Datasets
  • Hardware Integration
    • Configuring Radio Telemetry
    • Setting up RTK + GPS
    • Integration of Sensors with PixHawk
      • Connecting Lidar-lite through I2C
    • Connections
    • Setting up Offboard Mission
      • Setting up Companion Computer
        • Raspberry Pi 4B Setup
        • Jetson TX2 Setup
      • Communication Setup
      • Guided mode
    • Miscellaneous
  • Resources
    • Open-source algorithms and resources
    • Courses
      • State Space Modelling of a Multirotor
      • Path Planning Lecture
      • Introduction to AI in Robotics
      • RRT, RRT* and RRT*- Path Planning Algorithms
    • Useful Reading Links
      • Aerial Robotics
      • Books
      • Computer Vision and Image Processing
      • Courses on AI and Robotics
      • Deep Neural Network
      • Dynamics and Controls system
      • Motion Planning
      • Probabilistic Robotics
      • Programming
      • Robotics Hardware
      • Miscellaneous and Awesome
    • Online Purchase websites
  • Competitions
    • Inter-IIT TechMeet 8.0
    • Inter-IIT TechMeet 9.0
    • IMAV 2019, Madrid, Spain
    • Inter-IIT TechMeet 10.0
    • Inter-IIT TechMeet 11.0
Powered by GitBook
On this page
  • Create custom mavros receiver and sender
  • Inside px4

Was this helpful?

  1. Tutorials
  2. How to Write a ROS Package

Custom mavros message

Create custom mavros receiver and sender

The aim is to send signals to servo connected to pixhawk, using ROS node.

  • In mavros_msgs/msg add custom msg file

GripperServo.msg
time frame_stamp		# Timestamp 
float32 servo_setpoint
  • Create gripper_servo.cpp in the path mavros_extras/src/plugins/. This is the receiver node that listen user input and publish to pixhawk.

gripper_servo.cpp
#include <mavros/mavros_plugin.h>
#include <mavros_msgs/GripperServo.h>

namespace mavros
{
namespace extra_plugins
{
class GripperServoPlugin : public plugin::PluginBase
{
public:
    GripperServoPlugin() : PluginBase(),
                                     status_nh("~gripper_servo")
    {
    }

    void initialize(UAS &uas_)
    {
        PluginBase::initialize(uas_);

        status_sub = status_nh.subscribe("gripper_servo", 10, &GripperServoPlugin::status_cb, this);
    }

    Subscriptions get_subscriptions()
    {
        return {/* Rx disabled */};
    }

private:
    ros::NodeHandle status_nh;
    ros::Subscriber status_sub;

    /**
	 * @param req	received GripperServo msg
	 */
    void status_cb(const mavros_msgs::GripperServo::ConstPtr &req)
    {
        mavlink::common::msg::GRIPPER_SERVO gripper{};

        gripper.servo_setpoint = req->servo_setpoint;
        
        UAS_FCU(m_uas)->send_message_ignore_drop(gripper);
    }
};
} // namespace extra_plugins
} // namespace mavros

#include <pluginlib/class_list_macros.h>
PLUGINLIB_EXPORT_CLASS(mavros::extra_plugins::GripperServoPlugin, mavros::plugin::PluginBase)
  • Edit CMakeList.txt at

add_message_files(
....
GripperServo.msg
....
)
add_library(
...
src/plugins/gripper_servo
.cpp
...
)
  • Edit mavros_plugins.xml inside mavros_extras/mavros_plugins.xml

mavros_plugins.xml

<class name="gripper_servo" type="mavros::extra_plugins::GripperServoPlugin" base_class_type="mavros::plugin::PluginBase">
  	<description>Send servo controls to FCU.</description>
</class>
  • Edit common.xml inside mavlink/message_definitions/v1.0/

common.xml
<message id="229" name="GRIPPER_SERVO">

   <description> Send gripper control generated from off board cobntroller to on board Pixhawk</description>
   <field type="uint64_t" name="time_usec" units="us">Timestamp (synced to UNIX time or since system boot).</field>
   <field type="float" name="servo_setpoint">actuator controls</field>
</message>

Inside px4

  • Edit common.xml inside Firmware/mavlink/include/mavlink/v2.0/message_definitions/

common.xml
 <!--Gripper servo control -->
<message id="229" name="GRIPPER_SERVO">
  <description> Send gripper control generated from off board cobntroller to on board Pixhawk</description>
  <field type="uint64_t" name="time_usec" units="us">Timestamp (synced to UNIX time or since system boot).</field>
  <field type="float" name="servo_setpoint">actuator controls</field>
</message>
  • Remove common and standard directories from Firmware/mavlink/include/mavlink/v2.0

$ python mavgenerate.py
At XML, "Browse" to [Firmware/mavlink/include/mavlink/v2.0/message_definitions/standard.xml].
At Out, "Browse" to [Firmware/mavlink/include/mavlink/v2.0].
Select Language "C"
Select Protocol "2.0"
Check Validate
  • Make uOrb message Firmware/msg/gripper_servo.msg

uint64 timestamp
float32 servo_setpoint
  1. Edit CMakeList.txt inside Firmware/msg/

set(
...
     gripper_servo.msg
     )
  1. Edit mavlink_receiver.h located at Firmware/src/modules/mavlink/

mavlink_receiver.h
 #include <uORB/topics/gripper_servo.h>
.....
class MavlinkReceiver
{
 	void handle_message_gripper_servo(mavlink_message_t *msg);
......
 	orb_advert_t _gripper_servo_pub{nullptr};
}
  1. Edit mavlink_receiver.cpp located at Firmware/src/modules/mavlink/

MavlinkReceiver::handle_message(mavlink_message_t *msg)
{
  switch (msg->msgid) {

	case MAVLINK_MSG_ID_GRIPPER_SERVO:
		 handle_message_gripper_servo(msg);
         break;
        ....
}

add function

void
MavlinkReceiver::handle_message_gripper_servo(mavlink_message_t *msg)
{
    mavlink_gripper_servo_t man;
    mavlink_msg_gripper_servo_decode(msg, &man);

    struct gripper_servo_s key;
    memset(&key, 0, sizeof(key));

    key.timestamp = hrt_absolute_time();
    key.servo_setpoint = man.servo_setpoint;

    if (_gripper_servo_pub == nullptr) {
        _gripper_servo_pub = orb_advertise(ORB_ID(gripper_servo), &key);

    } else {
        orb_publish(ORB_ID(gripper_servo), _gripper_servo_pub, &key);
    }

}
PreviousCoding StandardsNextTransformations

Last updated 5 years ago

Was this helpful?

Generate standard and common directories using mavlink-generator located in ros pkg

mavlink/pymavlink