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
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:
More Online Resources:
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:
OnFwdSync(“ports”, “pwr”, “turnpct”);
OnRevSync("ports", "power", "turnpct");
RotateMotor(“ports”, “pwr”, “angle”);
Task and Sub Commands:
Structure of NXC Commands:
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.
1. Getting Started with programming:
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!
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.
Point Turns: The Robot rotates both wheels in opposite directions to pivot on a center point between the wheels.
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.
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()":
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:
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:
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:
4. You have access to the following sub commands: