Application of AI and Machine learning in Robotics: Using Karel the Robot

Application of AI and Machine learning in Robotics: Using Karel the Robot

The World today is revolving around sophisticated technology in which robots are used to perform various tasks with a human-like vision to recognize various objects using great precision and accuracy.

Robots are now created using machine learning training. Also, a sizable number of datasets are utilized to train the computer vision model, enabling robotics to distinguish distinct objects and take the appropriate actions.

In this article, you are going to learn how you can direct Karel to perform certain tasks within its world using some set of commands.

Introduction.

Karel is a programming language that is used for teaching beginners the basics of programming and robotics. It is a high-level programming language that is easy to learn and understand.

Karel is also used to teach a wide range of concepts including control flow, functions, and basic algorithms. It is also used to explain an introduction to robotics, as the students can learn about basic concepts such as sensors, actuators, and feedback control.

Requirements

  1. PyCharm — for running and testing your codes. You don’t have it? Don’t worry you can download it for free here Pycharm by JetBrains

  2. You can also write and run your code using Coding rooms workspace. It’s free

  3. Basic Knowledge of Python.

This is going to be an exciting pathway to understanding and controlling Karel the robot. Let get started

Control Flow

Firstly, I will introduce you to some in-built descriptive names that Karel already understands.

Most of these names are written in snake_case which include but are not limited to move(), pick_beeper(), put_beeper(), and turn_left() but Karel does not understand turn_right(). So you need to teach Karel how to turn_right()

Let’s put all these descriptive names into use.

Note — Take note of the indentation used below, it is very important to indent properly to avoid code errors while using Python

def main():
    move()
    pick_beeper()
    move()
    turn_left()
    move()
    turn_right()
    move()
    put_beeper()
    move()
# Teaching karel how to turn right by defining turn right
def turn_right():
    turn_left()
    turn_left()
    turn_left()

Now you have made Karel understand that turning left three times automatically resulted in turning right. This was achieved by defining turn_right using def.

Now let’s run the code to see Karel performing those commands.

Using For Loops

For loop can also be referred to as a definite loop. This is a repeat process that is used to know how many times a command should be carried out.

One of the attributes of a great programmer is to use a small line of code to perform a specific task. So, you will use for loop to avoid typing turn_left() three times when defining turn_right().

def main():
     turn_right()

def turn_right():
    for i in range(3):
        turn_left()

For loop can also be used to perform a series of repetitive tasks which can make your coding much simpler and easy to understand

For example, you will use for loop to command Karel the robot to perform a task four times using descriptive names.

def main():
    for i in range(4):
        put_beeper()
        move()
        turn_left()

The result is shown below

Using While Loops

While loop can also be referred to as an indefinite loop. This is also a repeat process, which is used to check if a certain condition is true or false before it carries out the command.

The conditions Karel can check for include but are not limited to:

The conditions Karel can check for include but are not limited to:

TestOppositeWhat it checks
front_is_clear()front_is_blocked()Is there a wall in front of Karel?
left_is_clear()left_is_blocked()Is there a wall to Karel’s left?
right_is_clear()right_is_blocked()Is there a wall to Karel’s right?
beepers_present()no_beepers_present()Are there beepers on this corner?
beepers_in_bag()beepers_in_bag()Are there beepers in Karel’s bag?
facing_north()not_facing_north()Is Karel facing north?

The syntax of a while loop is shown below

def move_to_wall():
    while front_is_clear():
        move()

