Independent motorcontrol

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
Post Reply
merijn0301
Posts: 8
Joined: 27 Jun 2012, 13:43

Independent motorcontrol

Post by merijn0301 »

Hi,
I want to control the speed of 2 motors indepedently. Therefore I let the motors run with the same power and I monitor de RotationCount. Then, in another task I check which motor had more (or less) rotations. I adjust the power until in sync.
The code I wrote for this is pasted below.
The point is that the motors start but the task "CheckSync" doesn't seem te be active although it is called via " precedes(...).

I could use "OnFwdSync". However, later I want to use the same principle to move in an (part-)circle with a specific radius. OnFwdSync is a little bit too coarse in my opinion. But more then that, I want to know what's wrong with the code.

Kind Regards, Merijn
---------------------
mutex MoveMutex;

int iRotA, iRotC;
int iRotAcs, iRotCcs;
string str1,str2;
string strRotAcs,strRotCcs;

task StartMoving()
{
while(true)
{
Acquire(MoveMutex);
OnFwd(OUT_A, 50);
OnFwd(OUT_C, 45);
iRotA = MotorRotationCount(OUT_A);
iRotC = MotorRotationCount(OUT_C);
str1 = NumToStr(iRotA);
str2 = NumToStr(iRotC);
if (iRotA > iRotC)
{
TextOut(10, LCD_LINE3, "A > C");
}
if (iRotC > iRotA)
{
TextOut(10, LCD_LINE3, "C > A");
}
TextOut(10, LCD_LINE4, str1);
TextOut(10, LCD_LINE5, str2);
Release(MoveMutex);
}
}

task CheckSync1()
{
while(true)
iRotAcs = MotorRotationCount(OUT_A);
iRotCcs = MotorRotationCount(OUT_C);
strRotAcs = NumToStr(iRotAcs);
strRotCcs = NumToStr(iRotCcs);
TextOut(10, LCD_LINE7, strRotAcs);
TextOut(10, LCD_LINE8, strRotCcs);

if(iRotAcs>iRotCcs)
{
TextOut(10, LCD_LINE6, "Checked : A > C");
Acquire(MoveMutex);
do
{
iRotAcs = MotorRotationCount(OUT_A);
iRotCcs = MotorRotationCount(OUT_C);
OnFwd(OUT_A, 45);
OnFwd(OUT_C, 55);
}
while(iRotAcs>iRotCcs);
Release(MoveMutex);
}

if(iRotCcs>iRotAcs)
{
TextOut(10, LCD_LINE6, "Checked : C > A");
Acquire(MoveMutex);
do
{
iRotAcs = MotorRotationCount(OUT_A);
iRotCcs = MotorRotationCount(OUT_C);
OnFwd(OUT_A, 55);
OnFwd(OUT_C, 45);
}
while(iRotCcs>iRotAcs);
Release(MoveMutex);
}
}

task main()
{

ResetRotationCount(OUT_A);
ResetRotationCount(OUT_C);
Precedes(StartMoving, CheckSync1);
}
merijn0301
Posts: 8
Joined: 27 Jun 2012, 13:43

Re: Independent motorcontrol

Post by merijn0301 »

Hi,

If have found the error myself.
One of the while-loops wasn't a loop due to the lack of {..}

Stupid, I know.

Merijn
hassenplug
Posts: 346
Joined: 27 Sep 2010, 03:05
Contact:

Re: Independent motorcontrol

Post by hassenplug »

This is a common type of programming error. You are not unique in having this problem.

One thing I try to always do is to use {} in an if statement, even when it's not required...

This code

Code: Select all

if (condition)
 function1();
is the same as this code

Code: Select all

if (condition) // preferred 
{
 function1();
}
but this...

Code: Select all

if (condition)
 function1();
 function2();
is not the same as

Code: Select all

if (condition) // preferred 
{
 function1();
 function2();
}
Steve
---> Link to lots of MINDSTORMS stuff under my picture --->
Post Reply

Who is online

Users browsing this forum: Semrush [Bot] and 2 guests