Page 1 of 2

motor problems

Posted: 07 Mar 2014, 23:51
by ninjaman1138
hello
im new here
I have tried to program a motor to rotate left to right and repeat. initially I couldn't get it to go both ways. then I could get it to go both ways. im not sure what I did. I have learnt that you have to save the file before it will compile or download. I want to connect the ultrasonic device and move it left and right so it can detect what is in front of it. I programmed it like this

task main()
{
OnFwd(OUT_B, 70);
Wait(1000);
OnRev(OUT_B, 70);
Wait(1000);
}

the problem is that in one direction it moves more then In the next. does anyone know why it does this?
any help would really be appreciated!
thanks
simon

Re: motor problems

Posted: 08 Mar 2014, 01:37
by h-g-t
Try putting this before the motor commands -

OffEx (OUT_B, RESET_ALL); // Reset the motor counters
Wait (100); // Give sensor time to update

Re: motor problems

Posted: 08 Mar 2014, 02:48
by h-g-t
The code you have given will only work for one turn each way before stopping so you need to put a loop round it if you want it to repeat.

Perhaps you should look at the 'line follower' programs which abound on the web to see how others have coded similar actions.

Re: motor problems

Posted: 08 Mar 2014, 12:42
by ninjaman1138
that's great, thanks for your help.

I will try that reset thing. I haven't looked at much else really. I got the set secondhand recently and read a copy of NXC that uses a tribot which you modify with the sensors. it didn't mention anything like resetting or motor counters which has my mind going at the moment. I started learning about arduino C, then got a book on ANSI C and only got a few pages into that when I was given an assignment from college to make an nxt robot and program it to do certain things. I have limited time but I don't think the project will be too difficult. its little things like this that confuse the hell out of me.
thanks for the help
all the best

simon

Re: motor problems

Posted: 08 Mar 2014, 14:34
by h-g-t
The brick seems to keep track of the motors so, if you move a motor manually then run a program to move that motor, it resets the motor to the original position before following the move command.

Not saying it is the cause of your problem, but I got some weird behaviour out of my rig until I found advice to reset the motors first.

NXC is a good choice as it is easier to learn and to program than the LEGO software.

I thought it would be too difficult so avoided it until frustration with the speed of the LEGO graphical programming interface drove me to seek an alternative.

Because it is such a great program, there are lots of information about it on the net.

Just google nxc then guide or tutorial and you'll get plenty of hits.

The BRIXCC also has inbuilt help, as well as a list of commands down the side.

You can also search for NXC programs; I learned a lot but seeing how others wrote their code.

There are also loads of other programming environments http://www.teamhassenplug.org/NXT/NXTSoftware.html or you can control the brick directly from a computer using Bluetooth and the gadgets in BRIXCC.

Never tried it myself, but I have seen examples of programs written on a PC, tablet PDA or phone issuing commands to control the NXT.

Re: motor problems

Posted: 08 Mar 2014, 22:37
by HaWe
back to TOP, you commanded the motors to do the following

OnFwd(OUT_B, 70);
Wait(1000);
OnRev(OUT_B, 70);
Wait(1000);

that means:

first mot B forward by 70% for 1 sec,
then mot B reverse by 70% for 1 sec
then end of program.

As there is no loop to make all this repeatedly (i.e., more often than just once) the program finally exits after these operations.
But after it finally ends, the run state of mot B is still "running".
In order to terminate the program correctly, an Off() command should be set at the end:
Off(OUT_B);

if you'd need a quick tutorial try this following - maybe Google translate can help you to translate it into your mother's tongue:

http://www.mindstormsforum.de/viewtopic.php?f=25&t=4937

John Hansen also has written a complete book about his NXC PL.

HTH!

Re: motor problems

Posted: 12 Mar 2014, 14:35
by ninjaman1138
hello

I am still having the same problem. it will move slightly in the fwd direction then full movement in the other direction.
this is what im using, I have tried the offex bit in different positions and no change. the wait(100) does nothing either as I have changed this, removed it and still nothing.


task main()
{
OnFwd(OUT_A, 30);
Wait(100);
OffEx (OUT_A, RESET_ALL);
Wait(100);
OnRev(OUT_A, 30);
Wait(100);
}

Re: motor problems

Posted: 12 Mar 2014, 16:12
by HaWe
well, this wait thing actually makes no sense, this doesn't cause your issues.
The same it's about the reset thing - this is only meaningful for encoder counter issues IMHO.
BTW, there is no need to save the source, in case NXC saves temporarily by its own, Program name will be "Untitled1".

Anyway, I tried your first program (modified) and everything's running perfectly with my NXT, both by 70 and 30 pwm - absolutely no issues!

Code: Select all

task main()
{
   OnFwd(OUT_B, 70); //  also fine with OnFwd(OUT_B, 30);
   Wait(1000);
   OnRev(OUT_B, 70); //  also fine with OnRev(OUT_B, 30);
   Wait(1000);
   Off(OUT_B);
}

Re: motor problems

Posted: 12 Mar 2014, 17:35
by h-g-t
The 'OFFEX' command would have to be given before any other commands for that motor.

Try making it the first line of code.

Re: motor problems

Posted: 12 Mar 2014, 17:39
by HaWe
why use OFFEX at all as the program is fine anyway ?
h-g-t, did you try and test the program personally already by your own?