LEGO mindstorms NXT power programming: robotics in C

News, rumors, and other broad discussion topics.
borntoown
Posts: 58
Joined: 14 Nov 2010, 16:58

LEGO mindstorms NXT power programming: robotics in C

Post by borntoown »

The title is for a book that I purchased and I did not receive it yet.

1) Will I regret purchasing this book?
2) Will it teach me NXC? How good (Like only giving commands or explaining them)?

Thanks.
B2O productions are the best for the best.
mightor
Site Admin
Posts: 1079
Joined: 25 Sep 2010, 15:02
Location: Rotterdam, Netherlands
Contact:

Re: LEGO mindstorms NXT power programming: robotics in C

Post by mightor »

borntoown wrote:1) Will I regret purchasing this book?
Only if you don't read it and use it as a door stop. A rock or box would be a cheaper alternative.
2) Will it teach me NXC? How good (Like only giving commands or explaining them)?
Pretty much everything you need to know about programming NXC is covered by this book.

- Xander
| My Blog: I'd Rather Be Building Robots (http://botbench.com)
| RobotC 3rd Party Driver Suite: (http://rdpartyrobotcdr.sourceforge.net)
| Some people, when confronted with a problem, think, "I know, I'll use threads,"
| and then two they hav erpoblesms. (@nedbat)
muntoo
Posts: 834
Joined: 01 Oct 2010, 02:54
Location: Your Worst Nightmare
Contact:

Re: LEGO mindstorms NXT power programming: robotics in C

Post by muntoo »

mightor wrote: Pretty much everything you need to know about programming NXC is covered by this book.
The part about hacking into the US Military was removed before the book was released, but otherwise...
Image

Commit to LEGO Mindstorms Robotics Stack Exchange:
bit.ly/MindstormsSE


Commit to LEGO Stack Exchange: bit.ly/Area51LEGOcommit
tabbycatrobots
Posts: 100
Joined: 27 Dec 2010, 19:10

Re: LEGO mindstorms NXT power programming: robotics in C

Post by tabbycatrobots »

I agree with the previous comments that this is a good, very useful book, and you won't regret the
purchase. That said, I have found that there are 2 methods (probably more) that people learn a
language. The first is by reading the spec, and the second is by starting with someone else's
working code and modifying it. I fall into the latter camp, and found the book Creating Cool
Mindstorms NXT Robots by Daniele Benedettelli has good code examples, that give me a starting
point. As I write NXT programs, I find myself bouncing back and forth between these 2 books.
Other resources that help me are the 2 on-line guides, which you may have already found.
<bricxcc.sourceforge.net/nbc/nxcdoc/NXC_tutorial.pdf> and
<bricxcc.sourceforge.net/nbc/nxcdoc/NXC_Guide.pdf>
borntoown
Posts: 58
Joined: 14 Nov 2010, 16:58

Re: LEGO mindstorms NXT power programming: robotics in C

Post by borntoown »

I didn't understand a lot from the book, only some of the basic commands that i already know. Any help, advice, suggestion, etc...?
Thanks.
B2O.
B2O productions are the best for the best.
mightor
Site Admin
Posts: 1079
Joined: 25 Sep 2010, 15:02
Location: Rotterdam, Netherlands
Contact:

Re: LEGO mindstorms NXT power programming: robotics in C

Post by mightor »

First bit of advice I can give you is to stop assuming we can read your mind and know which things you don't understand.

Be specific.

- Xander
| My Blog: I'd Rather Be Building Robots (http://botbench.com)
| RobotC 3rd Party Driver Suite: (http://rdpartyrobotcdr.sourceforge.net)
| Some people, when confronted with a problem, think, "I know, I'll use threads,"
| and then two they hav erpoblesms. (@nedbat)
borntoown
Posts: 58
Joined: 14 Nov 2010, 16:58

Re: LEGO mindstorms NXT power programming: robotics in C

Post by borntoown »

You mean what commands I understood? The following:

1)Touch Sensor.
2)Wait.
3)Motor (OnFwd, OnRev, off).
4)Displaying text and number.
5)If, do, do-while statements.
6)Repeat, until, while.
7)Stop all tasks.
8)Clear screen.

