a1tD0000007EUCTIA4

ANDROID 1: Introduction to Algorithms

  • 1-5 grade

Lesson Description:

Introduction to the RobotLAB Autonomous Car and the Android programming environment.

image description

Lesson Modules


Teaching Tips:

  1. Before the lesson starts, demo this code:

ActivityMain

forwards = (Button) findViewById(R.id.bt_forwards);
forwards.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
sendBuf_byte[4] = 1;
sendBuf_byte[5] = 2;
sendBuf_byte[6] = 0;
sendBuf_byte[7] = (byte) (300 / 256);
sendBuf_byte[8] = (byte) (300 % 256);
sendBuf_byte[9] = 0;
sendBuf_byte[10] = (byte) (300 / 256);
sendBuf_byte[11] = (byte) (300 % 256);
sendBuf_byte[23] = (byte) (0x03);

sendByte(sendBuf_byte);
}
});

left = (Button) findViewById(R.id.bt_left);
left.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
sendBuf_byte[4] = 1;
sendBuf_byte[5] = 1;
sendBuf_byte[6] = 0;
sendBuf_byte[7] = (byte) (300 / 256);
sendBuf_byte[8] = (byte) (300 % 256);
sendBuf_byte[9] = 0;
sendBuf_byte[10] = (byte) (300 / 256);
sendBuf_byte[11] = (byte) (300 % 256);
sendBuf_byte[23] = (byte) (0x03);

sendByte(sendBuf_byte);
}
});

right = (Button) findViewById(R.id.bt_right);
right.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
sendBuf_byte[4] = 1;
sendBuf_byte[5] = 3;
sendBuf_byte[6] = 0;
sendBuf_byte[7] = (byte) (300 / 256);
sendBuf_byte[8] = (byte) (300 % 256);
sendBuf_byte[9] = 0;
sendBuf_byte[10] = (byte) (300 / 256);
sendBuf_byte[11] = (byte) (300 % 256);
sendBuf_byte[23] = (byte) (0x03);

sendByte(sendBuf_byte);
}
});
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="forward"
android:id="@+id/bt_forwards"
android:layout_centerVertical="true"
android:layout_alignLeft="@+id/txtStatus"
android:layout_alignStart="@+id/txtStatus" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="left"
android:id="@+id/bt_left"
android:layout_centerVertical="true"
android:layout_alignLeft="@+id/txtStatus"
android:layout_alignStart="@+id/txtStatus" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="right"
android:id="@+id/bt_right"
android:layout_centerVertical="true"
android:layout_alignLeft="@+id/txtStatus"
android:layout_alignStart="@+id/txtStatus" />
private Button forwards;
public static byte[] sendBuf_byte = new byte[28];

private Button left;
public static byte[] sendBuf_byte = new byte[28];

private Button right;
public static byte[] sendBuf_byte = new byte[28];

This code makes the car controllable using your phone. It can go forward, left, right.


2. Next, talk about the Autonomous cars currently on the market. Have any of the sudents seen these cars before?


3. Here are some possible answers to Question 1: 

a. What to do at a green light.

b. What to do when it senses another car in front of it.

c. When to turn on the headlights.


Have you ever seen an autonomous car?

An autonomous (AWW-TAW-KNEE-MUSS) car is a car that can drive itself!  Sometimes it is called a self-driving or driverless car.




How does it work?


 

 

There are no self-driving cars for sale today, but perhaps there will be soon.  Google Inc. is currently developing a self-driving car that is being tested in California.



 

 

Uber Inc. is testing their own autonomous cars in Arizona.




 


  

How did your teacher control the car?

Autonomous cars use sensors to “see” their environment.  People use sensors too. We have eyes that see, noses that smell, and ears that hear. But just seeing isn’t enough.  The things we see, hear, and smell help us make decisions.  Your eyes don’t tell your body to stop when you reach the finish line at a race, your brain does!

Your autonomous car has a brain that it uses to understand the information from the sensors.  Instead of a picture, sound or smell, the car gets a number.  When a race car is speeding down the track, the driver inside knows when to turn and when to stop.  That’s because someone taught him how.  Autonomous cars need to learn how to drive too.  We “teach” them what the numbers mean using code.

 

What are some things the car needs to learn in order to drive?  



Teaching Tips:

At this point the teacher should do the PB&J experiment as outlined here:

  1. Bring a loaf of bread, a jar of peanut butter, a jar of jelly and a butter knife to class on the day of the lesson.  (Components for other sandwiches and food could work too, but PB&J is simple and cheap)
  2. Put the students into small groups and tell them to write step-by-step instructions for making a PB&J sandwich.  
  3. Have one group read their instructions aloud, and act them out exactly.  EX: if step one is “take a piece of bread and put some peanut butter on it” take a piece of bread and set the whole jar of peanut butter on it.  
  4. After you act out the first set of instructions, have the students revise their lists to be more specific.
  5. A successful list will look something like this:
    1. Take two pieces of bread out of the packaging, and set them on the desk.  

    2. Open the jar of peanut butter using a twisting motion with your right hand

    3. Set down the peanut butter jar, pick up the knife

    4. Put the knife in the jar, and take it out again

    5. Swipe the knife across one piece of bread

