0

I have some code in which I am waiting on the current x,y,z position of a motor to be equal to the position I sent to the motors. There is no command to ask the motors directly so I set up a boolean to be set to true when the current.x , current.y, and current.z are equal to my move(x,y,z) numbers.

I have tried several different ideas, first, setting up a timer and on each tick of the timer, check if the values are equal. But with this attempt I can't get the code to wait if they are not equal.

I also tried a recursion method where if the boolean was false, wait some milliseconds and check again. The code said I had infinite recursion and returned a stackoverflow.

How can I wait for the motors to stop moving before sending my next command?

(The only data I can access from the motor is the current position of it)

kenetik
  • 250
  • 3
  • 13

4 Answers4

2

You can create an event and register the waiting code with the event. Back in the motors code, as soon as the values matches, you can fire the event.

Pawan Mishra
  • 7,212
  • 5
  • 29
  • 39
  • How do I create an event for this? The commands for getting the position of the motor are motor.currentx, motor.currenty, motor.currentz – kenetik Nov 04 '11 at 15:45
  • Lets say you have two classes e.g. Motors and Command. You define the event in Motors class, the event which "Motors" class will fire, once the co-ordinates matches. To this event, you can register a method of "Command" class, which will be invoked once the event is fired. If you have never done custom event based programming, then I will suggest you to read some articles/blogs. Following could be a good place to start : http://www.akadia.com/services/dotnet_delegates_and_events.html – Pawan Mishra Nov 04 '11 at 15:58
1

You could possibly try to have some sort of event triggered when all the values are equal to the values you want and have your actor class subscribe to the event.

AD.Net
  • 13,352
  • 2
  • 28
  • 47
  • I would like to try this but I dont know how to write an event in C#, with visual studios being so easy to just double click a button and create an event, I'm afraid I dont know how to create my own custom event. – kenetik Nov 04 '11 at 15:47
  • This link is quite well put together - http://www.akadia.com/services/dotnet_delegates_and_events.html – Hugh Jones Nov 04 '11 at 15:59
1

If you convert recursion into iteration (a while loop), you won't get a stack overflow.

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
  • I've tried this before but the code skipped over it and I almost crashed the stage, I like the idea of creating an event however, but thankyou for your suggestion. – kenetik Nov 04 '11 at 15:46
1

If you have communication between 2 threads where one is waiting for the other to reach a certain condition then the easiest mechanism is to use a WaitHandle of sort. In this case I would recommend an AutoResetEvent.

// Shared
AutoResetEvent m_moveHit = new AutoResetEvent(false);

// Thread 1 
void MoveHit(Position position) {
  if (position == thePositionDesired) {
    m_moveHit.Set();
  }
}

// Thread 2
void Go() {
  // Wait until the move happens 
  m_moveHit.WaitOne();

  // Won't get here until it happens
}
JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454