Welcome to the Lua Visual Challenge. This Challenge is meant to give a demonstration of the Lua Visual Robot Simulation and to gather feedback from you, the user. The challenge will consist of 3 short obstacles that you will need to complete. But remember, have fun!
Challenge 1: Moving Forward
Objective: Drive forwards until the robot reaches the goal.
The picture to the left is your virtual robot. The front of the robot is colored grey as seen in the picture. It has 2 drivable wheels, which we will use to move the robot around the simulation. In order to do this, we will need to program the wheels to turn. You will use the "setLeftMotor(percentage)" and "setRightMotor(percentage)" functions to control the wheels. percentage should be a value from -100 to 100. Here are some examples:
setLeftMotor(100) -- Moves the Left Wheel 100% speed forward setRightMotor(25) -- Moves the Right Wheel 25% speed forward setLeftMotor(-50) -- Moves the Left Wheel 50% speed backwards setRightMotor(-100) -- Moves the Right Wheel 100% speed backwards
Step 1: Open the file called "challenge_1.lua". The file contains 2 functions, "behavior_1()" and "DriveForward()". "behavior_1()" is completed for you, but "DriveForward()" is empty.
Step 2: Inside the "DriveForward()", use the "setLeftMotor(100)" and "setRightMotor(100)" functions to make the robot move forward.
Step 3: Save the file (Ctrl+S). Go to the "Build" menu, and click "Run Simulation". Select "Challenge 1". If you have no coding errors, the simulation will start. If you have coding errors, read the console to see what you did wrong or ask for help.
Step 4: In order to move around in the virtual world, use the Up, Down, Right, Left arrow keys. To change your viewing angle, click on the screen, and move your cursor. If your robot moves forward correctly, you will see the robot go inside the green square, and stop. This means you completed the challenge!
Reality Check
Did you notice anything strange about the robot's movement? Newton's first law of motion states that a object in motion wants to stay in motion. This is the basic concept of Inertia. Our simulator ignores this law, since our virtual robot is able to instantly change speed. If this was a real robot, you would see the robot slowly increase in speed before it reached its max speed. Remember this when programming real robots!
Challenge 2: Detect Walls
Objective: Drive forwards until near the wall. Then drive backwards until the robot reaches the goal.
Now its time to sense our environment. This virtual robot has a range finding sensor on the front which will allow you to program the robot to avoid crashing into walls. To use the range finding sensor, use the "getFrontRange()" function. This will return the distance to the closest wall directly in front of your robot. Here is a example:
-- Drive Forward until a wall is less than 5 units away. if (getFrontRange() > 5) then DriveForward() end
Step 1: Open the file called "challenge_2.lua". The current code will tell the robot to drive up to a wall and stop when within 5 units. But we want to robot to move backwards after it detects the wall.
Step 2: Inside "behavior_1()", call the "ChangeBehavior(2)" function inside the "else" statement. This will switch the behavior to the "behavior_2()" function when the robot gets near the wall.
Step 3: Save the file and run the simulation.
Reality Check
In reality, sensors are not prefect. All sensors have a margin of error, and most sensors can return completely wrong values. In order to use sensors on real robots, we must filter that sensor data and remove any data that might be incorrect. Thankfully, in this virtual world, the sensor values are always accurate.
Challenge 3: Turning
Objective: Drive forwards until near the wall. Then turn the robot 90 degrees to the left, and repeat until the robot reaches the goal.
So far we have only moved the robot forward and backwards. Now its time to turn the robot. There are a few functions you must use to complete a turn. "setDesiredAngle(angle)" should be used to set the amount you want to turn. "angle" should be a positive number for turning left or negative number for turning right. For example:
setDesiredAngle(90) -- Turns the robot 90 degrees left setDesiredAngle(180) -- Turns the robot 180 degrees left setDesiredAngle(-360) -- Turns the robot 360 degrees right
Step 1: Open the file called "challenge_3.lua".
Step 2: Inside "behavior_1()", modify the "setDesiredAngle(0)" to turn the robot 90 degrees to the left.
Step 3: Inside "behavior_2()", add "TurnLeft()" inside the "if" statement. This will cause the robot to turn left until the angle you set is reached.
Step 4: Save the file and run the simulation. If done properly, the robot will keep driving, and not crash into any walls.
Reality Check
How do you think you would program the getAngle() function? On a real robot, it is difficult to determine the current angle of your robot without additional sensors. Sensors such as wheel encoders can be very useful for calculating the angle your robot has turned. More advanced sensors such as gyroscopes or acceleratometers can also be used.