I am confusing "OnFwdReg"

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
Post Reply
walduss
Posts: 16
Joined: 24 Apr 2012, 23:18
Location: Canary Islands, Spain
Contact:

I am confusing "OnFwdReg"

Post 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!
walduss
Posts: 16
Joined: 24 Apr 2012, 23:18
Location: Canary Islands, Spain
Contact:

Re: I am confusing "OnFwdReg"

Post 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?
walduss
Posts: 16
Joined: 24 Apr 2012, 23:18
Location: Canary Islands, Spain
Contact:

Re: I am confusing "OnFwdReg"

Post 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.
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: I am confusing "OnFwdReg"

Post 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!
walduss
Posts: 16
Joined: 24 Apr 2012, 23:18
Location: Canary Islands, Spain
Contact:

Re: I am confusing "OnFwdReg"

Post 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!
mattallen37
Posts: 1818
Joined: 02 Oct 2010, 02:19
Location: Michigan USA
Contact:

Re: I am confusing "OnFwdReg"

Post by mattallen37 »

If I were you, I would use OnFwd and/or OnRev.
Matt
http://mattallen37.wordpress.com/

I'm all for gun control... that's why I use both hands when shooting ;)
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: I am confusing "OnFwdReg"

Post by HaWe »

yes, matt wrote what I actually intended to say.
walduss
Posts: 16
Joined: 24 Apr 2012, 23:18
Location: Canary Islands, Spain
Contact:

Re: I am confusing "OnFwdReg"

Post 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.
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: I am confusing "OnFwdReg"

Post 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...
Post Reply

Who is online

Users browsing this forum: No registered users and 13 guests