Robotic Animals and NXC with Lego Mindstorms

Definition of a Robot: A Robot is a machine that can be programmed to do a task without being directly controlled by a human being.   There are two parts to a robotic system.

1. Hardware: This is the mechanical robot. The motors, gears, wheels, parts, computers that make up the machine.  This is the part you build.

2. Software:  This is the instructions for the tasks and actions the robot will do.  This is the part you write.

NXC Language is used to create the software that controls the actions of the robot hardware.  NXC is a text based system of assembling instructions for the machine.  The flow of direction usually moves sequencially from top to bottom.  This software is an example of a compiled program.  Programs you write in NXC must be compiled with NBC and downloaded to the NXT Brick using Bricx Command Center before the Robot will perform the program.  (Scratch and Python are "Interpreted Languages.")

The website for NXC and Bricx Command Center:

http://bricxcc.sourceforge.net/

More Online Resources:

http://www.legoeducation.us/store/
http://www.ortop.org/NXT_Tutorial/

Two motors that control the drive wheels are connected to the B and C ports. The A motor controls the Pen.

C = Left Motor
B = Right Motor
A = Pen Motor

The Touch, Ultrasonic, and Light Sensors are plugged into their default positions.

Touch Sensor:       Port 1
Ultrasonic Sensor: Port 4
Light Sensor:         Port 3

Overview of Bricx Command Center IDE:




Basic Bricx / NXC Commands:


Motor (Output) Commands:

OnFwd("ports", "pwr");
OnRev("ports", "pwr");
Off("ports");
OnFwdSync(“ports”, “pwr”, “turnpct”);
OnRevSync("ports", "power", "turnpct");
RotateMotor(“ports”, “pwr”, “angle”);

Flow Commands:

Wait(4000);     (milliseconds)
until (“condition”);
repeat (“value”)
{
   “body”
}

Task and Sub Commands:

Sub methodName()
{
    “body”
}

task main()
{    
     “body”
}

Structure of NXC Commands:




Process and Tutorial:

Select and Build a Robotic Animal!


Select a robotic animal to build. You may choose from this list or design your own! You have 1 hour to build.

Robot Building Plans:


1. Getting Started with programming:
  • Start Bricx Command Center
  • Select “usb” and “NXT”
  • Turn On Robot
  • Plug Robot Into Computer
  • Wait short time for computer to recognize Robot
  • Click “OK”

2. Take a test drive!

Click “Joystick” Tool
Set Motors to C Left and B Right
Try the Arrows! (Make sure the robot does not drive off the table!)



3.   First Robot Program: Out and Back



4.  Compile and Download!
  • Save Your Program (File -> Save)
  • Select “Compile->Compile” from menu bar. (Or “F5”)
  • Turn On Robot
  • Select Compile -> Download
  • Wait for Beep
  • Unplug Robot and Test! (Use Orange Button)
    • Robot: Software -> My Files -> Your Program
    • Select Program with Orange Button

5.  Swing and Point Turns:

The Robot needs to be able to Turn Right and Left. There are two types of turns:

Swing Turns: The Robot rotates one wheel and pivots on the opposite wheel.

Examples:


Left
Right
By Time:


By Rotation Degrees:



Point Turns: The Robot rotates both wheels in opposite directions to pivot on a center point between the wheels.

Examples:


Left
Right
By Time:



By Rotation Degree Left:


By Rotation Degree Right:


6. Using Sensors

You have access to 4 types of sensors with the NXT Robot.

Touch: Port 1
Sound: Port 2
Light: Port 3
UltraSonic (measures distance using sound waves): Port 4

These sensors provide your robot with its perception of the world around it. We will now experiment with using the sensors to "stop" the animal from moving at a certain "input."

Table of input and output codes with NXC.