If you want to help, please mention the following points (In addition to other stuff):

1)Variable.
2)Files.
3)Sensors that come with the NXT and NXT 2.0.
4)Define, include, sub, void, switch and int commands.

Please explain to me the other basic commands (like sounds, graphics, etc...). Do not take me to the advanced, I'm not ready for that.

Hope this is what you meant by "Be specific".
Do not explain stuff to me as in the guide or the book (Do it in a better way, please).

Please note that this post's aim is not to let the book down.

Thanks,
B2O.
B2O productions are the best for the best.
mattallen37
Posts: 1818
Joined: 02 Oct 2010, 02:19
Location: Michigan USA
Contact:

Re: LEGO mindstorms NXT power programming: robotics in C

Post by mattallen37 »

Variables are numbers with no single, specific, predefined value. They can be mathematically performed on to have the value change. Here are a couple examples.

Code: Select all

int x,y,z; //this is where you make variables to work with. The int means that they are integers, and by the NXC/NBC (possibly all C) definition, they are 16 bit signed (positive or negative) values in the range of -32768 to 32767.
x=10;      //x now has the value of 10
y=5;      //y now has the value of 5
z=x+y      //z equals x (10) plus y (5), so z now equals 15
Do you want to know how to use files with an NXC program? Based on your current understanding level, I think it is a little early to discuss specifics.

What is it that you want to know about the NXT sensors? Before using them in a program, you must initialize them, so the NXT knows how to read them. I use the SetSensor commands as follows.

Code: Select all

SetSensorTouch(S1); //Set the port up for a touch sensor. It starts it reading the analog pin, in inactive mode.
SetSensorLight(S1); //Set the port up for a light sensor. It starts it reading the analog pin, in inactive mode. Also uses pin 5 to turn on and off the light.
SetSensorSound(S1); //Set the port up for a sound sensor. It starts it reading the analog pin, in inactive mode. Also uses pins 5 and 6.
SetSensorLowspeed(S1);   //Set the port up for a digital (I2C) sensor.
SetSensorUltrasonic(S1); //I can only assume it is similar to the SetSensorLowspeed command. I have never tested this function.
SetSensorColorFull(S1);  //For the 2.0 color sensor. It turns it on in multicolor mode, so it reads all three colors.
SetSensorColorRed(S1);   //For the 2.0 color sensor. It turns on the red light.
SetSensorColorGreen(S1); //For the 2.0 color sensor. It turns on the green light.
SetSensorColorBlue(S1);  //For the 2.0 color sensor. It turns on the blue light.
SetSensorColorNone(S1);  //For the 2.0 color sensor. It turns off all the lights.
The values returned by the analog sensors are scaled to whatever they are set up for. For example, if it is set up as a touch sensor, only the value of 0 or 1 will be returned. If you want to read the RAW value (0-1023), use the following.

Code: Select all

SetSensorMode(S1, IN_MODE_RAW);
To access the values from the sensors, you can usually use SENSOR_1 (or 2 or 3 or 4). Here is an example of a few of the last concepts together.

Code: Select all

task main()
{
  SetSensorLight(S1);                          //Set it up as a light sensor
  SetSensorMode(S1, IN_MODE_RAW);              //Set it up to return the RAW values, 0-1023
  //SetSensorType(S1, IN_TYPE_LIGHT_INACTIVE); //use this line to turn the light off
  //SetSensorType(S1, IN_TYPE_LIGHT_ACTIVE);   //use this line to turn the light on
  while (true)                                 //repeat forever
  {
    Wait (50);                                 //slow down a little
    ClearScreen();                             //clear the screen
    NumOut(0,LCD_LINE1,SENSOR_1);              //display the value being returned by the light sensor (it will be the RAW value).
  }
}

Define, is used like this.

Code: Select all

#define LeftMotor OUT_B
It sets the value of "LeftMotor" to that of "OUT_B". Now, instead of coding OnFwd(OUT_B, 100);, you would use OnFwd(LeftMotor, 100);. This way, you can have a huge number of commands, that when coded, used "LeftMotor" to refer to the left motor. When you are redesigning mechanically, and you are required to move the wire for the left motor to port OUT_C, all you have to do is change #define LeftMotor OUT_B to #define LeftMotor OUT_C, and that is ALL you have to change in the code. You won't have to scroll down, looking for all the OUT_B constants, and changing them to OUT_C.

