[Music] hey guys welcome back to the channel welcome back to this series this is a series where we build our own autonomous rover using ross from scratch it's been a really long time since i've uploaded the first part of this series and that's because everything that could go wrong went wrong whether it is shipping times whether it's the batteries and whether it's you know changing a bit of the design everything that could go wrong went wrong but here we are and let's get started with this series just before that i am also working on building a robotic arm it's been a work in progress for about a year and a half i continuously keep updating it and changing the design uh some footage of it will be playing right here i'd love to make a video about how i've built the arm so let me know down below for now let's get into this video looking at the rover not much has changed since the last video i got sick of seeing all the wires tangled so i had to do some wire management and now to the main chain since last episode we aren't using the 18650 lithium-ion batteries that we talked about in the previous video one pair suddenly stopped working and the pair i got to replace them caught fire while i was trying to charge them i then turned to these nimh batteries which worked decently well despite a lower voltage but were incredibly heavy and i had to face a lot of problems powering the pi with these they kept getting stuck in a boot loop and would never start i also knew if we had to add a lidar eventually we'd have to have extra power so i switched to this it's a 10000mah mi power bank the important spec for me was that it supported fast charging and had three outputs i could also switch to usb c power delivery if i wanted one output went to the motors from a dc dc boost converter and it all works really well one goes to the pi and one hopefully is for the lidar now time to move ahead with the project last episode we wrote some code to move the robot forwards and backwards but if you're going to navigate the robot with ross we'll need to have a slightly more advanced system than just moving it forwards and backwards most indoor mobile robots resemble cars but are actually very different animals the most popular way to drive these indoor robots is by using a differential drive system it consists of two wheels actuated by motors that lie on the same axis so by varying the speeds of these motors we can drive them in different directions suppose we drive the motors in the same direction the resultant velocity of the robot will be in that particular direction if we drive it in opposite directions then the robot will revolve around its axis okay so let's summarize the kinematics of a differential drive robot let's say we have a differential drive robot with its wheel separated by length l and that it's moving in a curve this curve has a center called icc or instantaneous center of curvature now the distance between icc and the midpoint of the wheel base is given by r by varying r we can change the arc in which the robot goes if r is equal to zero then this robot spins around its own axis so let's take the resultant velocity v of the robot to be split into two components v linear plus v omega or v angular if we have to finally translate it to the wheel velocities vl and vr we do it by these equations so vl is equal to v linear minus v omega into l by 2 and vr is v linear plus v omega into l by 2 with these two equations we actually can translate the v the final velocity of the robot in terms of its individual wheel velocities so why is this important now when ross is commanding a robot it actually sends a value in a variable called command velocity now this command velocity actually is made of v x and v z which actually correspond to v linear and v angular now when our arduino receives these values it is meant to translate it to individual wheel velocities and we do this using these two equations okay so let's discuss a topic that we've been delaying for a while what is ross and why do we need it to answer this question let's rewind a bit to the mid 2000s when robotic software was a very proprietary affair people felt that too much time was being spent re-implementing the software stack that is building complex robotics algorithms for slam navigation and this left too little time to actually build and make robots two people from stanford eric and keenan recognized this problem and wanted to remove the whole reinventing the wheel situation by starting the personal robotics program with the aim to build a framework that allowed processes to communicate with each other they went on to develop the software stack as a universal system in a company called willow garage and finally the pr2 robot was created and draws with it the ross project then became so important that they discarded other projects for the development of ross below garage would later announce that they were shutting down and the newly created osrf or open source robotics foundation would take over and further the development of ros so ross contrary to popular opinion is not an operating system but a middleware and a set of open source libraries and applications that make building robotics applications easier for example if i have to build a navigation stack from scratch i would have to write my own algorithms for slam path planning and navigation when we can just use the navigation stack provided to us by ross so how does it all work ross provides a runtime in the form of ros master for nodes to register and then talk to each other a node is a single executable program that contains some code for the robot to work some nodes are written by us and some come from packages ros provides a few communication methods for these nodes to talk to each other a few of the important ones are topics topics are like pigeon pigeonhole systems that notes publish to or subscribe from and get messages services represent a traditional internet-like request and response infrastructure also ross is a distributed computing system and has a master's slave infrastructure in this series the raspberry pi acts as a slave and the main computer that is my laptop acts as a master i won't be going through the installation procedure as that is already very well documented over at the ross website i'll have it linked in the description below same goes for the network setup they actually have beautiful tutorials and i have both of these linked in the description below it's finally time to write the last arduino program hopefully we're putting together all the code that we have accumulated till now into one big program all of this code will be available on my github the links will be given in the description below the goal is to set up a ros node on the arduino which subscribes to the topic slash cmd well or command velocity which other packages in ross will publish too to control the robot and then we make two topics slash lwheel and slash rv to which the robot will then publish the current wheel positions of the robot with the arduino first let's include the header files required for the sketch to run we first have ros and ross time which are needed to keep the ros node running we then also include the message header files for the message types of the topics that we'll be publishing and subscribing to we then have the pid library in a differential drive robot for the robot to move straight we need the motors to move at the same speed in theory giving it the same voltage would mean the motors would move at the same speed in practice giving motors the same voltage does not always mean that they move in the same speed in a differential drive robot that means when you expect the robot to move straight it actually turns either to the left or to the right depending on which motor is moving faster or which motor is moving slower this is why we need a pid to get the actual speed from the encoders and use it to match the speed required as per the command velocity topic we then declare the ros node handle nh it's a service that tracks and takes care of nodes in the c plus program we have two variables update nh and loop time that we'll come back to in a bit we then set up our pid controller and its p i and d parameters these are values that i have tuned for and these might not work for you we need two pid controllers for both the wheels as stated before each pid works by taking the current output which is the current speed comparing it to the set point which is the required speed and giving the corresponding output as current to the wheels based on the proportional integral and derivative parameters this is a whole branch of engineering and i'll link a relevant resource below to learn more we then set up the raw subscriber and publisher nodes we first set up the subscriber node that subscribes to the command velocity topic and has a callback function that executes every time we get a message this message basically gives us the linear and angular velocity from ros and stores it in the demand x and demand z variables we also set up the two ros publishers left wheel and right wheel that published left and right wheel positions respectively we first initialize these nodes and then set up the subscribers and publishers by advertising them we also set up the pid controllers and also use the same four functions to attach interrupt handlers that we talked about in the previous video coming to the loop now we take in the two demand z and demand x variables and split it accord to the left and right wheels according to the formula that we described about before we then take in the speed as the difference in encoder counts per unit time and feed it to the pid controller which will then give us the exact outputs that we feed to the wheels as we scroll down we also see that we use the same four functions that we use to get back the encoder counts so now after uploading the sketch let's see how it all fits together we connect our arduino to the pi and then connect to the pi via ssh now make sure that your pi and computer are on the same network also for this to work you need to download two packages that is the raw serial python package and the teleop twist keyboard package make sure that you've downloaded these two packages the links will be given in the description below meanwhile open another shell and run raw score we then set permissions to use the arduino as an output and then run the serial node command ros run raw serial python serial node dot py and give the port that the arduino is connected to now if everything goes well you should see that it has set up two publishers and one subscriber time to finally move the robot let's open up another terminal and run ros run teleop twist keyboard tell your twist keyboard dot py now if all goes well you can use the keys i comma j and l to just move the robot around time to ssh into our pi and set up the differential drive workspace let's make a new workspace name it ws rover underscore ws or anything that's easily typed into the terminal ross follows a certain file system structure and will be following that in ws you make a directory called src this is the source directory in which all our packages will reside we then type catkin init workspace which sets up the initial layout of the workspace the next step is to go back and run catkin make that builds the empty workspace now this may take a while so sit back and let the workspace build after the build is over we then source the workspace with source devil setup.zsh now this depends on your terminal i am using the zsh terminal you might use bash or any other terminal this is the same workflow that will always use for building applications with ross after sourcing the workspace now it's time for us to add the packages going back to the source directory now it's time to add the differential drive package we do this by getting the git link from the github repository going back and cloning the website into the source directory we then add a new package for our rover which is where all the code required for our rover will exist we do this by catkin create package rover and then we go back and we build the workspace again so now after all of this is done your workspace should look something like this it's time to make our first launch file a ros launch file is the file that we use to fire up and run multiple nodes we will store all our nodes and our launch files in our rover package as it's easy to keep track of it all i'm using vs code to work on the remote ssh workspace that is on the raspberry pi but if you all have any other workflows that's okay as well vs code is amazing and i highly recommend that you all use it okay now let's open our source folder in the workspace so that we can see both the packages and their files on the side we now create a new folder in the rover package called launch to store our launch files and then create a new file called drive.launch this is where we run our two nodes raw serial python and diff tf and give them their respective parameters okay so launch files are generally written in the xml format and you can use this as reference we then add our two nodes and set it up in this particular format and give them the ports and the general parameters required for the program to run time to run these launch files in the terminal to run our launch files we run two terminals again our host running raw score and the second for ssh after the ssh we first move to the workspace and then source it now we actually forgot a step that we were supposed to do in the previous video which is to set the diff tf dot py script as an executable we do this by running the command chmod plus x diff tf dot py now we set the arduino as an output by using the chmod command again and finally run the ros launch rover drive dot launch command now that this is running we open up another instance of the terminal and run rvs rvs or ros visualization is what we use to visualize this project we then go over to the left side and add a tf visualization pane we now see the two tf frames odom and bass link overlapped with each other now if you open up another terminal instance and open teleop twist keyboard instance or even more the robot wheels with your hands you can see the axis shift by the same amount and that's it and that's it that's all that is there is for this video i hope you guys can take this information and tune the parameters and get your odometry working as good as possible i'll see you guys in the next one uh take care and peace you