Sensor Type
Input Command
Get Value Command
Touch
SetSensor(IN_1, SENSOR_TOUCH)
(SENSOR_1 == 1)
Sound
SetSensorType(IN_2, IN_TYPE_SOUND_DB);
(Sensor(IN_2))
Light Active
SetSensorType(IN_3, IN_TYPE_LIGHT_ACTIVE); (Sensor(IN_3)/10)
Light Passive
SetSensorType(IN_3, IN_TYPE_LIGHT_INACTIVE); (Sensor(IN_3)/10)
UltraSonic
SetSensowLowspeed(IN_4);
(SensorUS(IN_4))


Example 1: The Touch Stop

The robot animal moves forward until it touches an object. Then it stops in place.



To use the above command - put the "sub" in the "task main()":


 task main():
{
    TouchStop(50);
}


Example 2: The Light Stop

The robot animal moves forward until it "sees" dark. Then it stops.



Example 3: The Ultrasonic Stop

The Ultrasonic sensor reads distance in centimeters. This program stops when the robot reaches within 15 centimeters.



Example 4: Line Following with while loop counting cycles:



7. Animal Behavior

Using the sensors and motors - write programs to mimic common animal behaviors:

Stinging
Fleeing
Hunting
Line Following
Searching
Getting Out
Seeking LIght

Example of "Snapping Behavior"



Example of "Finding the Way Out"



Here is an example of a program that "Seeks Out" the closet point and moves the animal to touch that point. (Like attacking) the nearest object.


8. Useful subs with Sensors and Motors


To make your programming easier - you may use these two files with several pre-defined subs for movement, turns, and Sensor Stops:

PenSubs.nxc
SensorSubs.nxc

Steps to using PenSubs.nxc:

1. Download the above file.
2. Save it into the same directory that you save your other nxc programs.
3. In your programming put these two lines at the beginning of the program:

#include "PenSubs.nxc"
#include "SensorSubs.nxc

4. You have access to the following sub commands:

Command:
Example:
Description:
ForwardDeg("speed", "degrees"); ForwardDeg(50, 720); Drive Forward at a given speed for a given rotation of wheels.
ReverseDeg("speed", "degrees");
ReverseDeg(50, 720);
Drive Reverse at a given speed for a given rotation of wheels.
ForwardTime("speed", "time");
ForwardTime(50, 2000);
Drive Forward at a given speed for a given time in milliseconds (1000 = 1 second)
ReverseTime("speed", "time");
ReverseTime(50, 2000);
Drive Reverse at a given speed for a given time in milliseconds (1000 = 1 second)
PointTurnRightDeg("speed", "degrees");
PointTurnRightDeg(50, 360);
Point Turn Right at a given speed for a given rotation.
PointTurnLeftDeg("speed", "degrees");
PointTurnLeftDeg(50, 360);
Point Turn Left at a given speed for a given rotation.
PointTurnRightTime("speed", "degrees");
PointTurnRightTime(50, 1000);
Point Turn Right at a given speed for a time in milliseconds.
PointTurnLeftTime("speed", "degrees");    
PointTurnLeftTime(50, 1000);
Point Turn Left at a given speed for a time in milliseconds.
Turn90DegRight();   
Turn90DegRight();
Point Turn 90 degrees to the right.
Turn90DegLeft();
Turn90DegLeft();
Point Turn 90 degrees to the left.
PenUp();
PenUp();
Pick up the pen.
PenDown();
PenDown();
Put the pen down.
TouchStop("speed")
TouchStop(50);
Move forward at 50 power until touch sensor activated.
LightStop("speed", "light")
LightStop(50, 40);
Move forward at 50 power until light sensor reads greater than 40.
DarkStop("speed", "light")
DarkStop(50, 50);
Move forward at 50 power until light sensor reads less than 50.
NearStop("speed", "distance")
NearStop(50, 20);
Move forward at 50 power until Ultrasonic reads less than 20 cm.
FarStop("speed", "distance")
FarStop(50, 20);
Move backwards at 50 power until Ultrasonic reads greater than 20 cm.

Email Mr.Michaud

Copyright ©2011 Mr. Michaud.