ROBOTC.net forums
http://www.robotc.net/forums/

Delay and troubles controlling PF motors with HTIRL
http://www.robotc.net/forums/viewtopic.php?f=41&t=2471
Page 1 of 2

Author:  Jampoz [ Sat Jul 10, 2010 7:11 pm ]
Post subject:  Delay and troubles controlling PF motors with HTIRL

Here I am again

Let's go straight to some code posting and then explain the problem, I'll go thru this loop twice with two different code versions and, of course, two different problems :D

(I spared a conversion code to keep things short, the CommandL/R will only give 1-6 as useful commands)

Code:
#pragma config(Sensor, S2,     HTIRL,               sensorLowSpeed)

#include "drivers/HTIRL-driver.h"
#include "PSP-Nx-lib.c"

#define Addr 0x02
#define SensorPort S1

int CommandL = 0;
int CommandR = 0;

task main()
{
   psp currState;
   
   nI2CBytesReady[SensorPort] = 0;
   SensorType[SensorPort] = sensorI2CCustom9V;
   
   while (true)
   {
      PSP_ReadButtonState(SensorPort, Addr, currState);
      
      CommandL = currState.l_j_y * 0.07;
      CommandR = currState.r_j_y * 0.07;
      
      PFcomboPwmMode(HTIRL, 0, (eCPMMotorCommand)CommandL, (eCPMMotorCommand)CommandL);
      PFcomboPwmMode(HTIRL, 1, (eCPMMotorCommand)CommandR, (eCPMMotorCommand)CommandR);
      
      nxtDisplayTextLine(5,"CommandL: %d", (int)CommandL);
      nxtDisplayTextLine(6,"CommandR: %d", (int)CommandR);
   }
}


This way, I get the motors to move fine, but I also get about 1sec delay between interacting with the joypad and the actual motor action, it's weird it takes so much time to do the stuff so I thought about tasks and tried this other wersion...

Code:
#pragma config(Sensor, S2,     HTIRL,               sensorLowSpeed)

#include "drivers/HTIRL-driver.h"
#include "PSP-Nx-lib.c"

#define Addr 0x02
#define SensorPort S1

int CommandL = 0;
int CommandR = 0;

task Motore() {
   PFcomboPwmMode(HTIRL, 0, (eCPMMotorCommand)CommandL, (eCPMMotorCommand)CommandL);
   PFcomboPwmMode(HTIRL, 1, (eCPMMotorCommand)CommandR, (eCPMMotorCommand)CommandR);
}

task main()
{
   psp currState;
   
   nI2CBytesReady[SensorPort] = 0;
   SensorType[SensorPort] = sensorI2CCustom9V;
   
   while (true)
   {
      PSP_ReadButtonState(SensorPort, Addr, currState);
      
      CommandL = currState.l_j_y * 0.07;
      CommandR = currState.r_j_y * 0.07;

      StartTask(Motore, 10);
      
      nxtDisplayTextLine(5,"CommandL: %d", (int)CommandL);
      nxtDisplayTextLine(6,"CommandR: %d", (int)CommandR);
   }
}


Now the motor move perfectly, but I didn't use plural form as it does execute only the first command, moving the motor on port 0 and not the motor on port 1

I tried using 2 tasks for the 2 motors but it seems stupid as the number of tasks is limited, and even then, RobotC complained about using the function PFcomboPwmMode twice in parallel on two different tasks and the same time

There must be some solution and I feel like I'm close to it, yet so far away that I'm forced to ask here for help

Thank you all if you feel like trying to give me something to work on!

Author:  Gary Samad [ Sat Jul 10, 2010 8:04 pm ]
Post subject:  Re: Delay and troubles controlling PF motors with HTIRL

Looks to me like you don't really need a motors "task", but just a function. Try defining:

task Motore()

as

void Motore()

and calling it directly using

Motore();

cu,
Gary

Author:  Jampoz [ Sat Jul 10, 2010 11:23 pm ]
Post subject:  Re: Delay and troubles controlling PF motors with HTIRL