Include is used like this.

Code: Select all

#include "file.nxc"
It includes another part of a program, or library, and compiles both together at download. It can also be used for other types of files. Note, the file MUST be saved in the same directory on your computer as the program that "#include"s it.

IIRC, sub is a part of a program that is in a different place on the screen when programming, but can be called upon with a single line of code. This way, you can have a lot of "junk" on it, and not have to look at it in the task you want it in.

void seems to be similar to sub, but IIRC, it is a better method to use. Again, you can run the function with a single line of code. like the "sub", it contains the "junk" code that doesn't need to be an eyesore in the task, and it can also be used to shorten the code. For example, you can have 10 lines of code, but if you need access to the exact code in 5 different places, you can just call it from a single function (using a single line of code).

A switch is similar to an array of if-else statements. Instead if using the following:

Code: Select all

if (1==x){}
else{if(2==x){}
 else{if(3==x){}
  else{if(4==x){}
   else{if(5==x){}
   }
  }
 }
}
you can use

Code: Select all

switch (x){
 case 1:{}break;
 case 2:{}break;
 case 3:{}break;
 case 4:{}break;
 case 5:{}break;
}
Hopefully that answers some of your questions. Also note, that none of my code has been tested, so it likely has bugs.
Matt
http://mattallen37.wordpress.com/

I'm all for gun control... that's why I use both hands when shooting ;)
borntoown
Posts: 58
Joined: 14 Nov 2010, 16:58

Re: LEGO mindstorms NXT power programming: robotics in C

Post by borntoown »

Yes, you answered some of the questions i asked, but not all.
First of all, thanks.

The commands I still need to understand it are:

1) Files (I never make a program without making a file or two, so this is important for me)
2) Sub and void
3) Array

At first I thought myself like not wanted in these forums but I see people here are good =)
Thanks again.
B2O.
B2O productions are the best for the best.
mattallen37
Posts: 1818
Joined: 02 Oct 2010, 02:19
Location: Michigan USA
Contact:

Re: LEGO mindstorms NXT power programming: robotics in C

Post by mattallen37 »

I am glad you feel welcome here.

Well, then please explain what you mean by "files". do you mean the general concept of files on a computer? If you are referring to the file system on the NXT (reading and writing files with an NXC program), then I don't think you are ready to try to understand how to do it.

An array is a set of variables indexed by number."int x[10];" would declare an array of integers containing 10 elements (numbered 0-9) with the name "x". To access the elements, look at this example.

Code: Select all

int x[10];//declare the array
x[0]=10;//element 0 equals 10
x[1]=5; //element 1 equals 5
x[2]=x[0]+x[1]; //element 2 equals elements 0+1, therefore the value of 15
Note, that in the above code, I only initialized 10 elements (0-9), so trying to do anything with x[10] would cause a runtime error on the NXT, causing the program to crash.

Examine this example to learn of one use of the void function.

Code: Select all

void TurnLeft(){
  OnFwd(OUT_C,100);
  OnRev(OUT_A,100);
}

void TurnRight(){
  OnFwd(OUT_A,100);
  OnRev(OUT_C,100);
}

void TurnOff(){
  Off(OUT_AC);
}

task main(){
  TurnLeft();
  Wait(1500);
  TurnRight();
  Wait(1500);
  TurnOff();
}
See how in task main, you only use one line of code to turn left, right, or off? This is almost pointless in this case, because I only have one or two lines of code in each of the functions, but if I were to have 10 or so, it could really help clean up the appearances of the main task. Also, if I were to have other tasks running, or I wanted to do the same thing multiple times, i could just add a single line each place I wanted it to execute, and not have to have a ton of duplicate code.

I think sub is basically identical in function to void, but it is not as efficient, IIRC.
Matt
http://mattallen37.wordpress.com/

I'm all for gun control... that's why I use both hands when shooting ;)
Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests