Page 1
Robotics: Designing and Building
Multi-robot Systems
Day 2
UNO Summer 2010 High School UNO Summer 2010 High School
Workshop
Raj Dasupta
Associate Professor
Computer Science Department
University of Nebraska, Omaha
College of Information Science and Technology
Page 2
Plan for Day 2
• Designing autonomous intelligence in robots...controller– MyBot: Simple Obstacle Avoidance
– Blinking LEDs
– Controlling motors– Controlling motors
– Camera-based object following
– Finte State machine: Lawn mower like pattern
– Obstacle Avoidance: Code review
– Line Follower: Code review
– Odometry: Simulation only
– Braitenberg: Code review
Page 3
Designing the Robot’s Controller
• Controller contains the ‘brain’ of the robot
Controller
Read input from
sensors
Send output
to actuators
Page 4
Designing the Robot’s Controller
• Controller contains the ‘brain’ of the robot
Controller
Read input from
sensors
Send output
to actuators
Page 5
Reading the Input from the sensors
#include <webots/robot.h>#include <webots/distance_sensor.h>#include <stdio.h>
#define TIME_STEP 32
int main() {wb_robot_init();
WbDeviceTag ds = wb_robot_get_device("my_distance_sensor");wb_distance_sensor_enable(ds, TIME_STEP);
while (1) {wb_robot_step(TIME_STEP);double dist = wb_distance_sensor_get_value(ds);printf("sensor value is %f\n", dist);
}
return 0;}
Page 6
Reading the Input from the sensors
#include <webots/robot.h>#include <webots/distance_sensor.h>#include <stdio.h>
#define TIME_STEP 32
int main() {wb_robot_init();
This is the “name”
field of the robot’s
sensor from the
scene tree
1. Get a handle to
the sensor device
WbDeviceTag ds = wb_robot_get_device("my_distance_sensor");wb_distance_sensor_enable(ds, TIME_STEP);
while (1) {wb_robot_step(TIME_STEP);double dist = wb_distance_sensor_get_value(ds);printf("sensor value is %f\n", dist);
}
return 0;}
2. Enable the sensor device...the general format of this step is
wb_<sensor_name>_enable (sensor_handle, poll_time)
How often to get
the data from the
sensor
3. Get the sensor data...the general format of this step is
wb_<sensor_name>_get_value (sensor_handle)
Page 7
Designing the Robot’s Controller
• Controller contains the ‘brain’ of the robot
Controller
Read input from
sensors
Send output
to actuators
Page 8
Sending the output to actuators
#include <webots/robot.h>#include <webots/servo.h>#include <math.h>
#define TIME_STEP 32
int main() {wb_robot_init();
WbDeviceTag servo = wb_robot_get_device("my_servo");WbDeviceTag servo = wb_robot_get_device("my_servo");
double F = 2.0; // frequency 2 Hzdouble t = 0.0; // elapsed simulation time
while (1) {double pos = sin(t * 2.0 * M_PI * F);wb_servo_set_position(servo, pos);wb_robot_step(TIME_STEP);t += (double)TIME_STEP / 1000.0;
}
return 0;}
Page 9
Designing the Robot’s Controller
• Controller contains the ‘brain’ of the robot
Controller
Read input from
sensors
Send output
to actuators
Page 10
A simple example#include <webots/robot.h>#include <webots/differential_wheels.h>#include <webots/distance_sensor.h>
#define TIME_STEP 32
int main() {wb_robot_init();
WbDeviceTag left_sensor = wb_robot_get_device("left_sensor");WbDeviceTag right_sensor = wb_robot_get_device("right_sensor");wb_distance_sensor_enable(left_sensor, TIME_STEP);wb_distance_sensor_enable(right_sensor, TIME_STEP);
while (1) {wb_robot_step(TIME_STEP);
// read sensorsdouble left_dist = wb_distance_sensor_get_value(left_sensor);double right_dist = wb_distance_sensor_get_value(right_sensor);
// compute behaviordouble left = compute_left_speed(left_dist, right_dist);double right = compute_right_speed(left_dist, right_dist);
// actuate wheel motorswb_differential_wheels_set_speed(left, right);
}
return 0;}
Page 11
A simple example#include <webots/robot.h>#include <webots/differential_wheels.h>#include <webots/distance_sensor.h>
#define TIME_STEP 32
int main() {wb_robot_init();
WbDeviceTag left_sensor = wb_robot_get_device("left_sensor");WbDeviceTag right_sensor = wb_robot_get_device("right_sensor");wb_distance_sensor_enable(left_sensor, TIME_STEP);wb_distance_sensor_enable(right_sensor, TIME_STEP);
while (1) {wb_robot_step(TIME_STEP);
// read sensorsdouble left_dist = wb_distance_sensor_get_value(left_sensor);double right_dist = wb_distance_sensor_get_value(right_sensor);
// compute behaviordouble left = compute_left_speed(left_dist, right_dist);double right = compute_right_speed(left_dist, right_dist);
// actuate wheel motorswb_differential_wheels_set_speed(left, right);
}
return 0;}
Get input from sensor data
Send output to actuator
A very simple controller
Page 12
A few other points#include <webots/robot.h>#include <webots/differential_wheels.h>#include <webots/distance_sensor.h>
#define TIME_STEP 32
int main() {wb_robot_init();
WbDeviceTag left_sensor = wb_robot_get_device("left_sensor");WbDeviceTag right_sensor = wb_robot_get_device("right_sensor");wb_distance_sensor_enable(left_sensor, TIME_STEP);wb_distance_sensor_enable(right_sensor, TIME_STEP);
Keep doing this as long as
the simulation (and the
robot) runs
Mandatory initialization
step...only used in C language
while (1) {wb_robot_step(TIME_STEP);
// read sensorsdouble left_dist = wb_distance_sensor_get_value(left_sensor);double right_dist = wb_distance_sensor_get_value(right_sensor);
// compute behaviordouble left = compute_left_speed(left_dist, right_dist);double right = compute_right_speed(left_dist, right_dist);
// actuate wheel motorswb_differential_wheels_set_speed(left, right);
}
return 0;}
• How often to get data
from simulated robot into
the controller program
• Every controller must
have it
• Must be called at regular
intervals
• Placed inside main()
Page 13
MyBot Controller
• Simple obstacle avoidance behavior
– Get IR distance sensor inputs (both L and R)
– If both of these readings are > 500... its an
emergency, back up fastemergency, back up fast
– If only one of these readings is > 500...turn
proportionately to the sensor values
– If both readings are < 500...nothing wrong, keep
moving straight
• Let’s program this in Webots
Page 14
E-puck: Spinning
• Start spinning left
• If left wheel speed > 1234, stop
Page 15
E-puck: LED blinking
• Blink the 8 LEDs of the e-puck one after another
– Turn all LEDs off
– Count repeatedly from 1...8– Count repeatedly from 1...8
• Turn LED<count> on
• Note that in this example, we are not using any sensor inputs...the LEDS just start blinking in a circle when we start running the simulation
Page 16
Camera Controller
• Objective: Follow a white ball
• How to do it...
– Get the image from the camera
– Calculate the brightest spot on the camera’s – Calculate the brightest spot on the camera’s
image...the portion of the image that has the
highest intensity (white color of ball will have
highest intensity)
– Set the direction and speed of the wheels of the
robot to move towards the brightest spot
Page 17
State-based controller for object
following with camera
Analyze
image to
find the
brightest
spot
Do some math
to calculate the
direction and
speed of the
wheels to get to
the brightest spo
Get image
from
camera
the brightest spo
Set wheel
speed to
the
calculated
values
Page 18
State Machine
• Controller is usually implemented as a finite
state machine
State i
State j
State k
Transition (i,j) Transition (J,k)
Page 19
A finite state-based controller for
avoiding obstacles
Stop
One of L or R front
sensors recording
obstacles
40 steps complete
Moving
forward
Make a
U-turn
Both L and R front
sensors not recording
any obstacles
40 steps not complete
Angle turned >= 180
degrees
Page 20
E-puck Webots Review
• Obstacle Avoidance: Code review
• Line Follower: Code review
• Odometry: Simulation only
• Braitenberg: Code review• Braitenberg: Code review
Page 21
Summary of Day 2 Activities
• Today we learned about the controller of a robot
• The controller is the autonomous, intelligent part of the robotpart of the robot
• The controller processes the inputs from the robot’s sensors and tells the robot’s actuator what to do
• We wrote the code for different controllers and reviewed some complex controllers
Page 22
Plan for Day 3
• We will learn how to program some basic
behaviors on the e-puck robot
– Zachary will be teaching this section