1061 AdvancedServo: Actuate forward, then backward

Legacy support with Phidget21
digitalmatt
Phidgetsian
Posts: 12
Joined: Wed Jul 25, 2012 1:24 pm
Contact:

1061 AdvancedServo: Actuate forward, then backward

Postby digitalmatt » Wed Sep 26, 2012 8:45 am

Did some searching around the forums, and couldn't find an answer to this.

I'm trying to figure out the programming logic (with servo event listeners) to make a linear actuator go forward and then backward in one instance. Not quite sure how to achieve this with the POSITION_CHANGE event.

It seems like POSITION_CHANGE fires whenever the actuator is moving, but there's no POSITION_CHANGE_COMPLETE event. So how can I tell my servo to actuate backward when it's completely reached its forward position? I tried comparing its positionMin and positionMax, but that doesn't seem to work. See below:

Code: Select all

private function onPositionChange( e:PhidgetDataEvent = null ):void {
         
         trace("Position changing...");
         
         //trace("Position target: " + e.target.getPosition(0));
         trace("Stopped: " + e.target.getStopped(0));
         
         if( !actuatorInitialized ) { // if the actuator hasn't been initialized, then go forward and backward
            
            if( e.target.getPosition(0) == ACTUATOR_POS_MAX ) {
               
               e.target.setPosition(0, ACTUATOR_POS_MIN);
            
            }
            
            if( e.target.getPosition(0) == ACTUATOR_POS_MIN ) {
               
               e.target.setPosition(0, ACTUATOR_POS_MAX);
            }
            
            actuatorInitialized = true;
            
         }
         


Any help would be greatly appreciated.

digitalmatt
Phidgetsian
Posts: 12
Joined: Wed Jul 25, 2012 1:24 pm
Contact:

Re: 1061 AdvancedServo: Actuate forward, then backward

Postby digitalmatt » Wed Sep 26, 2012 9:09 am

Ok I figured out the logic that makes it go back and forth:

Code: Select all

if( e.target.getPosition(0) >= ACTUATOR_POS_MAX ) {
            
            e.target.setPosition(0, ACTUATOR_POS_MIN);
         }
         
         if( e.target.getPosition(0) <= ACTUATOR_POS_MIN ) {
            
            e.target.setPosition(0, ACTUATOR_POS_MAX);
         }


My only problem now is getting it to only go forward and backward once. At a glance, you would think it's as simple as setting a variable that increments each time positionChange is fired. The problem is, positionChange is fired multiple times. when I actually setPosition...

Any ideas?

digitalmatt
Phidgetsian
Posts: 12
Joined: Wed Jul 25, 2012 1:24 pm
Contact:

Re: 1061 AdvancedServo: Actuate forward, then backward

Postby digitalmatt » Wed Sep 26, 2012 10:15 am

So here's the solution I've come up with. It feels dirty, but maybe it's not. Within the onPositionChange event listener, I'm dispatching a custom event when the actuator reaches its positionMax. When that gets fired, it calls another event listener which sends the actuator back to its positionMin.

Mieh. I dunno. Maybe it's ok.

Code: Select all

// here's the position change event listener
private function onPositionChange( e:PhidgetDataEvent = null ):void {
         
   trace("Position changing...");
         
   if( e.target.getPosition(0) >= ACTUATOR_POS_MAX ) {
   
      dispatchEvent(new Event( NexGenModel.ACTUATE_FORWARD_COMPLETE ));
   }
}

// here's the custom event listenr that's listening for an "ACTUATE_FORWARD_COMPLETE" event
public function actuateBackward( e:Event = null ):void {
         
   phidgetServoBoard.setPosition(0, ACTUATOR_POS_MIN);
}

// and of course somewhere in my code I add the event listener
addEventListener(this.ACTUATE_FORWARD_COMPLETE, actuateBackward);

User avatar
Patrick
Lead Developer
Posts: 3078
Joined: Mon Jun 20, 2005 8:46 am
Location: Canada
Contact:

Re: 1061 AdvancedServo: Actuate forward, then backward

Postby Patrick » Wed Sep 26, 2012 11:47 am

I'm not sure what the point of the extra event is. Why not just set the position to minimum directly in the position change event?

Code: Select all

private function onPositionChange( e:PhidgetDataEvent = null ):void {
   if( e.target.getPosition(0) >= ACTUATOR_POS_MAX ) {
      phidgetServoBoard.setPosition(0, ACTUATOR_POS_MIN);
   }
}

To get things started, you just set the position to MAX in your code somewhere, and then the position change event takes care of returning it to the min position once it reaches max - then next time you want a cycle just set it to MAX again.

-Patrick

digitalmatt
Phidgetsian
Posts: 12
Joined: Wed Jul 25, 2012 1:24 pm
Contact:

Re: 1061 AdvancedServo: Actuate forward, then backward

Postby digitalmatt » Wed Sep 26, 2012 1:02 pm

Thanks for your response, Patrick. That makes sense. I think I was over-complicating it.

One more question for you: the POSITION_CHANGE event doesn't seem to be tied directly to a servo itself, but rather to the controller. I need to abstract several servos into classes to give them their own position_change event. Is there some other way to target a servo on the board? Such as PhidgetAdvancedServo.servo[i] or something along those lines?

I feel like the only way I can target a servo is through one of its methods and passing in its index (like PhidgetAdvancedServo.setPosition(i, 0) ).

digitalmatt
Phidgetsian
Posts: 12
Joined: Wed Jul 25, 2012 1:24 pm
Contact:

Re: 1061 AdvancedServo: Actuate forward, then backward

Postby digitalmatt » Wed Sep 26, 2012 1:19 pm

I guess what I'm really asking is there a way to get access to a servo attached to the controller as an object, and from there access its methods/properties.


Return to “ActionScript 3.0 - Flash CS3 / Flex / AIR”

Who is online

Users browsing this forum: No registered users and 2 guests