CobraPin Servo example code

138 views
Skip to first unread message

Dorus van der Linden

unread,
Apr 17, 2023, 1:44:53 PM4/17/23
to MPF Users

Dear all,

Does anyone have a MPF code example for connecting a servo to the CobraPin controller?

I am building a ball dropper with a valve I would like to open with a servo.

Thank you so much,

(This is for a Portal themed home brew)

cobra18t

unread,
Apr 19, 2023, 1:16:19 AM4/19/23
to MPF Users
I understand this is not as straightforward as it could be, so keep asking questions and I can help.

Here is a servo section below that I used in a test. The key is that only certain pins can be used for servo outputs and those pins have to be configured in the OPP config before MPF can use them. On CobraPin 2.0.0 and higher, those pins are marked in silkscreen, but any CobraPin version will work. The pins are X-0-8, X-0-9, X-0-10, and X-0-11 (where X could be 0, 1, or 3).


I also attached a sample config file for Board 0 on CobraPin that enables servo outputs on 4 pins. This was the same OPP config I used for the MPF example below. The relevant servo line from the OPP Config is
'\x40', '\xff', '\xfe', '\x96', \

From the OPP interface document:
"If an STM32 wing 1 is configured as an input wing or a solenoid wing, the inputs to the wing can be configured as outputs for driving servo motors. Each input can be individually configured as a PWM output where the PWM period is 20 ms, and has a tick rate of 10 us. If the input configuration byte is > 0x40, the PWM output has an active period of byteCfg * 10 us. The active portion of the PWM can be configured from .64 ms to 2.55 ms. This allows PWM connections to be tested in white wood mode. The PWM output can be modified using fade commands in the same way Neopixels can use fade commands to move slowly between two positions without the host sending multiple commands."

So the breakdown of those 4 numbers is as follows:
'\x40' - set 0-0-8 as a servo with a power-on pulse command of 0.64ms
'\xff' - set 0-0-9 as a servo with no power-on pulse command
'\xfe' - set 0-0-10 as a servo with a power-on pulse command of 2.54ms
'\x96' - set 0-0-11 as a servo with a power-on pulse command of 1.5ms


servos:
servo1:
servo_min: 0
servo_max: 1
speed_limit: 20
positions:
0.392: servo1_up
0.784: servo1_down
reset_position: 0.588
reset_events: reset_servo1, machine_reset_phase_3
number: 0-0-8
servo2:
speed_limit: 40
positions:
0.392: servo2_left
0.784: servo2_home
reset_position: 0.588
reset_events: reset_servo2, machine_reset_phase_3
number: 0-0-9
servo3:
servo_min: 0
servo_max: 1
speed_limit: 80
positions:
0.392: servo3_up
0.784: servo3_down
reset_position: 0.588
reset_events: reset_servo3, machine_reset_phase_3
number: 0-0-10
servo4:
positions:
0.392: servo4_left
0.784: servo4_home
reset_position: 0.588
reset_events: reset_servo4, machine_reset_phase_3
number: 0-0-11

CobraPin_Board0_servos.py

Dorus van der Linden

unread,
Apr 19, 2023, 6:19:21 AM4/19/23
to MPF Users
Thank you, this is a great start. I will try this in the next few evenings. And I will feedback the results. The   The PWM output can be modified using fade commands in the same way Neopixels can use fade commands to move slowly between two positions without the host sending multiple commands  - part is also interesting to me. Would you be willing to share the test config for this part? Regards Dorus.

Dorus van der Linden

unread,
Apr 19, 2023, 2:58:36 PM4/19/23
to MPF Users
Hi, I tried it this evening. The board config seems to work. Yippie!
Thank you so much for your support so far.

I am using MPF v0.55, and get the error: Config File Error in OPP: Platform <class 'mpf.platforms.opp.opp.OppHardwarePlatform'> does not support to configure servos. Please make sure the platform you configured for servos actually supports that type of devices. Should I upgrade to 0.56?
Warm regards Dorus

cobra18t

unread,
Apr 19, 2023, 3:38:16 PM4/19/23
to MPF Users
Regarding the use of the fade command, that occurs "under the hood" of MPF when you use the speed_limit config. That was included in my example.

Yes, that error implies MPF needs an update to a version that supports servos on OPP. 0.56 should do it!

cobra18t

unread,
Apr 19, 2023, 3:49:05 PM4/19/23
to MPF Users
Also, there is a minimum OPP firmware version for servo support. What firmware do you have? If it is less than 2.4.0.0, you may want to update it since there was a coil output bug before that.

cobra18t

unread,
Apr 24, 2023, 8:53:50 PM4/24/23
to MPF Users
Were you able to get this working?

Dorus van der Linden

unread,
Apr 26, 2023, 2:48:11 AM4/26/23
to mpf-...@googlegroups.com
I was able to update the boards. I will update python and mpf tomorrow! Thank you. I will also write a small instruction about this. 
Regards,
Dorus van der Linden

On 25 Apr 2023, at 02:53, cobra18t <tfule...@gmail.com> wrote:

Were you able to get this working?
--
You received this message because you are subscribed to a topic in the Google Groups "MPF Users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mpf-users/DbKRG73KCx4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to mpf-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/mpf-users/2b3d9f63-b193-4818-bd71-7f269d4c48ccn%40googlegroups.com.

grastle flowpat

unread,
Apr 29, 2023, 11:01:26 PM4/29/23
to MPF Users
Could somebody point me at the OPP interface document, I've googled the text and am advised no results found :-( It looks like a great read!

cobra18t

unread,
Apr 29, 2023, 11:35:22 PM4/29/23
to MPF Users

Dorus van der Linden

unread,
Apr 30, 2023, 5:18:33 AM4/30/23
to MPF Users
Dear,

Thank you, I have the servo working now! 

With the below steps
  1. Updated the firmware: https://openpinballproject.wordpress.com/2020/11/03/11-3-2020-loading-stm32-firmware
  2. Updated the configuration of board 1:  https://pinballmakers.com/wiki/index.php?title=CobraPin#STM32_Flashing_and_OPP_Configuration
  3. tested in the attract mode, with servo on pin 0-0-10 and a 3 led strip on board 0 : below example
With servo added as a test to the attract mode: 

#config_version=5
show_player:
  mode_attract_started:
    rainbow:
      show_tokens:
        leds: led1, led2, led3
shows:
  rainbow:
    - lights:
        (leds): red
    - lights:
        (leds): orange
    - lights:
        (leds): yellow
    - duration: 1s
      events: servo_down
    - lights:
        (leds): green
    - lights:
        (leds): blue
    - lights:
        (leds): purple
      duration: 3s
    - duration: 1s
      events: servo_up 


and in hardware config:

lights:
  led1:
    number: 0-0-0
    subtype: led
    type: rgb
  led2:
    number: 0-0-1
    subtype: led
    type: rgb
  led3:
    number: 0-0-2
    subtype: led
    type: rgb

servos:
  servo1:
    servo_min: 0
    servo_max: 1
    speed_limit: 80
    positions:
      0.392: servo_up
      0.784: servo_down
    reset_position: 0

    reset_events: reset_servo3, machine_reset_phase_3
    number: 0-0-10

cobra18t

unread,
May 1, 2023, 2:59:24 AM5/1/23
to MPF Users
That is great! Do we get to see a video?
Reply all
Reply to author
Forward
0 new messages