And so on.


 

What is an algorithm?

An algorithm is a set of rules a computer follows to complete a task.  It can be short or long, but it must be very specific.  


Why is that?  

Think about that question as you watch the demonstration!

 


  

Here is an example of a tooth-brushing algorithm:

Put toothbrush under faucet

WAIT 1 second

Take toothbrush out of faucet

Squeeze toothpaste onto toothbrush

Put toothbrush on teeth

While (teeth = dirty)

{

    Move brush left

    WAIT 0.5 second

    Move brush right

}


 



Write down the steps for your final sandwich algorithm here.


Teaching Tips:

What does this code mean?

forwards = (Button) findViewById(R.id.bt_forwards);
forwards.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
sendBuf_byte[4] = 1;
sendBuf_byte[5] = 2;
sendBuf_byte[6] = 0;
sendBuf_byte[7] = (byte) (300 / 256);
sendBuf_byte[8] = (byte) (300 % 256);
sendBuf_byte[9] = 0;
sendBuf_byte[10] = (byte) (300 / 256);
sendBuf_byte[11] = (byte) (300 % 256);
sendBuf_byte[23] = (byte) (0x03);

sendByte(sendBuf_byte);
}
});

Lines No.4 to 12 are for contents of setting variable values to control the ALTINO motor. The function sendByte on the line No.14 is for sending the value saved in the buffer to ALTINO through Bluetooth communication.



Emphasize that most of their answers make logical sense, but the robot is like a toddler.  It only knows a few words and phrases, and it’s easy to put things in an order it cannot understand.

 

Before we make an autonomous car, let’s make a car you can control directly.  At the end of this lesson, you will be able to drive this car yourself using your PC computer!  




Step 1 

The first thing your car needs to do is go forward.  What algorithm do you need to write to make the robot drive forward?

This time the answer will be provided.

forwards = (Button) findViewById(R.id.bt_forwards);
forwards.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
sendBuf_byte[4] = 1;
sendBuf_byte[5] = 2;
sendBuf_byte[6] = 0;
sendBuf_byte[7] = (byte) (300 / 256);
sendBuf_byte[8] = (byte) (300 % 256);
sendBuf_byte[9] = 0;
sendBuf_byte[10] = (byte) (300 / 256);
sendBuf_byte[11] = (byte) (300 % 256);
sendBuf_byte[23] = (byte) (0x03);

sendByte(sendBuf_byte);
}
});

 

Step 2

Make a button so you can call this function any time you want. Remember to put this in content_main, where your user interface lives.

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="forward"
android:id="@+id/bt_forwards"
android:layout_centerVertical="true"
android:layout_alignLeft="@+id/txtStatus"
android:layout_alignStart="@+id/txtStatus" />


 Step 3

Finally, you need to declare an object for the button you just made, and make a variable where you can save all the information to be sent to the car.

private Button forwards;
public static byte[] sendBuf_byte = new byte[28];




Step 4

Knowing what you know about making the car go, what do you think you’ll need to do to make the car stop?  

  1. An algorithm that tells the car to stop.
  2. A button so you can stop the car on command.
  3. Code to declare the button/save data directions to send to the car.

Hint: See the code from Steps 1-3.




Extra Challenge:

Modify your forward code so that the button says "left" and the car moves to the left when you press it.


Hint:  See array [5] in your function from before and see what you can change to send the car a different message.


Teaching Tips:

Share the students' answers with the class. If time is allowed, have a quick discussion about what went well and what could have been done differently better. 


Question 1: Check the concepts you understand.  
This is the students' self-assessment of their understanding of the material.  You will see a bar graph once all the poll answers are submitted.  

Question 2: What is an algorithm?
An algorithm is a set of (specific) rules a computer follows to complete a task.  (Anything similar to this answer is correct.)

Question 3: What was the coolest thing you learned today?
Any answer will do here.  If time permits have a short discussion with students about their answers.


AFTER CLASS
Store Student Files
Put the student files on a thumb drive or store them in some way so that the students can continue working on their project in the next lesson. 

Pack Up Hardware
Turn off the robots and computers and pack the robots in the transport case accordingly. 

Don’t worry, this isn’t for a grade, it’s just so your teacher can check the classes’ understanding.

 

Check the concepts you understand.  Don’t worry, this isn’t for a grade, it’s just so your teacher can check the classes’ understanding.
  • Algorithm
  • Button
  • Array
  • Bluetooth
  • Autonomous car

What is an algorithm?

What was the coolest thing you learned today?  Write your answer in the box, and see it appear with your classmates answers!