Course 10 / Lecture 4:

Convert a SNAP app into C#

In this lecture you will first build an app in SNAP, that directs your robot to steal a can of coke (Part 1). Next you will create the exact same functionality in a C#.Net app (Part 2). By taking this lecture you will learn the procedure to convert any SNAP example into C#.Net. This is great, because the robots can be made much smarter if you take them to the next level by switching from SNAP to C#.Net.

Part 1

Task #1:

Steal a can of cola using a Snap app!


Figure 1 - Steal a can of cola by using this program code on the Dozer

Terms and Definitions

  • Gripper: The claw of the Ozeki Dozer. It consists of two arms which can be open or closed.

Requirements


Program code

Program code
Figure 2 - A program code that grabs a cola can


Step-by-step guide

STEP 1: Preparations

As you read before you will 'steal' an empty can of cola with the Ozeki Dozer. Please put the robot and the can of cola on a flat table or floor. Face to face, approximately 25 centimeters distance from each other (see it on Figure 3). The gripper is closed at first. Connect the robot to Ozeki 10 and open SNAP. You are ready to start programming the robot. The first is the 'when green flag clicked' block, so you can start the cola stealing with the green flag button.


Figure 3 - Starting state

STEP 2: Open the gripper

As you know Ozeki Dozer has motors, in the previous lectures you have used motor A and D. They drive the robot's wheels and the 3rd motor called C moves the gripper. It opens and closes it. In the program your first task is to open the gripper on Ozeki Dozer, so you need a 'Rotate C by 1080 degrees' block. It will rotate motor C counterclockwise to open the gripper. It is enough to rotate motor C by 1080 degrees to open the gripper enough (see on Figure 4).


Figure 4 - Opening the gripper

STEP 3: Grab the can of cola

Now you can go ahead with the robot to the can of cola, so the next block is 'Drive A+D reverse 1.8 sec at 100%'. The last value in the block determines the speed of the motor, in this case it set to full speed. The motors which move the Ozeki Dozer are in a reversed direction thus you have to use the reverse direction in the block to go forward with the robot. Approximately 1.8 seconds is enough to reach the can of cola. The 4th block is 'Rotate C by -1080 degrees at 50%' (see on Figure 5) which will not fully close the gripper, but it is enough to grab the can of cola.


Figure 5 - Grabbing the can of cola

STEP 4: Move the can of cola

After you have grabbed the can of cola you have to move it backwards. Put the following block after the existing ones: 'Drive A+D forward 3 sec at 50%'. You need to select 'forward' to move backwards because as you know the A and D motors are in reversed direction. Then 'Rotate C by 10 degrees' to drop the can of cola. Then move it back for 1 seconds again thus the next block is 'Drive A+D forward 1 sec at 50%'. The last block is 'Rotate C by -1080 degrees at 50%' (Figure 6).


Figure 6 - Stealing the can of cola

STEP 5: Upgrade the program

To steal the can of cola in a smarter way you have to upgrade the program. It will use the IR distance sensor. The goal is that the robot will move towards to the can of cola, when it is near enough it will slow down and when reach it it will stop and grab the cola. To do this you have to modify the 100% value to 10% in the 3rd block. Then insert the following 5 blocks below the 'when green flag clicked' block: 'Drive A+D reverse at 50%', 'wait until Object proximity [sensor] < 25', 'Drive A+D reverse at 20%', 'wait until Object proximity [sensor] < 8' and 'Stop motor A+D'. Select the connected IR sensor as [sensor] in the 'Object proximity [sensor]' blocks (Figure 7).


Figure 7 - Stealing the can of cola smarter

Part 2

Task #1:

Create a new page and a start button in C#!

The following video series will show you how to write a coke stealing app in C#.NET that is similar to the Snap app that can be found in Part 1 of this lecture. First we will create a new page, add it to the appsitemap, the menu and the toolbar. Finally we place a Start button on the webpage.

Task #2:

Prepare your C# app for messaging!

To be able to receive messages from your robot and its sensors, you need to subscribe to its events. First you can learn how to write an AppOut function to send messages to the robot, then send a subscribe message to the Infrared sensor using this AppOut method. To receive messages from the sensor, you also need to subscribe to the OnMessageReceived event of the app that is implemented in the previous lecture (the In method has been overrided in the AppHelloWorld class that can also be seen in this video). If the OnMessageReceived occurs you will get the distance between the infrared sensor and an object (in this case this object is a can of coke). This value is saved in the _cokeDistance variable.

Task #3:

Write the StealCoke function and run it in a Thread!

In the following video you can see how to write a similar app to the previous snap app. Note that some values can be different since we used an other Lego EV3 robot in our simulation environment and it is possible that motors and sensors were connected to different ports on the robot! After you have written the function, subscribe to the OnClick method of the start button and call the StealCoke function in a new thread by pressing the Start button.

Task #4:

Make the program smarter by using a stop button!

It is possible that we need to stop the Lego robot in some cases. In this video we will create a Stop button and set it to true whether we click on it. Furthermore we will place an if (stopNow) return; line under every line where we called the AppOut function to break this function if there is an error.

Task #5:

Test the application!

More information


Next


Copyright © 2000- |Ozeki Ltd | info@ozekirobot.com |
Page: 6161 | 18.223.0.53 | 79.99.42.43 | Login