Programming multiple frames

In the previous chapter, we have learned the basics of how to make sure that the robot is working under safe limits. In this chapter, we will be looking at how a robot can be made to do different activities at a click of a button, instead of typing the values one by one.

To do this, we will need to understand some advanced concepts of motion. Whenever you watch any video or play any video games, then you must have come across the term frames per second (FPS). If you haven't heard this term, then let me explain it for you. Every video made right now is actually made by still images. These still images are captured by cameras that click 25-30 times in a second. When these images are played back on the screen at the same rate at which they are captured, it forms a smooth video.

Similarly, in robots, we do have the concept of frames. These frames, however, are not images but instead multiple steps that the robot has to follow to achieve a specific motion. In a simple robotic program, there could be simply two frames, that is, the initial frame and the final frame. These two frames will correspond to the initial position or the final position.

However, in the real world, this is not always possible, as whenever the robot goes directly from the initial position to the final position, it tends a specific path with a specific curvature. However, there can be obstacles in that path or this path would not be desired as the path that needs to be followed could be a different one. Hence, we need frames. These frames not only define the robot's motion from the initial position to the final position, but also break down the transition from these two positions into multiple steps making the robot follow the desired path.

This can be referred as frame programming, which we will cover in this chapter. One thing to keep in mind is that more the number of frames, smoother will be the functioning of the robot. Do you remember the CCTV footage we saw? We could say it's not smooth and has a lot of jerkiness. This is due to the low frame rate of the CCTV camera. Instead of working on 30 FPS, they work on 15 FPS. This is done to reduce the storage space of the video. However, if you see the latest videos, there are some games and videos with much higher frame rate than normal. Some of our latest camera works on 60 FPS, making the video even smoother and enjoyable to watch. The same will be the case with the robot. The more the number of frames, the smoother and controlled the motion would be. However, make sure you don't go into overkill.

Now, to move from one position to another, we will have to put the values of the angles of every single servos in the very beginning. Once fetched, it will automatically start to execute these values one by one. To do this, go ahead and write the following code:

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
GPIO.setup(14,GPIO.OUT)
GPIO.setup(16,GPIO.OUT)
GPIO.setup(18,GPIO.OUT)
GPIO.setup(20,GPIO.OUT)
GPIO.setup(21,GPIO.OUT)
GPIO.setup(22,GPIO.OUT)


GPIO.setwarnings(False)

pwm1 = GPIO.PWM(14, 50)
pwm2 = GPIO.PWM(16, 50)
pwm3 = GPIO.PWM(18, 50)
pwm4 = GPIO.PWM(20, 50)
pwm5 = GPIO.PWM(21, 50)
pwm6 = GPIO.PWM(22, 50)

pwm1.start(0)
pwm2.start(0)
pwm3.start(0)
pwm4.start(0)
pwm5.start(0)
pwm6.start(0)

def cvt_angle(angle):
dc = float(angle/90) + 0.5
return dc

prev0 = 90
prev1 = 90
prev2 = 90
prev3 = 90
prev4 = 90
prev5 = 90

while True:

a = raw_input("enter a list of 6 values for motor 1")
b = raw_input("enter a list of 6 values for motor 2")
c = raw_input("enter a list of 6 values for motor 3")
d = raw_input("enter a list of 6 values for motor 4")
e = raw_input("enter a list of 6 values for motor 5")
f = raw_input("enter a list of 6 values for motor 6")


for i in range(6):

if a[i] > 10 and a[i]< 180 :
pwm1.ChangeDutyCycle(cvt_angle(a[i]))

if b[i] > 10 and b[i] < 180:
pwm2.ChangeDutyCycle(cvt_angle(b[i]))

if c[i] > 10 and c[i] < 180:

pwm3.ChangeDutyCycle(cvt_angle(c[i]))

if d[i] > 10 and d[i] < 180:

pwm4.ChangeDutyCycle(cvt_angle(d[i]))

if e[i] > 10 and e[i] < 180:

pwm5.ChangeDutyCycle(cvt_angle(e[i]))

if f[i] > 10 and f[i] < 180:

pwm6.ChangeDutyCycle(cvt_angle(f[i]))


In this program, you can see that we have replicated the previous program with some very minor changes. So, let's see what these changes are:

    a = raw_input("enter a list of 6 values for motor 1")
b = raw_input("enter a list of 6 values for motor 2")
c = raw_input("enter a list of 6 values for motor 3")
d = raw_input("enter a list of 6 values for motor 4")
e = raw_input("enter a list of 6 values for motor 5")
f = raw_input("enter a list of 6 values for motor 6")

Here, we are taking the input values for each servo and storing it in a different list. For servo 1, the list a will be used; similarly, b will be used for servo 2, and so on until f. In the preceding lines of code, the robot will prompt the user to fill in the six frame values for motor 1. Then, it will ask six values for motor 2 and similarly until motor 6:

    for i in range(6):

The entire program for giving PWM to the servo is concentrated in this for loop. This loop will check the value of i and increment it every time. The value of i will start from 1 and the loop will run and increment the value of i until it reaches 6.

        if a[i] > 10 and a[i]< 180 :  
pwm1.ChangeDutyCycle(cvt_angle(a[i]))

In this line of the program, the value contained in the list is headed based on the value of 1. So, for the first time it will read the value of a[1], which will correspond to the first value of the list a[]. This value should be between the safe working limits, hence the if loop. If it is within safe working limits, then the program in the if condition will work, else it won't. Inside the if loop, we have a simple statement: pwm1.ChangeDutyCycle(cvt_angle(a[I])). This will simply take the value of a[1] and convert it into the corresponding PWM value and fetch it to the ChangeDutyCycle() function, which will change the PWM for servo 1.

A similar program is made for the rest of the servos as well going on from servo 1 to servo 6. Hence, all of these will read the values of their corresponding list one by one and change the angle of the servo the way the user has programmed it. Furthermore, as the loop gets executed, the value of i will increase, hence making the program read the different values fetched in the list. Every value of the servo in the list will correspond to a different frame, hence parsing the robot through it.

So go ahead and have some fun making your robot do some awesome moves. Just take care that you be gentle to it!

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.222.115.120