zoukankan      html  css  js  c++  java
  • [机器人仿真软件(二)]C++控制V-REP中的Baxter机器人简单例子

    注:本文所提供的例子是在路径V-REP_PRO_EDUprogrammingubbleRobClient下提供的代码基础上修改的,在其他路径下使用时需更改代码中的头文件,库,代码中的路径等。

    一.程序的效果

     本例子将实现C++程序不断读取存储在文本文件中的关节角信息,然后通过socket通信传输给V-REP中的Baxter机器人,实现简单的位置控制功能。

    二.V-REP端

    在Baxter机器人模型的左臂,右臂分别添加以下的语句:

    simExtRemoteApiStart(19998)

    simExtRemoteApiStart(19999)

    三.C++代码

    1.Socket的建立:

        int clientID = simxStart((simxChar*)"127.0.0.1", 19999, true, true, 2000, 5);
        int clientID1 = simxStart((simxChar*)"127.0.0.1", 19998, true, true, 2000, 5);

    2.对各个关节的获取:

    simxGetObjectHandle(clientID, "Baxter_rightArm_joint1", &Baxter_rightArm_joint1, simx_opmode_oneshot_wait);
    .....................................//包括了14个关节,不知能否用循环语句实现
    simxGetObjectHandle(clientID1, "Baxter_leftArm_joint7", &Baxter_leftArm_joint7, simx_opmode_oneshot_wait);

    3.读取文本,同时传输关节角:

    simxSetJointTargetPosition(clientID, Baxter_rightArm_joint1, x1, simx_opmode_oneshot);  //x1表示要传输的关节角
    .....................................//同样包括了14个关节
     simxSetJointTargetPosition(clientID1, Baxter_leftArm_joint7, 0, simx_opmode_oneshot);

    4.全部的代码

    #include <stdio.h>
    #include <stdlib.h>
    #include <iostream>  
    #include <iomanip>  
    #include <fstream> 
    #include <sstream>
    #include <string> 
    #include<windows.h>
    
    extern "C" {
        #include "extApi.h"
    }
    
    int main(int argc,char* argv[])
    {
        int clientID = simxStart((simxChar*)"127.0.0.1", 19999, true, true, 2000, 5);
        int clientID1 = simxStart((simxChar*)"127.0.0.1", 19998, true, true, 2000, 5);
        if (clientID != -1 || clientID1 != -1)
        {
            printf("Connected to remote API server
    ");
    
            int Baxter_rightArm_joint1;
            int Baxter_rightArm_joint2;
            int Baxter_rightArm_joint3;
            int Baxter_rightArm_joint4;
            int Baxter_rightArm_joint5;
            int Baxter_rightArm_joint6;
            int Baxter_rightArm_joint7;
    
            int Baxter_leftArm_joint1;
            int Baxter_leftArm_joint2;
            int Baxter_leftArm_joint3;
            int Baxter_leftArm_joint4;
            int Baxter_leftArm_joint5;
            int Baxter_leftArm_joint6;
            int Baxter_leftArm_joint7;
    
            simxGetObjectHandle(clientID, "Baxter_rightArm_joint1", &Baxter_rightArm_joint1, simx_opmode_oneshot_wait);
            simxGetObjectHandle(clientID, "Baxter_rightArm_joint2", &Baxter_rightArm_joint2, simx_opmode_oneshot_wait);
            simxGetObjectHandle(clientID, "Baxter_rightArm_joint3", &Baxter_rightArm_joint3, simx_opmode_oneshot_wait);
            simxGetObjectHandle(clientID, "Baxter_rightArm_joint4", &Baxter_rightArm_joint4, simx_opmode_oneshot_wait);
            simxGetObjectHandle(clientID, "Baxter_rightArm_joint5", &Baxter_rightArm_joint5, simx_opmode_oneshot_wait);
            simxGetObjectHandle(clientID, "Baxter_rightArm_joint6", &Baxter_rightArm_joint6, simx_opmode_oneshot_wait);
            simxGetObjectHandle(clientID, "Baxter_rightArm_joint7", &Baxter_rightArm_joint7, simx_opmode_oneshot_wait);
    
            simxGetObjectHandle(clientID1, "Baxter_leftArm_joint1", &Baxter_leftArm_joint1, simx_opmode_oneshot_wait);
            simxGetObjectHandle(clientID1, "Baxter_leftArm_joint2", &Baxter_leftArm_joint2, simx_opmode_oneshot_wait);
            simxGetObjectHandle(clientID1, "Baxter_leftArm_joint3", &Baxter_leftArm_joint3, simx_opmode_oneshot_wait);
            simxGetObjectHandle(clientID1, "Baxter_leftArm_joint4", &Baxter_leftArm_joint4, simx_opmode_oneshot_wait);
            simxGetObjectHandle(clientID1, "Baxter_leftArm_joint5", &Baxter_leftArm_joint5, simx_opmode_oneshot_wait);
            simxGetObjectHandle(clientID1, "Baxter_leftArm_joint6", &Baxter_leftArm_joint6, simx_opmode_oneshot_wait);
            simxGetObjectHandle(clientID1, "Baxter_leftArm_joint7", &Baxter_leftArm_joint7, simx_opmode_oneshot_wait);
    
            std::ifstream fin("angle.txt", std::ios::in);
            char line[1024] = { 0 };
            float x1;
            float x2;
            float x3;
            float x4;
            float x5;
            float x6;
            float x7; 
            
                while (simxGetConnectionId(clientID) != -1 || simxGetConnectionId(clientID1) != -1)
                {
                    while (fin.getline(line, sizeof(line)))
                    {
                        std::stringstream word(line);
                        word >> x1;
                        word >> x2;
                        word >> x3;
                        word >> x4;
                        word >> x5;
                        word >> x6;
                        word >> x7;
                    //printf("chuanshu");
                    // default settings for baxter robot.
                    simxSetJointTargetPosition(clientID, Baxter_rightArm_joint1, x1, simx_opmode_oneshot);
                    simxSetJointTargetPosition(clientID, Baxter_rightArm_joint2, x2, simx_opmode_oneshot);
                    simxSetJointTargetPosition(clientID, Baxter_rightArm_joint3, x3, simx_opmode_oneshot);
                    simxSetJointTargetPosition(clientID, Baxter_rightArm_joint4, x4, simx_opmode_oneshot);
                    simxSetJointTargetPosition(clientID, Baxter_rightArm_joint5, x5, simx_opmode_oneshot);
                    simxSetJointTargetPosition(clientID, Baxter_rightArm_joint6, x6, simx_opmode_oneshot);
                    simxSetJointTargetPosition(clientID, Baxter_rightArm_joint7, x7, simx_opmode_oneshot);
    
                    simxSetJointTargetPosition(clientID1, Baxter_leftArm_joint1, 0, simx_opmode_oneshot);
                    simxSetJointTargetPosition(clientID1, Baxter_leftArm_joint2, 0, simx_opmode_oneshot);
                    simxSetJointTargetPosition(clientID1, Baxter_leftArm_joint3, 0, simx_opmode_oneshot);
                    simxSetJointTargetPosition(clientID1, Baxter_leftArm_joint4, 0, simx_opmode_oneshot);
                    simxSetJointTargetPosition(clientID1, Baxter_leftArm_joint5, 0, simx_opmode_oneshot);
                    simxSetJointTargetPosition(clientID1, Baxter_leftArm_joint6, 0, simx_opmode_oneshot);
                    simxSetJointTargetPosition(clientID1, Baxter_leftArm_joint7, 0, simx_opmode_oneshot);
                    std::cout << "x1: " << x1;
                    Sleep(200);
                }
            }
            fin.clear();
            fin.close();          
       }
        return(0);
    }

    四.实验结果

    先运行V-REP,再运行C++程序。运行成功后,C++控制窗口显示关节角信息,V-REP中的机器人开始运动,直到读取完文本。

    Github地址:https://github.com/scutXuYang/Communication-between-C--and-V-REP.git

  • 相关阅读:
    Linux:删除程序
    Linux:目录操作
    Linux:加载硬盘
    mysql:查询结果添加序列号
    mysql:结果集去重
    mysql:字符串转换为日期类型
    MVC:上传文件时限制文件类型
    WebApi:WebApi的Self Host模式
    WebApi:过滤器的种类
    几种知名开源富文本编辑器记录和对比(仅供参考)
  • 原文地址:https://www.cnblogs.com/youngsea/p/7830994.html
Copyright © 2011-2022 走看看