Now, let us command Karel to perform a task using while loop`

def main():
    move_to_the wall()

def move_to_the_wall(); 
    while front_is_clear():
        put_beeper()
        move()

The result of the task performed by Karel is shown below

A program is executed one line at a time. The while loop checks its condition only at the start of the code block and before repeating.

If Statement

This is a conditional statement that checks if a condition is true before executing the task.

You can write the syntax for if statement as shown below

def main():
    safe_pick_up()

# note the indentation
def safe_pick_up():
    if beepers_present():
        pick_beeper()

If-Else statement

You are going to use else statement to command Karel what to do if a certain condition set is not true.

def main():
     invert_beepers()

def invert_beepers():
    if beepers_present():
        pick_beeper()   # note indenting
    else:
        put_beeper()

Steeple Chase

Now, you will use all what you have learned in control flow to command Karel the robot to perform a major task.

This task is a kind of simulation that shows Karel as an athlete in a relay race that involves running and jumping some hurdles before crossing the finish line.

Your goal here is to get Karel to the finishing line which is number 9 horizontally. To do this, you will be focusing on one steeple at a time using a small line of code as possible.

You will command Karel to turn_left(), followed by using a while loop to command Karel to move up while his right is still blocked. Then you will command Karel to turn_right(), move() and turn_right() again.

def main():
    turn_left()
    while right_is_blocked():
        move()
    turn_right()
    move()
    turn_right()

def turn_right():
    for i in range(3):
        turn_left()

Testing your code above will get Karel into this position

The next step here is to get Karel to move down to the wall using move_to_the_wall(), and then you will instruct Karel to turn left Karel does not understand this descriptive name so you need to define it using a while loop.

Note - use proper indentation where necessary

def main():
    turn_left()
    while right_is_blocked():
        move()
    turn_right()
    move()
    turn_right()
    move_to_wall()
    turn_left()

# defining move_to_the_wall()
def move_to_wall():
    while front_is_clear():
        move()

def turn_right():
    for i in range(3):
        turn_left()

The Output of the code you wrote above is shown below

Restructuring Your Code

To reduce your line of code, You will re-define your code using another descriptive name such as ascend_hurdle() and descend_hurdle() .

def main():
    ascend_hurdle()
    move()
    descend_hurdle()

#defining ascend_hurdle()
def ascend_hurdle():
    turn_left()
    while right_is_blocked():
        move()
    turn_right()

#defining turn_right()
def turn_right():
    for i in range(3):
        turn_left()

# defining descend_hurdle()
def descend_hurdle():
    turn_right()
    move_to_wall()
    turn_left()

# defining move_to_the_wall()
def move_to_wall():
    while front_is_clear():
        move()

Next, you are going to use another descriptive name called jump_hurdle() which will contain ascend_hurdle() and descend_hurdle() . Using a for loop, you will instruct Karel to jump the hurdles 8 times because there is 8 hurdles on the field track.

from karel.stanfordkarel import 
def main():
    for i in range(8):
        jump_hurdle()

# defining jump_hurdle()
def jump_hurdle():
    ascend_hurdle()
    move()
    descend_hurdle()

#defining ascend_hurdle()
def ascend_hurdle():
    turn_left()
    while right_is_blocked():
        move()
    turn_right()

#defining turn_right()
def turn_right():
    for i in range(3):
        turn_left()

# defining descend_hurdle()
def descend_hurdle():
    turn_right()
    move_to_wall()
    turn_left()

# defining move_to_the_wall()
def move_to_wall():
    while front_is_clear():
        move()

# This is "boilerplate" code which launches your code
if __name__ == "__main__":
    run_karel_program()

Now, running the code above will make Karel cross and jump any hurdle no matter the height

Mountain Karel

Mountain hiking is among the most fascinating solo sport today whereby the hikers do consider some factors like weather before embarking on the journey. As dangerous as it may seem the hikers do erect a flag whenever they reach the summit of a mountain making them feel like conquering the world.

In this challenge, you will be teaching Karel the robot to hike a mountain and command Karel to place a beeper at the peak of the mountain before descending. It sounds cool, right? Let get started

Decomposition

Firstly, you will create a descriptive name called step_up() which will teach Karel how to climb a mountain step by step.

Remember - Descriptive names will be snake_case()

def main():
    step_up()

# defining step_up()
def step_up():    
    turn_left()
    move()
    turn_right()
    move()

# defining turn_right()
def turn_right():
    for i in range (3):
        turn_left()

Now let run the code above

Ascending Mountain

Now that Karel has climbed the first step of the mountain, you will create another name called ascend_mountain() which will contain all the commands in step_up().

You will now use a while loop to instruct Karel to climb to the summit of the mountain and then place a beeper (using put_beeper()) which will signify that Karel has reached the peak of the mountain.

def main():
    ascend_mountain()
    put_beeper()

# defining ascend_mountain()
def ascend_mountain():
    while front_is_blocked():
        step_up()

# defining step_up()
def step_up():    
    turn_left()
    move()
    turn_right()
    move()

# defining turn_right()
def turn_right():
    for i in range (3):
        turn_left()

Now, run the code again

Descending Mountain

Here, you are going to create a descriptive name called step_down() which will teach Karel how to climb down the mountain.

#Teacking karel how to step down
def step_down():
    move()
    turn_right()
    move()
    turn_left()

Running the code will make Karel step down the mountain as shown below

To avoid repetition of your code, you will create another name called descend_mountain() which will contain all the commands in step_down().

You will now use a while loop to instruct Karel to climb down the mountain using descend_mountain()

from karel.stanfordkarel import 
def main():
    ascend_mountain()
    put_beeper()
    descend_mountain()

## defining ascend_mountain()
def descend_mountain():
    while front_is_clear():
        step_down()

#Teaching karel how to step down
def step_down():
    move()
    turn_right()
    move()
    turn_left()

# defining ascend_mountain()
def ascend_mountain():
    while front_is_blocked():
        step_up()

# defining step_up()
def step_up():    
    turn_left()
    move()
    turn_right()
    move()

# defining turn_right()
def turn_right():
    for i in range (3):
        turn_left()


# This is "boilerplate" code which launches your code
if __name__ == "__main__":
    run_karel_program()

Now if you run the code, Karel can easily step down the mountain without crashing.

Using the code you wrote above, Karel can now easily hike and erect a beeper at the top of a mountain irrespective of the height of the mountain.

Source - You can have access to the source code on my GitHub repository

Conclusion

Congratulation! You have successfully learned and implemented basic Applications of AI and Machine learning in Robotics by teaching and instructing Karel the Robot to hike a mountain successfully and also partook in a hurdle race.

I believe you found this article impactful and easy to understand. Now you can also train a robot to reason like a human and perform some specific task for you using basic AI and Machine learning. Isn't that fascinating?