在ROS下控制dobot(magician)机械手的吸盘

在ROS下控制越疆科技dobot(magician)机械手的吸盘气泵

代码:

#include "ros/ros.h"
#include "ros/console.h"
#include "std_msgs/String.h"
#include "dobot/SetCmdTimeout.h"
#include "dobot/SetQueuedCmdClear.h"
#include "dobot/SetQueuedCmdStartExec.h"
#include "dobot/SetQueuedCmdForceStopExec.h"
#include "dobot/GetDeviceVersion.h"
#include <unistd.h>

#include "dobot/SetEndEffectorParams.h"
#include "dobot/SetPTPJointParams.h"
#include "dobot/SetPTPCoordinateParams.h"
#include "dobot/SetPTPJumpParams.h"
#include "dobot/SetPTPCommonParams.h"
#include "dobot/SetPTPCmd.h"
#include "dobot/SetEndEffectorSuctionCup.h"
#include "dobot/SetHOMEParams.h"
#include "dobot/SetHOMECmd.h"

int main(int argc, char **argv)
{
    ros::init(argc, argv, "DobotClient");
    ros::NodeHandle n;

    ros::ServiceClient client;

    // SetCmdTimeout
    client = n.serviceClient<dobot::SetCmdTimeout>("/DobotServer/SetCmdTimeout");
    dobot::SetCmdTimeout srv1;
    srv1.request.timeout = 3000;
    if (client.call(srv1) == false) {
        ROS_ERROR("Failed to call SetCmdTimeout. Maybe DobotServer isn't started yet!");
        return -1;
    }

    // Clear the command queue
    client = n.serviceClient<dobot::SetQueuedCmdClear>("/DobotServer/SetQueuedCmdClear");
    dobot::SetQueuedCmdClear srv2;
    client.call(srv2);

    // Start running the command queue
    client = n.serviceClient<dobot::SetQueuedCmdStartExec>("/DobotServer/SetQueuedCmdStartExec");
    dobot::SetQueuedCmdStartExec srv3;
    client.call(srv3);

    // Get device version information
    client = n.serviceClient<dobot::GetDeviceVersion>("/DobotServer/GetDeviceVersion");
    dobot::GetDeviceVersion srv4;
    client.call(srv4);
    if (srv4.response.result == 0) {
        ROS_INFO("Device version:%d.%d.%d", srv4.response.majorVersion, srv4.response.minorVersion, srv4.response.revision);
    } else {
        ROS_ERROR("Failed to get device version information!");
    }

    // Set PTP coordinate parameters
    do {
        client = n.serviceClient<dobot::SetPTPCoordinateParams>("/DobotServer/SetPTPCoordinateParams");
        dobot::SetPTPCoordinateParams srv;

        srv.request.xyzVelocity = 100;
        srv.request.xyzAcceleration = 100;
        srv.request.rVelocity = 100;
        srv.request.rAcceleration = 100;
        client.call(srv);
    } while (0);

    // Set PTP common parameters
    do {
        client = n.serviceClient<dobot::SetPTPCommonParams>("/DobotServer/SetPTPCommonParams");
        dobot::SetPTPCommonParams srv;

        srv.request.velocityRatio = 50;
        srv.request.accelerationRatio = 50;
        client.call(srv);
    } while (0);

    do{
        client = n.serviceClient<dobot::SetHOMEParams>("/DobotServer/SetHOMEParams");
        dobot::SetHOMEParams home;
        home.request.x = 200;
        home.request.y = 0;
        home.request.z = 0;
        home.request.r = 0;
        home.request.isQueued = 1;
        client.call(home); 

    } while(0);

    do{
        client = n.serviceClient<dobot::SetHOMECmd>("/DobotServer/SetHOMECmd");
        dobot::SetHOMECmd home1;
        client.call(home1);
    } while(0);

    ros::spinOnce();
    while (ros::ok()){
        client = n.serviceClient<dobot::SetEndEffectorSuctionCup>("/DobotServer/SetEndEffectorSuctionCup");
        dobot::SetEndEffectorSuctionCup sck1;   
        do{
            sck1.request.enableCtrl = 1; // When enableCtr == 1 the motor will operate. 
            sck1.request.suck = 1; // When suck == 1 the suction cup will suck.
            sck1.request.isQueued = true; // This command puts the request in the queue.
            ros::spinOnce(); 
        } while(0);

        client = n.serviceClient<dobot::SetPTPCmd>("/DobotServer/SetPTPCmd");
        dobot::SetPTPCmd srv;
        // The first point
        do{
            srv.request.ptpMode = 1;
            srv.request.x = 200;
            srv.request.y = 0;
            srv.request.z = 0;
            srv.request.r = 0;
            client.call(srv);

             if (srv.response.result == 0) {
                break; 
            }     
            ros::spinOnce(); //spinOnce() will execute any callbacks.
            if (ros::ok() == false) {
                break;
            }

        }while(0);

        do{
        
            srv.request.ptpMode = 1;
            srv.request.x = 250;
            srv.request.y = 0;
            srv.request.z = 0;
            srv.request.r = 0;
            client.call(srv);

            if (srv.response.result == 0) {
                break; 
            }     
            ros::spinOnce(); //spinOnce() will execute any commands in the queue.
            if (ros::ok() == false) {
                break;
            }
        } while(0);

        client = n.serviceClient<dobot::SetEndEffectorSuctionCup>("/DobotServer/SetEndEffectorSuctionCup");
        dobot::SetEndEffectorSuctionCup sck2;
        // pick up 
        do{
            sck2.request.enableCtrl = 1;
            sck2.request.suck = 1; // Enable suction 
            sck2.request.isQueued = true;
            client.call(sck2);

            ros::spinOnce(); //spinOnce() will execute any commands in the queue.
            if (ros::ok() == false) {
                break;
            }
        } while(0);
        client = n.serviceClient<dobot::SetPTPCmd>("/DobotServer/SetPTPCmd");
        dobot::SetPTPCmd srv6;
        do{
            srv6.request.ptpMode = 1;
            srv6.request.x = 250;
            srv6.request.y = 0;
            srv6.request.z = -45;
            srv6.request.r = 0;
            client.call(srv6);

             if (srv6.response.result == 0) {
                break; 
            }     
            ros::spinOnce(); //spinOnce() will execute any commands in the queue.
            if (ros::ok() == false) {
                break;
            }

        }while(0);

        do{

            srv6.request.ptpMode = 1;
            srv6.request.x = 250;
            srv6.request.y = 50;
            srv6.request.z = 0;
            srv6.request.r = 0;
            client.call(srv6);

             if (srv6.response.result == 0) {
                break; 
            }     
            ros::spinOnce(); //spinOnce() will execute any commands in the queue.
            if (ros::ok() == false) {
                break;
            }
        }while(0);

        do{

            srv6.request.ptpMode = 1;
            srv6.request.x = 200;
            srv6.request.y = 50;
            srv6.request.z = -45;
            srv6.request.r = 0;
            client.call(srv6);

            if (srv6.response.result == 0) {
                break; 
            }     
            ros::spinOnce(); //spinOnce() will execute any commands in the queue.
            if (ros::ok() == false) {
                break;
            }

        } while(0);

        client = n.serviceClient<dobot::SetEndEffectorSuctionCup>("/DobotServer/SetEndEffectorSuctionCup");
        dobot::SetEndEffectorSuctionCup sck3;
        //drop off 
        do{    
            sck3.request.enableCtrl = 1;
            sck3.request.suck = 0;
            sck3.request.isQueued = true;
            client.call(sck3);

            ros::spinOnce(); //spinOnce() will execute any commands in the queue.
            if (ros::ok() == false) {
                break;
            }
        }while (0);
        ros::spinOnce();

    }
    
    return 0;
}

 

  • 4
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
ROS控制阿克曼转向底盘,可以使用现成的ros-ackermann-msgs和ros-ackermann-drive软件包来实现。以下是基于这两个软件包的阿克曼转向底盘控制程序示例: 1. 安装ros-ackermann-msgs和ros-ackermann-drive软件包 ```bash sudo apt-get install ros-melodic-ackermann-msgs ros-melodic-ackermann-drive ``` 2. 编写控制程序 ```python #!/usr/bin/env python import rospy from ackermann_msgs.msg import AckermannDriveStamped def ackermann_drive(speed, steering_angle): # 初始化AckermannDriveStamped消息 ackermann_cmd = AckermannDriveStamped() # 设置线速度和转向角度 ackermann_cmd.drive.speed = speed ackermann_cmd.drive.steering_angle = steering_angle # 发布AckermannDriveStamped消息 pub.publish(ackermann_cmd) if __name__ == '__main__': try: # 初始化ROS节点 rospy.init_node('ackermann_controller') # 创建AckermannDriveStamped消息发布者 pub = rospy.Publisher('/ackermann_cmd', AckermannDriveStamped, queue_size=10) # 设置速度和转向角度 speed = 1.0 # 线速度 steering_angle = 0.2 # 转向角度 # 循环发送AckermannDriveStamped消息 rate = rospy.Rate(10) while not rospy.is_shutdown(): ackermann_drive(speed, steering_angle) rate.sleep() except rospy.ROSInterruptException: pass ``` 这个程序使用了ros-ackermann-msgs和ros-ackermann-drive软件包来控制车辆的阿克曼转向底盘。在程序中,创建了一个AckermannDriveStamped消息发布者,通过设置线速度和转向角度来控制车辆的运动。同时,程序还需要在ROS中设置好运动控制相关的参数,如车辆的轴距、转弯半径、线速度和角速度等。通过这个程序,可以实现对阿克曼转向底盘的控制

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值