Professor: Dr.Ali Talebi , Teaching Asistant: AmirAli Setayeshi
- Controlling robot in 1 dimention using P and PI controllers
- Controlling robot angle usign P and PI controller
- Moving robot to the targeted coordination in 2 dimentions using P and PI controllers
- Moving robot to the ball
We use v and ω to design controllers but in reality we have to control robot with left and right motor. So we use these transforms:
vr = (2*v + w*L)/(2*R)
vl = (2*v - w*L)/(2*R)
And there is a dead zone for real motors that we cannot get lower speed than a specific speed.
So we simulated it the soft way.
if abs(vr) > offset:
self.right_motor.setVelocity(vr)
else:
self.right_motor.setVelocity(0)
if abs(vl) > offset:
self.left_motor.setVelocity(vl)
else:
self.left_motor.setVelocity(0)
for ease of use we wrote a function to do all these
def set_v_w(v, w):
L, R = 0.02, 0.085
offset = 0.5
vr = (2*v + w*L)/(2*R)
vl = (2*v - w*L)/(2*R)
if abs(vr) > offset:
self.right_motor.setVelocity(vr)
else:
self.right_motor.setVelocity(0)
if abs(vl) > offset:
self.left_motor.setVelocity(vl)
else:
self.left_motor.setVelocity(0)
The credit of soccer robot simulation is belonged to Adman from this repo rcj-soccersim
The only difference is robot controllers.
To get the robot coordination there was a python dictionary variable named ball_data
that has two keys named strength
and direction
. strength
is the IR sensor output. it's reverse of distance. direction
was an array with unknown normalized data between -1 and 1. with multiple tests understood they probably are sine and cosine of the angle between robot and ball.
direction | angle |
---|---|
![]() |
![]() |
To make it simpler we defined a PI_controller
class, and used it for ball tracking. The ceoficients of Integrator (Ki) and Power (Kp) are beeing changed dependent to relative location of robot and ball. e.g. we defined a flag named turning_flag
for the situations that only ω speed is better for the robot. Another case is the distance between robot and ball, for distances more than 0.05 we use constant v with value of 0.75 but for less there is a PI controller.
In the below graph you can see the graph of controllers motor voltage output response to errors.
We measured the maximum time of each loop (T = 1.3e-6) and used a time.sleep()
to make the total time of each loop constant.
class PI_controller:
def __init__(self, Kp, Ki):
self.I = 0
self.T = 1.3e-6
self.Ki = Ki
self.Kp = Kp
def update(self, error):
self.I = self.T * self.Ki * error + self.I
Y = -self.Kp * error - self.I
return Y
We tested several Kis and Kps and higher Kp is better for response time and higher Kp results better final error.
as we told
with multiple tests understood they probably are sine and cosine of the angle between robot and ball
we're not sure what these variables exactly are, because they work like that most of the time but because of Webots or rcj-soccersim code bugs, sometimes they don't work like that and that causes unstablity of the robot. e.g. when robot is exactly behind the ball, direction[1]
should be 0, but it's 0.8 and we don't know why.
Connecting to raspberry pi (robot) using SSH and control it using keyboard (WSAD keys)
A soccer playing robot
The main algorithm inlcudes 2 main states:
- following the ball
- has the ball
- attack
- defend
using the algorithm in exercise the robot follows the ball until it reaches near the ball so we asume that the robot has the ball.
depends on the direction of the ball and the robot and the gate of teams, it defines to attack or defend. In both cases the controller tries to go to oponent gate but the ceoficients of the controller are different. for attack mode because the ball is between oponent gate and robot, it has high velosity to goal. for defend mode the ball is between the robot gate and it self, so it uses low velosity to controll the ball to the other direction.