Cpp code translation to python help [closed]

asked 2020-09-11 11:04:06 -0600

jimc91 gravatar image

Hi All,

My supervisor has given me some code that I can use with my project. However, he codes in C++ and I, as a beginner, code in python (at a very basic level I might add).

I am using ROS Kinetic & Ubuntu 16.04LTS and the aim of my project is communication between LabVIEW and ROS for robot navigation.

I have completed the ROS wiki tutorials on creating publishers & subscribers in Python, but I cannot get my head around the cpp code and I am trying to avoid it if possible as I feel it will just eat into my limited time left to complete this project. I have also completed numerous modules offered on Robot Ignite Academy and they have all been python focused, so I really do not have the time to now go and learn C++. I hope I`m not sounding lazy, Ive put in massive effort to get my skill level to where it is, I am a mechanical engineer with absolutely zero coding experience until I began this project a few weeks back.

Would anyone be of assistance or know where I could go for someone to walk me through the below code so that it can be translated to .py?

#include <ros/ros.h>

#include <move_base_msgs/MoveBaseAction.h>

#include <actionlib/client/simple_action_client.h>

#include "std_msgs/String.h"

#include <sstream>

#include <iostream> 

#include <unistd.h> 

using namespace std; 

string s;



void chatterCallback(const std_msgs::String::ConstPtr& msg)

{ 

s=msg->data; 

cout<<s<<endl; 

}

bool moveToGoal(double xGoal, double yGoal, double zOrientation, double wOrientation);



        double xPA = -0.04;

        double yPA = -0.95;

        double zPA = -0.712;

        double wPA = 0.702;



            double xDA = -0.2;

            double yDA = -2.4;

            double zDA = -0.008;

            double wDA = 0.100;







bool goalReached = false;





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



    ros::init(argc, argv, "code_testing");

    ros::NodeHandle n;

    ros::Publisher chatter_pub = n.advertise<std_msgs::String>("chatter", 100);



ros::Subscriber sub = n.subscribe("chatter1", 100, chatterCallback);



     ros::Rate loop_rate(10);

     ros::spinOnce();





    do{



    ros::spinOnce();    

    std::cout<<"waiting next command"<<std::endl;



        if (s == "Go_to_PICK_Location_A"){

            goalReached = moveToGoal(xPA, yPA, zPA, wPA);



            if (goalReached)

                {

                for (int j = 1; j<250; j++)

                {



                std_msgs::String msg;

                std::stringstream ss;

                    ss << "Reached_PICK_Location_A";

                msg.data = ss.str();

                    ROS_INFO("%s", msg.data.c_str());

                    ros::spinOnce();

                chatter_pub.publish(msg);

                ros::Rate loop_rate(10);                     

                }           

            while (s == "Go_to_PICK_Location_A")

                {

                ROS_INFO("Reached PA; waiting instructions from LabVIEW");

                ros::spinOnce();

                    }

                }



            else{

                for (int i = 1; i<250; i++)

                {



                std_msgs::String msg;

                std::stringstream ss;

                    ss << "PICK_Location_A_Unreachable";

                msg.data = ss.str();

                    ROS_INFO("%s", msg.data.c_str());

                    ros::spinOnce();

                chatter_pub.publish(msg);

                ros::Rate loop_rate(10);                     

                }



            while (s == "Go_to_PICK_Location_A")

                {

                ROS_INFO("Can not reached PA; waiting instructions from LabVIEW");

                ros::spinOnce();

                    }

            }

    }







else if (s == "Go_to_DROP_Location_A"){

            goalReached = moveToGoal(xDA, yDA, zDA, wDA);



            if (goalReached)

                {

                for (int j = 1; j<250; j++)

                {



                std_msgs::String msg;

                std::stringstream ss;

                    ss << "Reached_DROP_Location_A";

                msg.data = ss.str();

                    ROS_INFO("%s", msg.data.c_str());

                    ros::spinOnce();

                chatter_pub.publish(msg);

                ros::Rate loop_rate(10);                     

                }           

            while (s == "Go_to_DROP_Location_A")

                {

                ROS_INFO("Reached DA; waiting instructions from LabVIEW");

                ros ...
(more)
edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by jimc91
close date 2020-09-15 07:29:42.074142

Comments

1

@jimc91 In my opinion I think the correct approach here is that you learn some of the basic concepts of C++ programming and roscpp. The main reason is that you may need that knowledge in the future; another reason is that, to translate code from on language to another you really need to understand what the code is doing in the first place and It seems to me that you are not fully understanding what the code given to you is doing. Having said that, however, I do not know if you are asking for help to understand it or you want others to direcly translate it. But if you want to translate this code, as I said, you will need to understand what it is doing. The C++ code is just a a part of a greater project: that is, it is an actionlib client for sending goals to ...(more)

Weasfas gravatar image Weasfas  ( 2020-09-12 06:28:57 -0600 )edit

Thanks for the reply. No I was hoping for advice/input on it, for someone to translate it for me would be of no benefit to me and would just be an overall lazy option for me :).

I will begin to dig into cpp code and will aim to get the understanding of this.

jimc91 gravatar image jimc91  ( 2020-09-15 07:29:34 -0600 )edit

Good! @jimc91, proud of your decission, and remember that if you have any question or problem you can always rely in this forum, =).

Weasfas gravatar image Weasfas  ( 2020-09-15 11:37:19 -0600 )edit