Thank you Gary but using a function makes the motors answer with almost 1sec delay just like the first example having the commands inside the main task :(

Author:  Gary Samad [ Sun Jul 11, 2010 5:41 pm ]
Post subject:  Re: Delay and troubles controlling PF motors with HTIRL

Another thing to try is to add a wait1Msec(10) or similar in your loop, so that will free up the CPU to do other work.

cu,
Gary

Author:  Jampoz [ Mon Jul 12, 2010 4:18 am ]
Post subject:  Re: Delay and troubles controlling PF motors with HTIRL

To add a Pause in order to fix a Delay, that's wicked!
Now I'm at office but I'll try this one as soon as I reach home, can't wait to fix that in order to code other features

Thank you!

Author:  Jampoz [ Wed Jul 14, 2010 7:46 am ]
Post subject:  Re: Delay and troubles controlling PF motors with HTIRL

I still have to try the ms pause hint, need some spare time, hopefully I'll do it this evening

Any word from Xander about the issue, by the way? :D

Author:  mightor [ Wed Jul 14, 2010 9:16 am ]
Post subject:  Re: Delay and troubles controlling PF motors with HTIRL

What issue? I thought the problem was resolved by changing the tasks to a function?

- Xander

Author:  Jampoz [ Wed Jul 14, 2010 10:49 am ]
Post subject:  Re: Delay and troubles controlling PF motors with HTIRL

Nope, function still gives a 1 sec delay
I'm gonna try to wait 10 ms this evening

But why does the task move only one motor? why does it ignore the second command?

Author:  mightor [ Wed Jul 14, 2010 11:28 am ]
Post subject:  Re: Delay and troubles controlling PF motors with HTIRL

Just attach your current program (don't paste it). I'll take a look. I can't test anything as I am on vacation and don't have my NXTs with me.

- Xander

Author:  Jampoz [ Wed Jul 14, 2010 11:34 am ]
Post subject:  Re: Delay and troubles controlling PF motors with HTIRL

Man I'd bring my NXT on vacation... it sucks I can't take it at the office :D
Will send you the code later when I get home, after I test the last hints I received

Thank you, hope the best for you on your vacation ;)

Author:  Jampoz [ Sat Jul 17, 2010 9:54 am ]
Post subject:  Re: Delay and troubles controlling PF motors with HTIRL

Alright, this is the version I'm trying right now, all problems remain even with the pauses

Code:
#pragma config(Sensor, S2,     HTIRL,               sensorLowSpeed)

#include "drivers/HTIRL-driver.h"
#include "PSP-Nx-lib.c"

#define Addr 0x02
#define SensorPort S1

int CommandL = 0;
int CommandR = 0;

task Motore()
{
   PFcomboPwmMode(HTIRL, 0, (eCPMMotorCommand)CommandL, (eCPMMotorCommand)CommandL);
   wait1Msec(200);
   PFcomboPwmMode(HTIRL, 1, (eCPMMotorCommand)CommandR, (eCPMMotorCommand)CommandR);
   wait1Msec(200);
}

task main()
{
   psp currState;

   nI2CBytesReady[SensorPort] = 0;
   SensorType[SensorPort] = sensorI2CCustom9V;

   while (true)
   {
      PSP_ReadButtonState(SensorPort, Addr, currState);

      CommandL = currState.l_j_y * 0.07;
      CommandR = currState.r_j_y * 0.07;

      StartTask(Motore, 10);

      nxtDisplayTextLine(5,"CommandL: %d", (int)CommandL);
      nxtDisplayTextLine(6,"CommandR: %d", (int)CommandR);
   }
}


If I place the 2 PFcomboPwmMode inside the main while loop, I get 1sec delay between the joypad interaction and the actual motor reaction

If I use the Motore task technique I get a super fast motor response but it only execute the first PFcomboPwmMode, moving one track only

Using delay doesn't help, I tried from 10 to 200ms delay

What am I doing wrong?

Author:  mightor [ Sat Jul 17, 2010 12:38 pm ]
Post subject:  Re: Delay and troubles controlling PF motors with HTIRL

You shouldn't use tasks like that. Just turn it into a normal function.

- Xander

Author:  Jampoz [ Sat Jul 17, 2010 12:51 pm ]
Post subject:  Re: Delay and troubles controlling PF motors with HTIRL

Using a function still gives me the delay problem
I interact with the joypad and I can see display update and motor motion after like 1 second, it's unusable like that

Why do I get a delay when controlling two motors and I get none using a task? And why do the task run only 1 PFcomboPwmMode instead of 2?
I can't understand

Author:  mightor [ Sat Jul 17, 2010 1:34 pm ]
Post subject:  Re: Delay and troubles controlling PF motors with HTIRL

I can't really look at this code until the end of next week as I am on vacation.

- Xander

Author:  Gary Samad [ Sat Jul 17, 2010 6:08 pm ]
Post subject:  Re: Delay and troubles controlling PF motors with HTIRL

Jampoz, it looks to me that you need to do both - not use a task (this isn't the right way to use it, just use a function or put the two lines of code back into the main loop), and you need the wait in the loop. Without both of these changes, I don't think you can make further progress. After these two changes, if there are still problems, please post your code and describe the behavior.

cu,
Gary

Page 1 of 2 All times are UTC - 5 hours [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/