Controlling up to 72 servos from Bonsai via Arduino

231 views
Skip to first unread message

r.ho...@columbia.edu

unread,
Jan 10, 2018, 2:26:41 PM1/10/18
to Bonsai Users
Happy new year everybody!

I am totally new with Bonsai and in  this group. However I am familiar with Arduino, electronics, and software in general. 
Some of my colleagues in school are using Bonsai to analyze an image and then control servos mainly in two positions as needed, up to 72 servos!
So far we have seen how to do this with a few servos using your servo sink and regular i/o in arduino, but your protocol, arduino hardware, and to some point firmatta) appears to be limited to 16 pins as it is. Now we got a I2C driver shield for servos that I am familiar with and I can access and do what I need in excess of 72 servos from Arduino, so the hardware limitation is gone. However I am trying to get the path of less resistance to control this from Bonsai.

I studied a little bit Firmata and trying to modify standard Firmatta.ino, even the base class firmatta.cpp to capture messages and see how could I send pin value from your servo sink above 15 that would be the most transparent and easy way for future work, I even went to spy the protocol messages from Bonsai, where I can see three byte messages consistent with MIDI. Before is a pin mode command as hex  F4 pp 04 where pp is the pin  numer, this pp passes up to FF, so I got excited. Then goes the Servo Sink  (I think) as hex Ep vv  vv where p is the pin limited to F (only 16 pins) and vv vv are two 7 bits  combination LSB + MSB up to 2^14.  I also noticed a similar behavior using the analog sink

I hope I am wrong, but it appears there is no straight way unless there would be some other more flexible sink, I2C would be ideal so I don't need to mess Firmatta much? Another possibility I am thinking is if I could pass easily some generic data using the Firmatta sysex command if Bonsai could support it. If so I could pass an address and create software switch in Firmatta

In a desperate case, what I am thinking is to use the servo sink with a special valid pin value as 0, and then codify in the 14 bit servo int (position) the servo address up to 99. Besides being ugly, the 14 bit limitation would limit the number of angular positions available, what would be OK in this application.
 
Any suggestion and ideas would be really appreciated.

Thank you,
Rick.

    

Gonçalo Lopes

unread,
Jan 10, 2018, 4:13:45 PM1/10/18
to r.ho...@columbia.edu, Bonsai Users
Hi Rick and welcome to the forums!

That sounds a cool project :-) i would be curious to see some videos at the end of what you are trying to achieve, let me know if you end up sharing anything about it.

About your questions, all the information about Bonsai handling of the Firmata protocol is available on the bitbucket repository at:

Specifically you want to look at the following files:
  - Arduino.cs
  - ObservableArduino.cs
  - ServoOutput.cs

These three form the backbone of the servo communication using Firmata. You can customize yourself the behavior of the protocol at multiple points. As far as I can see there is no explicit limitation of pin number on the Bonsai side, but indeed there may be in the Firmata protocol itself.

If your Firmata implementation can handle it, you can take control of the Arduino serial port itself and send custom sysex commands. For that you need to use a strategy similar to what is used in ObservableArduino.cs:

var connection = ArduinoManager.ReserveConnection(portName)
lock (connection.Arduino)
{
// command 0x76 (I2C) + slave_addr (LSB,MSB) + data0 (LSB, MSB) + data1(LSB,MSB) + ....
connection.Arduino.SendSysex(0x76, LSB, MSB, etc... see above);
}

It should be relatively easy to code up some custom Sink to get the arduino connection and send the needed sysex, if you are familiar with that level of the protocol.
Looking back, I think it makes sense to include some kind of I2C support in the next version of Bonsai, and I have taken a note to do so.

Current ETA end of January, early February, we'll see.
In any case, everything is there to make your own hackable version. Let me know if you would like me to explain anything in more detail.

Hope this helps.


