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.
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.
- Ozeki 10 installed: http://www.ozeki.hu/index.php?owpn=6066
- Ozeki Dozer: http://www.ozeki.hu/index.php?owpn=6203
- Lego connection installed: http://www.ozeki.hu/index.php?owpn=6067
- SNAP basics: http://www.ozeki.hu/index.php?owpn=6180
- A can of cola
Figure 2 - A program code that grabs a cola can
Download the program
The program is capable to grab and move a can of cola.
It is easier to do the steps if you have the program in front of you:
Download the program code
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
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
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
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
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
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.
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.
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.
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.
Test the application!
- Become a Robot Master
- Build your first App in C#.Net
- Send messages from your C#.Net App to your Lego robot
- Receive messages from your robot and handle events
- Build a user interface for multiple robots
- Create a C#.Net version of the Barrier Control App
- Build a responsive user interface for various screen sizes
- Create a data class
- Learn to use an item manager
- Automatically generated grids and forms