Let’s Break It Down! “movement.py” part 3

Welcome to part 3 of Let’s break it down! ‘movement.py’. In the last post we went over two massive functions: ‘bckFwdRun(key, s)’ and ‘leftRightRun(key, s)’. In this post, we’re going to look at the next functions. 

These two functions play a very important part in the communications between python and client SART Control Panel. Both functions take only one parameter: ‘buf’. This is a ‘buffer’ or the strings being sent over from the client, over the web socket.

getSpeed(buf)

This function has only one line other than the first debug:
return int(str(buf)[2:])

This line returns the integer of the substring of the parameter ‘buf’, the 3rd value till the end, which is the ‘speed’ value. The ‘commands’ that will be sent from the client will be in the format of ‘key int {space} speed int’. For example, to send the command to go forward with 30% speed, the command would look like ‘1 3’.

getKey(buf)

Again, this function has only one line other than the debug. It’s exactly the same as ‘getSpeed’ except instead of getting the 3rd character and more, it gets the first one or ‘0’ because that’s where the key integer is located.

scanForRobot()

The last function we’re going to cover in this post is the ‘scanForRobot’ function. This function acts as a mini IP Scanner; it pings 5 IP addresses to see if they are online and then checks if any IP belongs to it. First of all, I created a list to contain all the online IPs. Next, I looped through the range 1 – 6 (Note: not including 6), as they are the numbers for the last digit on the end of the IPs I’m looking for. Next, we use the ‘subprocess’ module to run the UNIX command:
ping -c 1 -W 10.0.2.*

The output is determined to be either True or False (1 and 0) and if True (1), it adds the corresponding IP to the ‘online_ips’ list. After we’ve pinged all the IPs, we loop through the ‘online_ips’ list and check if there is a “10.0.2.4” and if not, a ‘10.0.2.3’. Those two are the only IP addresses that are manually assigned, however, it could change to be anywhere between 10.0.2.2 – 10.0.2.5.

 

That’s it for this post, stay tuned for the last post: “Let’s break it down! ‘movement.py’ part 4”!

 

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.