TurtleBot3 using ROS2 status update

Well.....I seem to have made "some" progress...of sorts.  I've managed to get the turtlebot3 software running correctly on the robot itself.  I have a Virtual Ubuntu 20.04 machine running ROS2 foxy edition and using all the OOTB stuff it seems to be behaving itself.

I have created a map using SLAM and used the Navigator2 to click around and get the turtlebot3 to plot a course and move around avoiding obstacles as well as "new" obstacles, like the cat suddenly appearing in front of it.  It seems to handle it well.

(A bit like a certain "Routing Widget v2.1" ;-) )

However...moving over into the ROS2 coding side of things, I had to go back into refresher-land for a day or two (I really do recall doing all of this back in 2017/2018, the memories are still there!).

If I do a direct call from the command-line:

ros2 topic pub --once /cmd_vel geometry_msgs/msg/Twist "{linear: {x: 2.0, y: 0.0, z: 0.0}, angular: {x: 1.0, y: 0.0, z: 1.8}}"


publisher: beginning loop
publishing #1: geometry_msgs.msg.Twist(linear=geometry_msgs.msg.Vector3(x=2.0, y=0.0, z=0.0), angular=geometry_msgs.msg.Vector3(x=1.0, y=0.0, z=1.8))

I get movement and the wheels move and off the robot goes - not what I want it to do, but it proves that the plumbing is correct.

I created a new package, created a new node and put the code in to do the same thing, but using the Python APIs....

Yes...the burden of my f*O)^*^ng life, Python... I should have just stuck with the C++ coding, but, no...I thought I would be good and "give Python another go"... sigh.

I've added the <exec_depends>rospy</exec_depends> into my package.xml file, I've correctly >colcon build --packages-package my_package1 with no errors

I've even opened a new terminal, >. install/setup.bash and then >ros2 run my_package1 my_node1 and then I get the error:

File "/home/tony/dev_ws/install/my_package1/lib/python3.8/site-packages/my_package1/my_node1.py", line 2, in <module>
    import rospy

ModuleNotFoundError: No module named 'rospy'

Thought I was onto a winner here....changing <exec_depends> to just <depend>

https://roboticsbackend.com/create-a-ros2-python-package/

However, that didn't seem to help - it "looks" like I have everything setup correct from a ROS2 perspective, so it must just be an install thing with Python 3.8 and the ROS2 libraries.

Well, after wasting a few hours going around the Internet of Confusion - it finally dawned on me.  I don't need rospy - it's one of the ROS1 libraries, along with roscore, rosrun and roslaunch - they've all been replaced with ros2 xxxx commands now.

I went in search of some equivalent samples to the code I had above (that turns out was for ROS1, not ROS2, even though the author I grabbed it from did state it was ROS2, however....)

I thought...hmmm....I'll re-do the code and put in reference to this new library and see if I can at least prevent the execution failure.  Yep.  On the right track, now for a bit of trial and error....

http://docs.ros.org.ros.informatik.uni-freiburg.de/en/foxy/Tutorials/Writing-A-Simple-Py-Publisher-And-Subscriber.html

As is usually the way - I had actually gone through this tutorial yesterday and had not quite appreciated that it DID NOT tell you how to do the Publish() API calls - it just explained it like you already knew - I went back over the pages around this, just to make sure I wasn't being ultra-dumb (it has been known) and nope, as far as I can see it was written "after the event". ie. I can now read that page and content and, now that I have figured it out / solved my problem, of course it now makes sense to me!  But, what with all the mis-information I was seeing in my DuckDuckGo results, I didn't trust myself and burnt a bit of time trusting other people - who, I now conclude, are mostly stupid and just copy&paste for web page hits - grrrhhh... the internet is going to get so messy over the coming years, someone will have to invent a product that will sit in front of the websites and "filter" out the crap (challenge: how do you figure that out?!?! AI perhaps?) and provide a non-biased set of results/content back to the user.  I digressed again.

So, here we have the Package.xml and the my_node1.py code:


As you can see, the commented line at the top shows the command-line command that publishes a command to the turtlebot3 to spin it's wheels, basically move along for a bit.  The second command would be to send 0.0 values to make it stop.

The code that follows is the same thing, but now using the 'rclpy' library to publish the command, along with the message content.

Top-Left: that's the new command to build the package

Middle-Left: An SSH session to the turtlebot3 and the command to start it up

Bottom-Left: initialising the environment and then calling the node

Top-Right: The command-line command we need to replicate

As you can see, the output shows the message that has been Published and this is picked up by the turtlebot3 - and yep, every 3 seconds, the wheel motors whirred away....I had expected them to get faster (as you can see the X and Z values increasing), however they decreased in speed, but at least it worked!

PHEW!  Okay, that means I now know how to perform the Publishing of commands to the turtlebot3, I can use the inverse for Subscribing to pick up the feedback from the LIDAR etc... and can then make decisions based upon how far away items are detected on which action to take, ie. turn left, turn right, go straight ahead, if don't detect anything for 1m, speed up, if detect something within 0.25m then slow down to assess the situation....

I'm quite pleased with myself :-D will post a video of it working soon.



 

Comments