Drunken tribot v1

From ZENotes
Jump to: navigation, search



 Learning nxc with Lola: drunken tribot1, overly simple version with 1 random move.

task main()                                 // start of real code
	SetSensor(IN_1,SENSOR_TOUCH);       // We'll need  the Touch Sensor in Port 1, OK?
	repeat(4)                           // Say, we bump 4 times & then he's out.
		do                          // do the following until...
			OnFwd(OUT_AC, 60);  // Forward 60%
		until (SENSOR_1 == 1);      // ...BUMP! CRASH!
		  Off(OUT_AC);              // DEBUG: need this to get a clean change of direction
		  OnRev(OUT_AC, 60);        // both Motors backs off
		Wait(600);                  // duration
		  Off(OUT_AC);              // DEBUG: otherwise motors just won't stop!
		if (Random() < 0)           // IF statement, what happens if... the value is BELOW Zero:
			Off(OUT_A);         // DEBUG: otherwise motor A just won't stop!
			OnRev(OUT_C, 60);   // Motor C backs off
		else                        // ELSE statement: returned value is ABOVE zero.
			Off(OUT_C);         // DEBUG: otherwise motor C just won't stop! 
			OnRev(OUT_A, 60);   // Motor A backs off
		Wait(800);                  // duration
	Off(OUT_AC);                        // after 4 "rounds", software reach this point: Off A & C
 }                                         // End of (little) Code. Lola out. Copyleft.


From http://bricxcc.sourceforge.net/nbc/nxcdoc/NXC_tutorial.pdf, I took inspiration to start a drunken tribot; in my slang, he's called "tridiot", to work on randomizations.

This v1 of file come actually 4th in my learning process: I had to simplify, get to a bare minimum in order to be sure everything that appeared random in this snippet was actually random according to MY plans :)

My main finding, reflected here, is that you have to Stop the Wheels at all time; without such instruction their coarse time may be as long as your little extra-randomized 1/2 second move and it won't fit; that explains why there is all these Off(OUT_XX); all around.

As a general recommendation, as soon as you know how to make music and how to make a 'bot moves forward you'll wan't a mobile jukebox: well, always work it progressively. If you look at the text layout, you can see ther's Blocks in C-like code after all: the { }; stuff, and it is usefull to copy, paste, move them around like you move blocks in NXT-G.

Don't be afraid to just copy "blocks" from elsewhere and tweak/adapt them, it is a safe way to start bug-free. Lola out

Now, what to do with this file?

Save it as plain text under any name that suits you followed by .nxc since this is an nxc snippet (hence not an .nbc one), for me it is tridiot1.nxc. From wherever you saved the file into, compile it using J. Hansen nbc compiler or, if you are not on *nix, *bsd, mac-something, use brixCC integrated compiler. Now all that's remain is to put it on the brick, and start it! Each time it will bump in something, the bot will move backwards and turn right or left at 60% for 8/10th of a second. That's obviously a good starting point to fool around with the snippets, modifying these parameters to suit you and your bot.

As such, this piece of code should run with any 'bot with two motors, respectively plugged into A and C ports of the NXT Brick, and equipped with a touch sensor in front.

It is your responsibility, ever-ever, to review the code prior to compile, download and execute it on your machines; while I don't see much room for terrifying exploits in the MindStorms context, it is still yours; that's the burden, and the goodness, of OpenSource.

What's Next?

To improve tridiot1, we could have a more randomized way of moving forward, as well as to how much he steers away from from the obstacle after colliding with it; also one could add al sorts of "HuK1" and "Sorry Sir" sound to it...

refs & help

here are the references to the various tools & resources needed. 11:31, 5 July 2008 (UTC)

Personal tools