Page 1 of 1

I am confusing "OnFwdReg"

Posted: 14 Jun 2012, 22:03
by walduss
Hi

I am starting with NXC. I have a code but I am confusing, so I have reduced my code to minimun.

I will show ..

Code: Select all

#define SPIN_SPEED  50


// NXT Port aliases
#define WHEELS            OUT_BC
#define LEFT_WHEEL        OUT_B
#define RIGHT_WHEEL       OUT_C

#define CW     1        // clockwise
#define ACW   -1        // anticlockwise

void Spin(short dir)
{
	// Gira hacia el lado de "dir"
	// Usar CW o ACW
	NumOut(0, LCD_LINE4, dir);
	NumOut(0, LCD_LINE5, sign(dir));
/*
	OnFwdReg(LEFT_WHEEL,  -sign(dir)*SPIN_SPEED, OUT_REGMODE_IDLE );
	OnFwdReg(RIGHT_WHEEL, sign(dir)*SPIN_SPEED, OUT_REGMODE_IDLE );
*/

	OnFwdReg(LEFT_WHEEL,  dir*10, OUT_REGMODE_IDLE );
	OnFwdReg(RIGHT_WHEEL, -dir*10, OUT_REGMODE_IDLE );

}


task main()
{
  Spin(CW);
  Wait(3000);
}
this code work fine, but If I change the main task to

Code: Select all

task main()
{
  Spin(ACW);
  Wait(3000);
}
program do nothing !!! For me it is suppose that Wheels run in oposite direction!!!! (Robot should spin oposite than before)

another test ... If I change main task to ...

Code: Select all

task main()
{
  Spin(CW);
  Wait(3000);
  
  Spin(ACW);
  Wait(3000);
}
or ....

Code: Select all

  Spin(ACW);
  Wait(3000);
  
  Spin(CW);
  Wait(3000);
It runs fine too !!!

So why programn dont work when I try to Spin ACW (AnticlockWise) only ????

thanks in advance and sorry for my english!

Re: I am confusing "OnFwdReg"

Posted: 15 Jun 2012, 09:38
by walduss
Hi again,

I have done another test after restart the brick and the computer.

Code: Select all

#define SPIN_SPEED  50


// NXT Port aliases
#define WHEELS            OUT_BC
#define LEFT_WHEEL        OUT_B
#define RIGHT_WHEEL       OUT_C

#define CW     1        // clockwise
#define ACW   -1        // anticlockwise

void Spin(short dir)
{
	// Rotot turn to "dir" direction
	// Use CW o ACW
	
	NumOut(0, LCD_LINE4, dir);
	OnFwdReg(LEFT_WHEEL,  -sign(dir)*SPIN_SPEED, OUT_REGMODE_IDLE );
  TextOut(0, LCD_LINE5, "Left done");
	OnFwdReg(RIGHT_WHEEL, sign(dir)*SPIN_SPEED, OUT_REGMODE_IDLE );
  TextOut(0, LCD_LINE6, "Right done");
}


task main()
{
  Spin(ACW);
  Wait(3000);
}
in this case Numer "-1" is display in line4 and but never it is shown the text "Left done". also the program never end.

But if I call Spin(CW) instead Spin(ACW) in main task, Program run sucessfully.

Anybody have any clue?

Re: I am confusing "OnFwdReg"

Posted: 15 Jun 2012, 10:25
by walduss
After some test I have discover hat the issue is due to "sign" function.


When NXT run "-sign(dir)" where "dir" is ACW the program get an unexpected behaviour (at least for me)

So I have remove sign function (really, it was not necessary)
#define SPIN_SPEED 50


// NXT Port aliases
#define WHEELS OUT_BC
#define LEFT_WHEEL OUT_B
#define RIGHT_WHEEL OUT_C

#define CW 1 // clockwise
#define ACW -1 // anticlockwise

void Spin(short dir)
{
// Gira hacia el lado de "dir"
// Usar CW o ACW

OnFwdReg(LEFT_WHEEL, (dir)*SPIN_SPEED, OUT_REGMODE_SPEED );
OnFwdReg(RIGHT_WHEEL, -(dir)*SPIN_SPEED, OUT_REGMODE_SPEED );
}

task main()
{
Spin(ACW);
Wait(3000);
}
Now, program seem to be fine.

But really I dont know if the issue with -sign(dir) is a bug or not. If anyone could confirm it will be apreciated.

Re: I am confusing "OnFwdReg"

Posted: 15 Jun 2012, 21:32
by HaWe
I agree, OnFwdReg is weird and often produces unexpected behaviours -
(to be honest: just like all of the advanced motor functions ...)
I personally sacrificed all of them.

Better you write your own functions!

Re: I am confusing "OnFwdReg"

Posted: 15 Jun 2012, 22:52
by walduss
doc-helmut wrote:I agree, OnFwdReg is weird and often produces unexpected behaviours -
(to be honest: just like all of the advanced motor functions ...)
I personally sacrificed all of them.

Better you write your own functions!
Sorry, I dont understand what you mean with create your own functions?

You mean create in a low level lenguage like NCB?


Now I am having more issues with other secuences of program, so any help will be usefull!

Re: I am confusing "OnFwdReg"

Posted: 16 Jun 2012, 00:46
by mattallen37
If I were you, I would use OnFwd and/or OnRev.

Re: I am confusing "OnFwdReg"

Posted: 16 Jun 2012, 08:21
by HaWe
yes, matt wrote what I actually intended to say.

Re: I am confusing "OnFwdReg"

Posted: 16 Jun 2012, 10:25
by walduss
mattallen37 wrote:If I were you, I would use OnFwd and/or OnRev.
doc-helmut wrote:yes, matt wrote what I actually intended to say.
Ok, I understand. In fact actually I am using OnFwd and OnRev instead.

Anyway, as I have comment before, after some test I have detected that issue was related to Sign fuction.

Thanks.

Re: I am confusing "OnFwdReg"

Posted: 16 Jun 2012, 10:45
by HaWe
to my observations, the sign() function always works correctly.
But not everything which normally works correctly does work also properly and predictalbly when used by the advanced motor control functions...