Transcription of Controlling Robobuilder Humanoid robot using Lisp/L#
1 Controlling Robobuilder using L#PhilPage 106/04/2010 Controlling Robobuilder Humanoid robot using Lisp/L# If you want to give a gripper for your Robobuilder , order it now from my Shapeways shop at And don t forget to order a servo from Robosavvy to go with 2 Controlling Robobuilder using L#PhilPage 206/04/2010 IndexIntroduction .. 3 Lisp .. 4 Car, Cdr, Cons, List .. 6So how do these work? .. 6 Function and conditionals .. 8 Controlling the robot .. 10 More PC Remote mode functions .. 13 Menu driven motions .. 15 Accessing the accelerometer.. 17 Access the servo 19 How to play servo 21 Synchronous moves .. 23 Smooth moves and the In-betweens .. 24 Making complete 25 Loading data from CSV files .. 27 Controlling the IO on a servo .. 29 Windows based graphics using L# !.. 30 Plotting and animation .. 32 Real-time accelerometer .. 35 Property 37 Blockworld / 39 Searching and matching .. 43 Adding a gripper with dynamic control of a 46A 48 Joystick.
2 51 Speech synthesis and recognition!.. 54 Useful features and functions .. 55 Concluding Remarks .. 58 Concluding Remarks .. 58 Appendix A Function 60 Appendix B File 63 Controlling Robobuilder using L#PhilPage 306/04/2010 IntroductionThis document is a tutorial guide on programming a Robobuilder (1)series Humanoid robot using Lisp based dialect called (2) (or L#). It will explain the basics of Lisp although rather briefly if the reader has no knowledge of Lisp you may well want to look at other books and websites that cover the Lisp language in more detail. This guide will then look at how to control the Robobuilder robot remotely from a PC using .NET. It will show how to control individual servos on the robot , and access its sensors such as the distance sensor and the accelerometer. It then looks at advanced control techniques such as dynamic control of a gripper, using the distance sensor for navigating a maze and accelerometer for auto-balance control.
3 Later chapters deal with interfacing with Windows to provide simple graphics and different input methods such as joystick and speech libraries to control the robot . The final chapters cover a basic introduction to some AI techniques to improve the intelligence of your document has been based on use of (L#), a dialect of Lisp built using .NETframework. Its big advantage is its ability to hook into .NET windows libraries including the standard ones such as and also DirectX and Speech (.Net V3) and my own for Controlling the the files required to run the code in the document can be download from the internet and links can be found in Appendix B. A summary of the Lisp/L# functions is Appendix AThroughout the document are code examples and example sessions. Lisp/L# is an interactive and immediate language and the reader is encouraged to read and try the examples out at the same time. In the examples user input is colour blueand application program output coloured : (1) Robobuilder is trademark of Robobuilder co (see details) (2) / L# are Copyright Rob BlackwellControlling Robobuilder using L#PhilPage 406/04/2010 Lisp Lisp was invented by John McCarthy in 1960 - So this year is its you re not familiar with Lisp it stands for list processing - this particular lisp (L#) uses a dialect based on arc a description can be found here: are a few simple examples to get you started.
4 First run the command line environment and get a prompt: > C:\> Sharp on Rob Blackwell. All rights reserved.> (+ 2 3 4 5) 14 Assuming you have downloaded and run the console program you should be able to try out the above example. Lisp is a very simple language, it consists entirely of list of thing contained within brackets (). It evaluates the list supplied and returns a result. In the above example (+ 2 3 4 5) is a list that return the result 14. The console program evaluates the list by looking at the first atom in the list (in this case +) and using that as a function to process the remaining elements in the list, in this case, summing the remaining elements. It would also correct to write the following (+ (+ 2 3) (+ 4 5)). Lisp would evaluate the inner elements first to give (+ 5 9) and the sum the outer elements giving things in LISP are atoms and atoms are formed into list by (). So (+ 1 2) represents a list of 3 atoms and if the list is evaluated the + is taken as function and 3 is the s another list:> (= x 5)5> x5 This shows how to assign a value to the atom x.
5 If we type x to the prompt it evaluates to value of x which in this case is 5. We can now use x in an expression here using + function which adds values:> (+ x x)10> x5 Note although (+ x x) added the value of to itself it didn t change its value. Atoms can also be made of ASCII characters - so (a b c) or (a b 1 3) are mentioned that Lisp is basically just atoms and list - or s-expressions as they are called. So what is a list and do we work on them? The simplest way is to do a few examples> (a b c d)(a b c d)> (1 2 3)(1 2 3)Note the quote ( ) - this is important! It means don t evaluate the list - take its literal contents. Atoms can be numbers or symbols, and numbers can be integers or floating points (real numbers). Controlling Robobuilder using L#PhilPage 506/04/2010 And lists can be me a mixture of elements (and even other lists)> (a b 1 2 )(a b 1 2 )> ( (a b) (c d))((a b) ( c d))> (= x (a b c))(a b c)> x(a b c)But atoms can also be functions - which is where Lisp gets its power - it s a user definable symbolic processing language that can be written in itself using just a few primitivesSo (+ 1 2) is just another list:> (+ 2 3)(+ 2 3)> (= x (+ 2 3))(+ 2 3)> x(+ 2 3)To load files into Lisp/Lsharp you use the function load.
6 So with you favourite text editor (vi or notepad) create a file . Add the following content to the file: (prn Hello world )Now start :> (Load )Hello worldNote: it runs the file immediately displaying the result to the Robobuilder using L#PhilPage 606/04/2010 Car, Cdr, Cons, ListTwo assembly language routines for the IBM 704 became the primitive operations for decomposing lists: car (Contents of Address Register) and cdr (Contents of Decrement Register). Lisp dialects still use car and cdr (pronounced / k?r/ and / k?d?r/) for the operations that return the first item in a list and the rest of the list respectively.(from wikipedia)So how do these work? car- a function that returns the first item in the list (some Lisp dialects call it first) cdr- a function that return the rest of the list (some Lisp dialects call it last) cons- construct a new list element list a function to create a new list from a list of elements> (= x (1 2 3 4 3 2 1))> ( car x)1> (cdr x)(2 3 4 3 2 1)These of course can be nested to access any element> (cdr (cdr ( cdr x)))(4 3 2 1)Also if you have an empty list ()> (cdr ())nullThis becomes important when you create recursion functions that strip off elements as they cdrbreaks lists apart - how do we stick them back together?
7 This is where consis used.> (cons a (1))(a 1)> (cons b x)(b 1 2 3 4 3 2 1)A more complex example is adding a list to another list> (cons (a b) (c d))((a b) c d)So what would I get if I took car of the newly constructed list??> (car ((a b) c d))(a b)A list is returned - the first element. Did you try to create like this?> (cons a b)Exception : Not a sequenceControlling Robobuilder using L#PhilPage 706/04/2010 This is because consexpects its second argument to be a list (or null). So to do the above you need> (cons a (cons b nil))(a b)An alternative way to this is to use the listfunction as follows> (list 'a 'b)(a b)The list function builds list using cons for you and so makes thinks a lot simpler ! Controlling Robobuilder using L#PhilPage 806/04/2010 Function and conditionals When a list is processed or evaluated the first atom is treated as a function. This function can be define by the user, here s an example> (def hello () (prn hello world )) > (hello)hello world hello world So what s going on here?
8 The first line defines a function called hello that takes no arguments (). The functions then calls prnto print with new line. Note how def is a function that returns a type I then invoke the function by using it in a list (hello). So that hello is evaluated. The output is hello world , and the return value from prnfunction hello world is also s an example of a simple function with an argument:> (def addone (x) (+ x 1)) > (addone 5)6> (addone (addone (addone 7)))10 You might try and see what happens if you addoneto a list. To make functions useful we need one more element, the conditional. In LISP this is normally called cond however in this Lisp dialect it instead uses the more familiar (to non LISP people!) if function. ifrelies on the fact that the atom t mean true and null is not true. So a few examples:> (if null true false)false> (if t true false)true> (if (> 3 5) true false)falseTo use an if statement you need a predicate - a function that return true (t) or false (null), such as is x greater than y ?
9 Which would be written (> x y), or is x equal to y ? written as (is x y). b]if[/b] statement can be compound so they are very similar to the old style LISP cond statements, (if a b c d e) means if a then b elseif c then d else e. A few examples:> (= x 1)1> (if (is x 1) one (is x 2) two no)one> (= x 2)2> (if (is x 1) one (is x 2) two no)two> (= x 3)3> (if (is x 1) one (is x 2) two no)noSo how to create a function spell that uses an if statement and outputs any number up to 99 (without listing all 99 values)? We need to have multiple if statements: Controlling Robobuilder using L#PhilPage 906/04/2010(def spell (x) (progn (if (> x 29) (progn (= x (- x 30)) (pr thirty ))) (if (> x 19) (progn (= x (- x 20)) (pr twenty ))) (if (is x 1) one (is x 2) two (is x 3) three (is x 4) four (is x 5) five (is x 6) six no ))) >(spell 1)one> (spell 2)two> (spell 21)twenty one> (spell 24)twenty four> (spell 20)twenty no>Important: look how to chain instructions you need a function - called progn.
10 You can t simplygo ((pr hello ) (pr world )) - can you see why ? The first element of the list isn t a function - its a list ! So it errors. The correct way to write this is (progn (pr hello ) (pr world )).Also: twenty no ??? - Well it s almost there - can you think how to improve this? Controlling Robobuilder using L#PhilPage 1006/04/2010 Controlling the RobotSo what is the process for Controlling Robobuilder from within L#. Here s a diagram outline the basic connection115200, 8, N, 1 RBC = RoboBuilderControllerFirmware: or aboveservosWindows PC Running XP or loaded(this could be a Home PC/Desktopor a miniature PC such Roboardmounted on back of the robot )LISP / L# Code (connect COM1 ) CLRstartPC RemoteDC modeBluetooth / or direct linkFigure 1 The LISP program or functions (in yellow) are defined in a file and loaded and run within the console application. This enables Lisp programs to access .NET dynamic libraries such as the built in.