--
You received this message because you are subscribed to the Google Groups "Bonsai Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bonsai-users+unsubscribe@googlegroups.com.
Visit this group at https://groups.google.com/group/bonsai-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/bonsai-users/302a327f-7989-4cfa-bcc0-72a1026ea6fd%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Rick

unread,
Jan 11, 2018, 1:39:52 AM1/11/18
to Bonsai Users
 Thank you Goncalo, I looked to your source and it looks there is in fact a limitation in the Arduino implementation of Firmata protocol in Bonsai, and I am hoping it would have a simple fix that I need to dig further.
 
The current Bonsai limits the access to the 16 first pins this is because uses the standard Analog Write of Firmatta that is in fact limited to 16 pins. This happens in the Arduino.cs file in the method AnalogWrite in this line: commandBuffer[0] = (byte)(ANALOG_MESSAGE | (pin & 0x0F));
 
The fact that the command byte is split to share pin and constant will limit to 16 pins, this is by Firmata protocol of Servo. I found the protocol info tonight:
 
This is just the standard SET_PIN_MODE message: Set digital pin mode (The problem is that this happens just once at the start to attach the servo to the pin)
0  set digital pin mode (0xF4) (MIDI Undefined)
1  pin number           (0-127)
2  state                (SERVO, 4)
Write to servo, servo write is performed if the pin mode is SERVO  (Then this is sent for every change in servo but uses the pin info in the less significant nibble (LSN) of the next command )
0  ANALOG_MESSAGE       (0xE0-0xEF)    <--- HERE IS THE TROUBLE ONLY THE LSN passes the pin info the rest is truncated, and so limited to the 16 first pins. 
1  value LSB
2  value MSB

However your proposal is in line with what I found tonight in the Firmata Protocol. There is explicitly proposed and implemented  a servo control for more than 16 pins using START_SYSEX and a Extended Analog Message. I only would have to add the I2C code in the firmata side to use it, and this way I could use your Servo sink almost as it is. To be honest to do a custom sink may be to much of a learning curve and time for me, and you would do a way better job.

For now, what I am thinking is to implement a quick way around using START_SYSEXand the extended analog write inside  the Bonsai AnalogWrite method. I could even put an if that uses only when the pin is detected above 15 to minimize to mess with other code using normal analog writes.

So what I would need is to modify the arduino.cs file, compile, and install. What I would appreciate is if you can point me to some info of how to do this easily to update my Bonsai-Arduino plug with it. I used C Sharp long ago, and I may download visual studio to compile, that looks straight forward, but I am not familiar with this Bonsai / Plug structure in my computer and what files and where need to be updated to success, and have my custom Bonsai.


As for the videos for this project no problem in my side, but I think you know the people involved likely better than me, I'll talk to the team  ;)   
Thank you,
Rick.





Gonçalo Lopes

unread,
Jan 11, 2018, 8:43:28 AM1/11/18
to Rick, Bonsai Users
Thanks for the clarification, you are completely correct, the standard firmata AnalogWrite is indeed limited to 16 pins.

I have opened two issues for adding support both for extended analog and I2C for the next version:

In the meantime, you can build your own version of the Arduino package. The easiest way for now would be to use Visual Studio 2012 or 2015 (2017 still not supported until next month).
Follow this tutorial to setup a custom package, and check that it works:

It's a bit dated, the start menu shortcut is now called "Install Bonsai VS Extensions". But other than that, it should go relatively smoothly.
After you get it to work, you should be able to run and debug your own custom code.

Then go ahead and simply copy all the files in the original Bonsai.Arduino into your own project. You may have to change the namespace if you want to use them side-by-side.
Add any modifications that you need and that should be it.

Let me know if you run into troubles.
Hope this helps.

Thank you,
Rick.





--
You received this message because you are subscribed to the Google Groups "Bonsai Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bonsai-users+unsubscribe@googlegroups.com.
Visit this group at https://groups.google.com/group/bonsai-users.
Reply all
Reply to author
Forward
0 new messages