btz5026
Hello. I'm new to OpenPLC and Ladder Logic coding. I searched the forum and read the documentation, but I still have a few questions that I'm hoping someone can help me with.
 
I'm trying to control a servo motor using an Arduino Uno and the "PWM_CONTROLLER" block in OpenPLC Ladder. Specifically, I'm trying to control the "TinkerKit Braccio Robot" arm: https://docs.arduino.cc/retired/getting-started-guides/Braccio/
 
I've had a little success with controlling 2 of the joints, but I can't get beyond this.
 
The motor controller shield uses the following pins to drive the 6 motors (M1 = base rotation, M2 = shoulder, etc.):
[203128%2F9331510%2Fmceclip0]
 
I'm trying to address these pins using the "CHANNEL" input on the "PWM_CONTROLLER" block. From experimentation, channel = 0 seems to map to pin #9 and channel =1 maps to pin #10. Channel 2 and 3 seem to do nothing, and I haven't experimented with other values beyond this.
 
FYI, a small quirk of this arm and hardware requires pin #12 to be "HIGH" to energize the arm and provide full voltage to the motors (part of the "soft start" protection). I've used the variable "start" to accomplish this in the program below.
 
Here is my test program that successfully drives the M2 shoulder joint to a desired position:
[203128%2F9331514%2Fmceclip0]
 
My question:
1) How does the "CHANNEL" input on the "PWM_CONTROLLER" block map to the physical pins on the Arduino Uno? From testing, it is not a simple 1:1 mapping (i.e. a value of "1" here does not correspond with pin #1).
 
2) Can I change this mapping somehow? I tried changing the "I/O Config" and the "Analog Outputs" (see below) when transferring the program to the PLC, but that seems to do nothing.
 
[203128%2F9331530%2Fmceclip0]
 
Thank you so much!
thiagoralves
The PWM_CONTROLLER block uses the hardware PWM unit inside the processor. Therefore, the amount of channels and supported frequencies will vary depending on the hardware. In case of the Uno, it only has 3 channels, available on pins 9,10, and 11. Since they are provided by the hardware, you can’t change those pins. See the Uno driver code for more information: https://github.com/thiagoralves/editor/blob/openplc-master/arduino/src/hal/uno_leonardo_nano_micro_zero.cpp
Reply 0 0
btz5026
Thank you for the quick response and clarification! That makes sense.
 
Since I need to control 6 servo motors with this arm, do you have an alternative approach or suggestion instead of the "PWM_CONTROLLER" block which is limited to 3 pins on the Uno?
 
I read in some other posts that perhaps I can just do an analog output. Any suggestions for how to use that here?
 
Alternatively, is there a way to create a "soft" PWM like this in Ladder (manually generating the wave form with "digitalWrite" commands):
 
Thanks again!
Reply 0 0
thiagoralves
Controlling 6 servo motors might not be feasible with a hardware so limited as the Uno. Perhaps if you do bare metal coding it might work, but not while running a full IEC 61131-3 PLC runtime. You won't be able to do a soft-PWM on ladder since you would be limited by the scan cycle period, which is too long for a PWM wave. Your best bet would be to use a more powerful platform, or perhaps try controlling the servos using Arduino code on an Arduino Extension. Just keep in mind that even running Arduino code, it won't be pure "baremetal" as the loop() function is controlled by the PLC scan cycle still.
Reply 0 0
btz5026
Got it, thank you! I'll try using Arduino Extension on the Uno or considering upgrading the hardware.
 
Why do you only support 3 channels (pins 9, 10, and 11) on the Uno with the PWM_CONTROLLER block? According to this, the Uno supports hardware PWM on pins 3, 5, 6, 9, 10, 11. Could this be expanded?
 
Reply 0 0
thiagoralves
analogWrite() calls do not allow you to change frequency. It is fixed at the Arduino-defined frequency of a few hertz. The PWM_CONTROLLER block allows you to have different frequencies and duty cycles in each channel. I’m using a popular PWM library to handle the PWM generation in hardware. As far as I can tell, I believe the number 3 comes from the AVR chip. It is a hardware limitation. There isn’t much that can be done about it.
Reply 0 0
thiagoralves
By the way, if you simply use the analog pins at %QW locations you will be using the same analogWrite() calls. But again, I don’t know if that will work for you because I think those servos require a specific frequency to work
Reply 0 0
btz5026
Thanks for the suggestions! I was able to get this working perfectly on the Uno using the "Arduino Extension" and the Servo.h or Braccio.h libraries. I can now control all 6 motors with the Uno while using Ladder for the high level control and Arduino code for the low level control.
 
This "Arduino Extension" feature seems to be relatively new and not well documented, but it is SUPER helpful. I wasn't able to find much information about it, but it was fairly self explanatory and this link was helpful to understand variable sharing:
 
No further questions at this time. Thanks for the excellent support and for developing such a great tool!
Reply 1 0
Reply