Servo attach

I read a discussion about Servo attach and the isAttached attribute in the shoutbox latley, but it is gone now. I remember that there was a discussion about what the different attach / detach methods are supposed to do and the meaning of the isAttached attribute vs controller != null.

The reason that I ask is that the Servo GUI's seems to use the isAttached attribute to know if the controller has been set, but that doesn't work anymore. So both the Swing and WebGui are broken.

So should the GUI's be changed or should the logic behind the isAttached attribute be changed back to how it was ? 

I think calamitys opinion was that the isAttached attribute should represent if pulses were sent to the servos and that controller != null should be used to know if the controller has been set.

That would be a change that isn't backward compatible, but may still be OK. But I don't know if any scripts exists that use the isAttached attribute, or if it's only the GUI's.

I can change the Servo gui's, but I don't want to do that until we have a common view of how it should work.


GroG's picture

Hi Mats,isAttached is to

Hi Mats,

isAttached is to represent whether a pwm stream is currently being used to control the servo.

There has been a considerable amount of confusion regarding attach/detach - but now I believe its relatively clear.  As I mentioned attach/detach was initially named out of the Arduino library .. I think pi4j has better names.

there are 4 states:
1. controller is set & pwm is enabled -> isAttached == true
2. controller is set & pwm is not enabled -> isAttached == false
3. controller is not set & pwm is enabled (invalid) -> isAttached == false
4. controller is not set & pwm is not enabled -> isAttached = false

More Stuff:

Testing if the controller is null turns out not to be so trivial in the web gui.  The json serialization can get tricky. I know its preferrable to have as minimal amount of variables possible, with as few copies, but in some places I need to use a "controllerName" variable to determine if the controller is set, when the it is marked as transient. I also just tested the webgui servo attach/detach and it appears to be working.. so perhaps the Swing gui only needs fixing...

calamity's picture

I remember that discussion,

I remember that discussion, but look I did not go back after the discussion to complete the change

at first, isAttached() was returning true if the controller have been set (controller != null)

I made a change to set isAttached() value to when the the servo have been energize (servoAttach)

The discussion about the state of what isAttached() is returning. We discuss that isAttach() should return true if both controller have been set and the servo been attach (controller!=null && isAttached == true)

But I think that change was never implemented. But I had the impression that the GUI's were working better

But now, thinking about it, isAttached()  should mean isUsable(). so the controller need to be set, but also connected. something like (controller != null && controller.isConnected() && isAttached == true)


GroG's picture

The crux of the problem I

The crux of the problem I believe is  :

  • We have a very good model of attaching and detaching devices - this concept  goes beyond just servos - but works as a model for all Sensors, Devices, Controllers, etc... regrettably, Arduino uses the same name - in fact at the DeviceControl level, the interface has isAttached() which is ALL ABOUT THE DEVICE (not about Servo)
  • I think most/all of this confusion would be cleared up quickly if we had energize / deenergize - or something appropriate - but that would break a great amount of scripts...


Mats's picture

GUI Buttons


So we have four states and one button in  the GUI's. So my conclusion is that one more button is needed.

May I suggest:

1. An Attach/Detach button that sets the controller and assigns the pin. It would create / delete the device.  Methods to be used:

attach(String controllerName, int pin) / detach(String controllerName)

2. An Start/Stop button that start/stops sending pwm to the servo. The controller and pin wil still be set but pwm will turn on/off. This button will not be enabled until the controller & pin has been set.

Methods to be used:

attach(int pin) / detach()

In scripts you have the option to do both in one command as it has been before. 


I was a bit unclear about what was broken in the current GUI's.

You should be able to toggle between Attach/Detach but after Detach/Attach, the state of the button never changes. 



calamity's picture

in webgui, there was a mix

in webgui, there was a mix between the attach/detach type, it was doing deviceAttach(controller, pin) but servoDetach()

I change it to deviceAttach(controller, pin) and deviceDetach(controller) and it seem to be working in my build, I can attach/detach the servo and the state of the buttons are changing.

I don't think I did the change in the Swing Gui


calamity's picture

Ok, I did some testWebGui is

Ok, I did some test

WebGui is working for me

if you use the swing GUI, then go to the webGui, webGui will not be working

I change line 157 of to send("detach",controller.getSelectedItem());

and the attach/detach on both Swing and Web GUI are working


The first time I hit detach in the swing GUI it throw an error. but the attach/detach happen. If I attach/detach again the error will not throw again. Not sure what throw the error.

On the webGui,  when you detach, the value of the controller dropdown get reset to nothing. so you have to manually set it back to the right controller before attaching again. It's just an annoying thing

I can push the change, but want to find about the error first

Mats's picture


I have made the same change in, so they both should work the same way now.

Oh. I didn't see your last message. Sorry. I only changed the line 157 the same way as you describe.

Just a little worried that executing detach() from a script also may cause that state in both GUI's to change.



calamity's picture

yes, a detach() in script

yes, a detach() in script will make the GUI attach button non-working until attach() was called in script again because attach/detach button don't do the same thing as attach()/detach() in script


so I  think that  the button in GUI can only be click if

  • controller is set and connected

show attach or detach  depending of if the servo is energized or not (isAttached() method)

Right now it only use isAttached() to do both thing