Scratch4Robots

From jderobot
Jump to: navigation, search

Scratch4Robots[edit]

Introduction[edit]

This tool allows to program complex robots like TurtleBot or drones with the visual language Scratch. Regular blocks from Scratch can be used in applications with Scratch2JdeRobot. In addition several blocks for some robots have been also created and can be included in the applications.

Blocks included in Scratch[edit]

Name Type Description
Wait () secs Control Block Pauses the script for the amount of time.
Forever Control Block A loop that will never end
If () then Control Block Checks the condition so that if the condition is true, the blocks inside it will activate
If () Then, Else Control Block Checks the condition so that if the condition is true, the blocks inside the first C will activate and if the condition is false, the blocks inside the second C will activate.
Repeat () Control Block A loop that repeats the specified amount of times
say () Looks blocks Print what you enter as parameter
Set () to () Variables blocks Sets the specified variable to the amount
Insert () at () of () List blocks Insert item in the selected position of indicated list
Item () of () List blocks Returns the item stored in the indicated position
Add () to () List blocks Insert item in the list
Delete () of () List blocks Remove the item stored in the indicated position

Blocks for drones[edit]

Name Type Inputs Outputs Description
Pose3D Perceptive XYZ position Get the value of the robot 3D position
Color detection Perceptive color SIZEimg, Ximg, Yimg Colored object in image
stop robot Motion Reset all velocity values
drone take off Motion Makes the drone take off
drone land Motion Makes the drone land
drone move Motion direction: (forward, back, up, down, left, right), speed: {velocity (integer)} Gives the drone a speed in the indicated direction
drone turn Motion direction: (left, right), speed: {velocity (integer)} Gives the drone a turning speed in the indicated direction

Blocks for TurtleBot[edit]

Name Type Input Parameters Output Description
Pose3D Perceptive XYZ position Get the value of the robot 3D position
Color detection Perceptive color SIZEimg, Ximg, Yimg Colored object in image
Frontal distance Perceptive The average measure of the frontal laser data Get the average value for the values of the frontal laser
robot move Motion direction: (forward, back, left, right); speed: (integer) Gives a speed in the indicated direction
robot turn Motion direction: (left, right); speed: (integer) Gives a turning speed in the indicated direction
robot move to position Motion direction: (forward, back, left, right); meter: (integer) Move robot the indicated meters in one direction


Prerequisites[edit]

Scratch 2.0[edit]

   $ wget -O adobe-air.sh http://drive.noobslab.com/data/apps/AdobeAir/adobe-air.sh
   $ chmod +x adobe-air.sh; sudo ./adobe-air.sh

Download the scratch file from here: https://scratch.mit.edu/scratchr2/static/sa/Scratch-456.0.2.air and then double click to install it.

ROS[edit]

Follow the ROS tutorial installation: http://wiki.ros.org/kinetic/Installation/Ubuntu

Python requirements[edit]

   $ git clone https://github.com/JdeRobot/Scratch4Robots
   $ cd Scratch4Robots
   $ pip install -r requirements.txt

Gazebo and worlds[edit]

Follow the Gazebo tutorial installation: http://gazebosim.org/tutorials?tut=install_ubuntu

With this line we get some prepared worlds based on ROS ready to use:

   $ sudo apt-get install ros-${ROS_DISTRO}-kobuki-gazebo

Installing[edit]

Full installation video[edit]

Install our ROS package[edit]

   $ sudo apt-get install ros-kinetic-scratch4robots


Install Scartch4Robots extension to use in Scratch[edit]

In scratch keep pressing shift key while you click on "File" > "Import experimental HTTP extension" and add our extension. The extension will be stored in the "extension" directory of your package.

   Note: This step is necessary each time you initialize Scratch

Download the tool from git[edit]

   $ git clone https://github.com/JdeRobot/Scratch4Robots.git

Now your package is ready to use.

How to Use[edit]

Make a scratch project[edit]

Make a scratch project and save it.

Make the translation from Scratch to python[edit]

Generate the code:

   $ rosrun scratch4robots scratch2python /path/to/your/scratchproject.sb2


This command will generate the python script on your current work directory


Launch the simulated world[edit]

For example:

   $ roslaunch kobuki_gazebo kobuki_empty_world.launch --screen


Execute the generated code[edit]

The generated code need a configuration file .yml as parameter, you will find some usefull configuration files in our git repository.


    $ ./myscratchfile.py /path/to/robot_ros.yml

Running an example[edit]

You have all you need for running the examples in the examples folder

How is this tool built?[edit]

Translation from Scratch to Python[edit]

To perform the block translation we use the Scratch2python.py script that parse our blocks replacing them with their corresponding code in python. This script uses kurt library to load the scratch project

How to add a new Scratch block to the extension[edit]

  • Add block code to scratch2robot.s2e
   ["", "move robot %m.robotDirections meters %n", "robot/move/meters", "forward", 1],
   ["r", "size of object", "camera/size"],
   ["r", "x position of object", "camera/x_pos"],
   ["r", "y position of object", "camera/y_pos"],
  • Add block code to scratch2python.py
   ['move robot {} meters {}', 'robot.move_meters("%s", %s)'],
   ['size of object', 'robot.get_size_object()'],
   ['x position of object', 'robot.get_x_position()'],
   ['y position of object', 'robot.get_y_position()'],
  • Add block code to robot.py or drone.py
   def move_meters(self,direction, meters=None)
      //code    
   def get_size_object(self):
       //code
       return size
   def get_x_postion(self):
       //code
       return x_position
   def get_y_position(self):
       //code
       return y_position
  • Add block code to kurt extension in commands_src_extras.py
   [' ', 'move robot %m.robotDirections meters %n', 'Scratch2JdeRobot/robot/move/meters', 'forward', 1],
   ['r', 'size of object', 'Scratch2JdeRobot/camera/size'],
   ['r', 'x position of object', 'Scratch2JdeRobot/camera/x_pos'],
   ['r', 'y position of object', 'Scratch2JdeRobot/camera/y_pos'],

Note: You can find more information about how to create a extension in ExtensionDoc