# Pixhawk PWM control for rovers with MAVROS/MAVLink

**URL:** <https://discuss.px4.io/t/pixhawk-pwm-control-for-rovers-with-mavros-mavlink/9985>\
**Category:** Pixhawk\
**Created:** [March 14, 2019, 12:08pm UTC](https://discuss.px4.io/t/pixhawk-pwm-control-for-rovers-with-mavros-mavlink/9985 "2019-03-14T12:08:17Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rappo](https://discuss.px4.io/letter_avatar_proxy/v4/letter/r/bc79bd/32.png) [@Rappo](https://discuss.px4.io/u/Rappo)\
**Post date:** [March 14, 2019, 12:08pm UTC](https://discuss.px4.io/t/pixhawk-pwm-control-for-rovers-with-mavros-mavlink/9985/1 "2019-03-14T12:08:17Z")

</div>

Hello everyone, I am currently try to control a Turtlebot3 rover by setting the right values of the PWM outputs on the pixhawk autopilot using MAVROS and MAVLink protocol from my laptop.  
My goal is to make the rover move by choosing the values for the PWM signals on pins 2 and 3 (steering and throttle) that will then be traduced by the OPENCR board of the Turtlebot that controls the motors.  
The Pixhawk is rightly connected and set in OFFBOARD mode (i used QGroundControl to upload the Rover firmware on the autopilot).  
I wrote a script (control\_actuator.cpp) that uses set\_actuator\_control messages and sets the values for the pins (the script is available at the end of this message).This procedure is similar to every other procedure i found online that regards this topic.  
The commands i use for the whole procedures are the following:

-)roslaunch mavros px4.launch

-)rosrun mavros mavsafety arm

-)then i run a script to set the OFFBOARD mode (offb\_node.cpp, found at the end of the message…I have to use this script since the command “rosrun mavros mavsys mode -c OFFBOARD” it doesn’t work for some reason).

At this point i run the control\_actuator script and if i check the values with “rostopic echo mavros/actuator\_control” i can see the right numbers for the pins i am interested in.  
However the pixhawk outputs are not working accordingly since no PWM signal is present in any output.  
Does anyone know how can I fix this?  
Thanks you all in advance, let me know if you need other information in order to better understand my problem.

CONTROL\_ACTUATOR.CPP:

#include ros/ros.h  
#include mavros\_msgs/ActuatorControl.h

mavros\_msgs::ActuatorControl actuator\_control\_msg;

void set\_actuator\_control(){   
actuator\_control\_msg.header.stamp = ros::Time::now();  
actuator\_control\_msg.group\_mix = 0;  
actuator\_control\_msg.controls[0] = 0;  
actuator\_control\_msg.controls[1] = 0;  
actuator\_control\_msg.controls[2] = 0.0;  
actuator\_control\_msg.controls[3] = 1.0; //here i try to set throttle pin to 1  
actuator\_control\_msg.controls[4] = 0.0;  
actuator\_control\_msg.controls[5] = 0.0;  
actuator\_control\_msg.controls[6] = 0.0;  
actuator\_control\_msg.controls[7] = 0.0;  
}

int main(int argc, char\*\* argv)  
{

ros::init(argc, argv, “rover\_teleop”);  
ros::NodeHandle n;  
int rate = 10;  
ros::Rate loop\_rate(10);  
//ros::Publisher actuator\_controls\_pub = n.advertise\<mavros\_msgs::ActuatorControl("/mavros/actuator\_control", 100);  
ros::Publisher actuator\_controls\_pub = n.advertise\<mavros\_msgs::ActuatorControl\>("/mavros/actuator\_control", 1000);

while (ros::ok())  
{  
set\_actuator\_control();   
actuator\_controls\_pub.publish(actuator\_control\_msg);

```
   ros::spinOnce();
   loop_rate.sleep();
}

```

}

OFFB\_NODE.CPP:

#include ros/ros.h  
#include geometry\_msgs/PoseStamped.h  
#include mavros\_msgs/CommandBool.h  
#include mavros\_msgs/SetMode.h  
#include mavros\_msgs/State.h

mavros\_msgs::State current\_state;  
void state\_cb(const mavros\_msgs::State::ConstPtr& msg){  
current\_state = \*msg;  
}

int main(int argc, char \*\*argv)  
{  
ros::init(argc, argv, “offb\_node”);  
ros::NodeHandle nh;

```
ros::Subscriber state_sub = nh.subscribe<mavros_msgs::State>
        ("mavros/state", 10, state_cb);
ros::Publisher local_pos_pub = nh.advertise<geometry_msgs::PoseStamped>
        ("mavros/setpoint_position/local", 10);
ros::ServiceClient arming_client = nh.serviceClient<mavros_msgs::CommandBool>
        ("mavros/cmd/arming");
ros::ServiceClient set_mode_client = nh.serviceClient<mavros_msgs::SetMode>
        ("mavros/set_mode");

//the setpoint publishing rate MUST be faster than 2Hz
ros::Rate rate(20.0);

// wait for FCU connection
while(ros::ok() && current_state.connected){
    ros::spinOnce();
    rate.sleep();
}

geometry_msgs::PoseStamped pose;
pose.pose.position.x = 0;
pose.pose.position.y = 0;
pose.pose.position.z = 2;

//send a few setpoints before starting
for(int i = 100; ros::ok() && i > 0; --i){
    local_pos_pub.publish(pose);
    ros::spinOnce();
    rate.sleep();
}

mavros_msgs::SetMode offb_set_mode;
offb_set_mode.request.custom_mode = "OFFBOARD";

mavros_msgs::CommandBool arm_cmd;
arm_cmd.request.value = true;

ros::Time last_request = ros::Time::now();

while(ros::ok()){
    if( current_state.mode != "OFFBOARD" &&
        (ros::Time::now() - last_request > ros::Duration(5.0))){
        if( set_mode_client.call(offb_set_mode) &&
            offb_set_mode.response.mode_sent){
            ROS_INFO("Offboard enabled");
        }
        last_request = ros::Time::now();
    } else {
        if( !current_state.armed &&
            (ros::Time::now() - last_request > ros::Duration(5.0))){
            if( arming_client.call(arm_cmd) &&
                arm_cmd.response.success){
                ROS_INFO("Vehicle armed");
            }
            last_request = ros::Time::now();
        }
    }

    local_pos_pub.publish(pose);

    ros::spinOnce();
    rate.sleep();
}

return 0;

```

}

---

<div class="post-metadata">

**Author:** ![JulianOes](https://discuss.px4.io/user_avatar/discuss.px4.io/julianoes/32/30_2.png) [@JulianOes](https://discuss.px4.io/u/JulianOes)\
**Post date:** [March 15, 2019, 8:28am UTC](https://discuss.px4.io/t/pixhawk-pwm-control-for-rovers-with-mavros-mavlink/9985/2 "2019-03-15T08:28:57Z")

</div>

> i used QGroundControl to upload the Rover firmware on the autopilot

Hm, did you by chance flash ArduRover? Or which firmware exactly did you flash?

I’m guessing here but I think if you just want to use “offboard control” straight with actuator controls, you could use the PX4 firmware and then get it armed and into offboard mode using ROS as you’re doing it. For that the firmware itself is just a passthrough and not really a rover firmware or a plane firmware.

This forum is for PX4, so I don’t know what the possibilities are with ArduRover and ROS. You’d have to check the ArduPilot forums and docs.
