ros releases messages to control the servo (arduino as the lower machine)

Based on ros, if you can directly control the steering gear, it will be easier if you want to do projects later.

At present, there are generally two ways to control the steering gear with ros:

1. Control the steering gear directly through the steering gear drive board.

2. Control the steering gear by communicating with the lower computer.

The first method is generally applicable to the situation with a servo drive board, such as some serial servos and dedicated servo drives.

These generally have a fixed agreement, which is controlled according to the agreement, and is suitable for situations with a dedicated agreement or a large number of steering gears.

The second method allows the lower computer to control the steering gear through communication with the lower computer. It is necessary to use the io port of the lower computer to directly control the steering gear, which is more suitable for situations where there are not many steering gears.

Today's study is mainly to control a steering gear to understand the following specific operations, and control a steering gear in the second way.

The computer is the upper computer, the arduino uno board is the lower computer, the upper computer publishes messages to the lower computer, and the lower computer subscribes to the news (the news here is mainly the steering gear angle data), according to

The content of the message controls the rotation of the steering gear.

We still use the routines in the serial package for the program of the host computer, and we don't need to program ourselves.

The lower arduino needs our own programming.

1. First download the following program to the arduino uno board:

#include <ros.h>
#include <std_msgs/UInt16.h>
#include <Servo.h>
 
ros::NodeHandle nh;
Servo myservo;  
int servoPin=3;
 
void messageCb(const std_msgs::UInt16& servo_msg){
 
    digitalWrite(13,HIGH);
    myservo.write(servo_msg.data);              // tell servo to go to position in variable 'pos'
    delay(15);
    digitalWrite(13,LOW);
}
 
ros::Subscriber<std_msgs::UInt16> sub("servo_led", &messageCb);
 
void setup()
{
  pinMode(13, OUTPUT);
  myservo.attach(servoPin);
  nh.initNode();
  nh.subscribe(sub);
}
 
void loop()
{
  nh.spinOnce();
  delay(1);
}

You can modify the servoPin variable to change the servo control pin according to your actual situation.

I used the arduino expansion board here to facilitate the wiring of the steering gear (directly plugged in and out, no Dupont cable is required. If not, you can use the Dupont cable to connect), so I chose the No. 3 io port for control.

2. Run the program and publish a message:

roscore

rosrun rosserial_python serial_node.py /dev/ttyACM1

(Bold in black, you can view the specific serial port through the command or view it in the arduino IDE, and you must fill it in correctly)

Release the steering gear corner news:

rostopic pub -1 /servo_led std_msgs/UInt16 30 (the parameter in bold black here is the value of the steering gear)

Every time a new angle is released, press Enter, and then the steering gear will rotate past that angle.

Reference link: https://blog.csdn.net/qq_27806947/article/details/83153934

Guess you like

Origin blog.csdn.net/qqliuzhitong/article/details/114391233