Today we’re back into breaking down the movement.py script. The last post walked through many of the functions needed to run the robot. Now we are going to break down some functions that create the movement. Without further ado, allons y!
This function is what we call when we want the robot to go forwards or backwards. It has two parameters: A key which is an integer and a speed which is an integer. The key parameter is used to determine how the robot should be moving. The other movement function for turning left and right ‘leftRightRun(key, s)’, also uses a key. The key being passed to the function determines what it should be doing, either going forward or backwards or left or right. In this function ‘bckFwdRun(key, s)’, the key determines whether the robot should go forwards or backwards.
The function begins with 2 variables: ‘fwd’ and ‘bwd’. These two variables call the ‘speedSettingToByteArray’ function from the last post to determine the speed from the integer passed as the ‘s‘ parameter. The first one, has reverse and arc set to false and passes it the ‘s’ parameter. The other variable has the reverse set to true and the arc false and passes the same ‘s’ parameter.
This makes two variables with the same speed except one or ‘fwd‘ makes the servos go forward, and the other ‘bwd‘ makes the servos go backwards.
Now we check if the ‘key’ passed was equal to 1, which is the key to make the robot go forward. If so, we run the motors of 1 and 3 using the ‘runMotorGroup(id_array, speed_in_byte_array)’ function and using the speed variable ‘fwd’. Then, we run servos 2 and 4 using the same function except with the ‘bwd’ speed variable. We do this because the servos are not mounted to spin the same way. Servos 1 and 3 spin in the opposite direction to 2 and 4. So to get servos 2 and 4 spinning the same direction as 1 and 3, we need them to run backwards.
Then we return ‘None’ to exit the function.
If the key wasn’t ‘1’, then we check if it is ‘2’. If so, we repeat the process at the first one, except servos 1 and 3 go backwards and 2 and 4 go forwards. Return ‘None’ to exit the function.
This last line of code is called when neither ‘1’ or ‘2’ is the ‘key’, or it breaks out of the ‘if’ statements. It simply prints it probably got a weird a key that wasn’t 1 or 2.
This function works the same way the above function does, however, to turn the robot left and right, one side of the robot must be running in reverse to the other side at the same speed. Our design does that. When the ‘key’ of ‘3’ is passed, it runs all motors with the ‘fwd’ variable. When the ‘key’ of ‘4’ is passed, it runs all servos with the ‘bwd’ variable.
That’s it for this post. Stay posted for the next post, Let’s break it down! ‘movement.py’ part 3. Adios!