Joe O'Reilly

AI - Task Sheet 4

18 Dec 2011

4.1

Approach an obstacle and stop

Using the first algorithm in the lecture notes I built a program to move towards an obstacle until the sensor reading below a certain value.

Algorithm One

var distance

while distance <= 20cm
     distance = sensor reading

    if distance > 20cm
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                move forwards 1cm

loop

Loop Control

You can see there is one main loop, containing a sensor block and a compare block. The result of this comparision is then used to decide if the robot should move forwards or not. You can control the distance the robot wants to be at inside the compare block. The loop exits when the robot is closer than the tested distance away.

I went on to build a program to test the second alogrithm from the lecture notes.

Algorithm Two

Pseudo Code

while distance < 20cm
    var distance = ultrasonic sensor reading

    if distance > 20cm
        switch motors on to move forward
    else
        switch motors off

loop

This algorithm contains two possible actions, move forwards and stop. When the robot reaches its target goal it stops when it hasn't reached its goal it moves forwards.

Algorithm Three

Pseudo Code

while true
    var distance = ultrasonic sensor reading

    if distance > 20cm
        switch motors on to move forwards
    else
        switch motors off

loop

Algorithm Four

Pseudo Code

while true
    var distance = ultrasonic sensor reading

        if distance > 20cm
            switch motors on to move forwards
        else
            if distance < 20cm
                switch motors on to move backwards
            else
                switch all motors off

loop

Observations of these Algorithms

Note: Slowing any of these alogirths down results in large improvements, reading is more accurate when the robot is slow as the bounce time for the sound more accurately reflects the actual distance.

4.2

Station-Keeping

The final algorthim from the last section proved fairly effective at this task however we had an idea of a better solution, here is the pseudo code as the actuall program is rather large

while true
    var power = sensor reading - goal distance

        if sensor reading = 20cm
            stop
        else 
            if power < 0
                move backwards at ((power * -1) + 15)
            else
                move forwards at (power + 15)

    loop

4.3

Wall-Following

Here is our wall following alogrithm expressed in pseudo code, too large in NXT software.

Reactive Control Algorithm

var previous = sensor reading
var current

move forwards 0 .5 rotations

while true

    current = sensor reading

    if current > 30 //on the right of the goal line
        if (current - previous) > 0 //moving further away from goal
            turn slightly to the left and go forward 0 .5 rotations
        else  //moving towards the goal
            go straight ahead for 0 .5 rotations
    else //on the left of the goal line
        if (current – previous) &lt; 0 //moving further away from goal
            turn slightly to the right and go forward 0 .5 rotations
    else //moving towards the goal
        go straight ahead for 0 .5 rotations

    current to previous

loop

It preformed very well at all distances, and didn't do anything unexpected like turning itself around.

Proportional Control Algorithm

var previous = sensor reading
var current

let gain = 0.4
let goal = 20 // Distance we want to keep to

while true

    current = sensor readings

    if current in range [-1..goal..+1]
        forwards(1)
    else

    if current > goal
        if current < previous
            forwards(1)
        else
            turn(current - goal * gain * -1)
    else
        if current < previous
            forwards(1)
        else
            turn(current - goal * gain)

    previous = current          

    handlebump()

loop

Proportional Derivative Control Algorithm

var previous = sensor reading
var current

var derivative = 0

let p-gain = 0.4
let d-gain = 0.5

while true

    current sensor reading

    derivative = (derivative + current) / 2

    if current in range goal±1
        forwards(1)
    else

    if current > goal
        if current < previous
            forwards(1)
        else
            turn (((current - goal) * gain) * (derivative * d-gain) *  -1)
    else
        if current < previous
            forwards(1)
        else
            turn (((current - goal) * gain) * (derivative * d-gain))

    previous = sensor reading

    handlebump()

loop

Next

AI - Task Sheet 5

Previous

AI - Task Sheet 1