240 likes | 407 Views
IN.1010, IN.0111, IN.0114, IN.1910, IN.0300 Robotics Project Autumn Semester 2013 Lecture 2: Basic Coding & Introduction to the Physical Arena Prof. B. Hirsbrunner (beat.hirsbrunner@unifr.ch) Thomas Rouvinez (thomas.rouvinez@unifr.ch). Today. Goals of Today's Course Comments Variables
E N D
IN.1010, IN.0111, IN.0114, IN.1910, IN.0300 Robotics ProjectAutumn Semester 2013 Lecture 2: Basic Coding & Introduction to the Physical Arena Prof. B. Hirsbrunner (beat.hirsbrunner@unifr.ch) Thomas Rouvinez (thomas.rouvinez@unifr.ch)
Today • Goals of Today's Course • Comments • Variables • Assignment • Constants • Expressions • Events • Navigation • Exercise
Goals of Today's Course • Learn to write ASEBA scripts • Apply programming language basics: • Comment and indent your code • Assign variables • Use constants • Execute expressions • Receive events • Experiment with the use of the e-puck motors
Comments (1) A comment starts with ’#’and ends with the line Commenting your code properly is mandatory • In English (best practice) • It should help you and other people to better understand your code Example : # Comment line 1 # Comment line 2
Comments (2) • Place comments at key locations (above a function, within a portion of cryptic code, etc.) • Place in all your codes a header :##### ROBOTICS PROJECT 2013 # # Author: Sheldon Cooper # Group: 01 # Created: 22.09.2013 # Last modified: 24.09.2013# # Description: e-puck implementation for ... # ###
Code Formatting (1) • Correct formatting makes reading code easier
Code Formatting (2) • Weuseindentation (usuallyonetabcharacter per levelofindentation) • A levelisdefinedby an instruction (do, while, if, etc.)
Code Formatting (3) Withoutformatting oneventevt.lockDoorifargs[0] == col.BLUEthenevt.blueFound = TRUE elseifargs[0] == col.REDthenevt.redFound = TRUE end With proper carriagereturn oneventevt.lockDoorif (args[0] == col.BLUE) thenevt.blueFound = TRUE elseif (args[0] == col.RED) thenevt.redFound = TRUE end With proper carriagereturnandindentation oneventevt.lockDoorif (args[0] == col.BLUE) thenevt.blueFound = TRUE elseif (args[0] == col.RED) thenevt.redFound = TRUE end
Variables • Named reference to one or several values: var demo_variable (also ok: demoVariable) var demo_table[5] • English and lower case letters only • Name should be memorisable and informative • In ASEBA, unsigned integers with 16 bits ranging from -32768 to 32767 0 1 2 3 4
Assignment (1) • Assign an initial value to a variable: var demo_variable demo_variable = 1 var demo_table[5] = 31, -23, 37, -43, 578 1 0 1 2 3 4 31 -23 37 -43 578
Assignment (2) • Change the value of a variable: demo_variable = 2 demo_table[0] = 8 demo_table[3] = 14 2 Variables that can hold multiple values are called arrays! 0 1 2 3 4 8 -23 37 14 578
Constants • A constant is a named reference to a value which doesn't change (can be defined only once) • It's a convention in some programming language that constants are in upper case letters with seperator ‘_’
Expressions (1) • Expressions allow us to do mathematical calculations • Operators (sorted by priority): • Multiplication (*), division (/), modulo (%), • addition (+), substraction (-), shift left (<<), shift right (>>) • You can use brackets '()' to change the priority
Expressions (2) • Bitwise left shift: • The last 0/1 to the left is lost • 0 is inserted on the right 1 << 1 = 2, in bits: 01 << 1 = 10 • Bitwise right shift: • The first 0/1 on the right is lost • 0 is inserted on the left 3 >> 1 = 1, in bits: 11 >> 1 = 01
Internal Events (1) • A robot interacts with the environment by: • Perceiving the environment through sensors • Making decisions with the internal processor • Executing actions with it's motors and leds robot computer sensors motors environment
Internal Events (2) • Internal events happen forexample, whenthevalueswhichareobtainedbythecameraarerefreshed. This happens in regular time intervalsandtherobotgetsnotifiedbytherefresh-events (e1, e2, ...). robot computer e1 e2 e3 e4 e5 e6 e7 sensors motors time environment
Internal Events (3) • Wheneverthecameraobtainsfreshpixelvalues, itreceives an eventandtheprogramcounterjumpstothelabeloneventcameraandexecutesthecodeuntilitreachesthenextlabel. onevent camera # the camera values have been updated ... onevent ir_sensors if behaviour == EXPLORE then ....
External Events (1) • A robot can interact with other robots using events Size of the array variable that is sent with the external event onevent demo_event # TODO: write event received code here # TODO: use arguments received with the event # Use args[0] to get transmitted value
External Events (2) • External events are not caused by sensors, they are created by the programmer to send information to other robots. Remember the code from exercise 1: # e-puck number 4 will execute this sequence onevent backward # TODO: complete the code # right after e-puck number 1 has sent the event emit backward speed # wait loop
External Events (3) • A robot can send an event to another robot with: var event_argument = 1 emit demo_event event_argument
Restrictions • All e-pucks contain a microprocessorwith a limited amountofmemory • Script sizetoobigfortargetbytecodesize • Amountofexecutablecodeis limited by: • Code complexity (stackorheapoverflow) • Code length (dataoverflow) • Write simple andefficientcode
Navigation • Motor: • Numberofsteps per seconds • Positive values == rotatesclockwise • Negative values == rotatescounterclockwise • Trajectory: dependingofthevaluesofspeed.leftandspeed.right, therobot will waitormove : • straightforwardorbackward • straightorwith a curve : rightorleft, forwardorbackward
Exercise • Series 2: Run yoyo-wiggle.aesl in the physical arena