Page 1 of 2

### Controlling motor with nxt

Posted: Thu Nov 05, 2009 9:41 am
I have just started with Aforge and a simple project. I just want to be able to control the lego motor to move to 8 fixed positions around a circle ( 45, 90, 135, 180, 225, 270, 315 and 360 degrees ). I have studied your project
"Driving Lego Mindstorms Bot" and docs for "NXTBrick MotorState Members" but I still have problems to move the motor to these exact positions. I miss or dont get it how to set the exact angle positions. If anyone have some ideas or more sample- code that could be usefule then I would be greatful.

Onother way to solve the problem is to use the visual programming that comes with mindstorms. In that case there is a variable where I can set the angle of the motor movement and there it works much better. The only problem is that I want to control the nxt and the motor from my computer. So another question that comes up is how to set so my computer keyboard to send impulses to the input connections in the nxt. Are there any sample code?

Thankful for any sugestions

### Re: Controlling motor with nxt

Posted: Thu Nov 05, 2009 9:52 am
Hello,

You may try using TachoLimit to set limit for motors' rotation. However, from my experiance with several NXT projets, I found that this only field will not give you the best precision. It is a known fact - if you want precision, then you need to use gears. By combining several gears you may get quite good setup, with very good precision.

### Re: Controlling motor with nxt

Posted: Fri Nov 06, 2009 2:09 pm

Now I have put one gear to my system, but I think the motor still behaves strange. One full rotaation corresponds to approximately 1200 in the tacholimit variable with my gear.

I have 8 buttons wich will set tacholomit to 150,300,450,600,750,900,1050 and 1200 wich corresponds to 45° , 90° ,135° , 180° , 225° , 270° , 315° and 360 degrees.

When tacholimit is set to 750, 900,1050 and 1200 it works perfect but when I set tachoLimit to 150, 300, 450, 600 it does not work as expected.
F ex when tacholimit is set to 300 the motor moves 22° , 22° , 225° , 22° , 22 degrees in sequence if you press the button several time. Do you have any explanation for this behavior?

I also can't get the break to function when I try this code.

motorState.Mode = NXTBrick.MotorMode.Brake;
mState = nxt.SetMotorState(NXTBrick.Motor.A, motorState);

Best regards

### Re: Controlling motor with nxt

Posted: Fri Nov 06, 2009 6:29 pm
Try to increase motor's power level. I've notices with my NXT, that sometimes motors don't start moving when low motor power is set and tacho limit. I noticed the same effect like you've described - no rotation (or little), no rotation , then more rotation than I expected, etc.

### Re: Controlling motor with nxt

Posted: Mon Nov 09, 2009 8:46 am
I have had the power variable set to 100 so I can't increase it anymore ( and I have new batteries ).

If I can't solve it to get the motor to work on smaller tacholimit than 750 I can try two other things. If the tacholimit is set to less then 750 I can set it to f ex 150 + 1200 (1200 for a full turn with this gear ) but then the motor will move unnecessarily all the time.

The other option that I have is to create a program with drag and drop with the software that comes with nxt. I have tried this and with this program the motor moves correct even with smaller angles. The problem here is how I should control the motor to move to 8 different positions. There are only 4 inputs for sensors. So what I want to in this case is to use Aforge to reset the inputs so they will take the impulses from the keyboard instead of the Lego sensors. Can I accomplish this with Aforge? Are there any example code on this? ( I have seen a program in C++ where you can set the inputs to different keys from keyboard so I assume it would be possible with library as well )

### Re: Controlling motor with nxt

Posted: Mon Nov 09, 2009 9:26 am
Hello

The AForge.NET framework has nothing to do with keyboard. So you will need to do something on your own. For example, you may try key press events in your Win Forms application.

### Re: Controlling motor with nxt

Posted: Mon Nov 09, 2009 9:40 am
Hi

But can you set the inputs in nxt so it responds on Aforge/C# instead of the sensors?

### Re: Controlling motor with nxt

Posted: Tue Jan 05, 2010 1:55 am
Hi all,
While working on a rubik´s cube solver which employs the aforge libraries to control one webcam and NXT motors, I also found out that the NXT motor is very imprecise at short turns. With the gear ratio in my project (4:1) I need to set the Tacho Limit value to 1440 for a full revolution of the cube. When Tacho Limit is set to 500 or higher, the NXT motor performs better, but in the range of 100 or less I also see very erratic behavior. In my case Tacho Limit 100 corresponds to 25 degrees. By rotating the motor several times with tacho limit 100, the cube platform turns 25, 25, 0, 50, or sometimes 25, 25, 0, 0, 25, 50, etc - which makes the system totally unusable. I also tried different power settings, and regulated modes, just like Henate did.
Now here is the thing - I suspect braking mode may not be working correctly with Aforge. Simply by looking at the motor when the rotation ends, it does not seem to have been braked at all.
So I created a simple program using the graphical interface that comes with Lego NXT. The brick awaits continuously for an integer value (positive or negative) received at mailbox 1, coming from the BlueTooth port. The NXT motor is set to operate in the brake mode always. If the integer value received is positive, the motor turns in one direction. If it is negative, it turns in the other. The integer value is the number of degrees to turn.
I compiled the program and got it running at the NXT brick. Then I wrote a C# program (using Visual Studio 2008) to open a BlueTooth connection to the brick and sent integer values to mailbox 1 at the NXT brick. With that, I can send whatever values I wish - even 1 - and the NXT motor operates correctly. Interestingly, with small values around 50 I can clearly see the motor oscilating a little when it reaches the defined tacho limit value, probably because the NXT firmware in the brick struggles to brake the motor at the expected value. But it always works!
By checking the NXT SDK documentation, I read that it is mandatory to set the UPDATE_MODE bit in the FLAGS bitfield to commit changes to the MODE property. I wonder if the Aforge library is indeed changing the FLAGS bitfield accordingly, when MODE is changed to BRAKE. I will take a look at the source code for the NXT aforge library, when I gather some courage, more skills and time....

Best Regards

### Re: Controlling motor with nxt

Posted: Tue Jan 05, 2010 9:24 am
Hello,

Thank you very much for the given info. Since motors' movement precision worried me for a long time, the information seems to be really interesting.

ahelios wrote:By checking the NXT SDK documentation, I read that it is mandatory to set the UPDATE_MODE bit in the FLAGS bitfield to commit changes to the MODE property. I wonder if the Aforge library is indeed changing the FLAGS bitfield accordingly, when MODE is changed to BRAKE.

Hmmm ... Not really sure if the flag is set properly. Need to check it also.

If you find anything else in this direction, I will appreciate if you share it.

### Re: Controlling motor with nxt

Posted: Mon Dec 13, 2010 10:37 am
I Also am looking at trying to get specific rotations with my motors not having much luck. I might try gears