Harsha_Nagaraja
Hi,

I'm facing an issue with the switching frequency of the digital I/Os while controlling the speed of a stepper motor. Previously, when using a Raspberry Pi, I set a pulse delay of 1ms (cycle time 1ms) and was able to smoothly control the motor rotation up to 100 RPM. However, after switching to PiXtend with the same cycle time configuration, the motor barely reaches 1 RPM and moves in distinct steps (tack tack!!).

I understand that PiXtend communicates with the extended board via SPI, which introduces additional delay compared to the Raspberry Pi’s direct GPIO access. To compensate, I adjusted the cycle time and pulse delay to 5ms, which improved the speed slightly (around 3 RPM).

However, I’m struggling to understand why there is such a significant difference in the pulse output through the digital output pins. Could this be related to the configuration settings of the PiXtend board or OpenPLC?
Any insights or suggestions to resolve this issue would be greatly appreciated!

Thanks in advance.
Reply 0 0
thiagoralves
You're trying to control the speed generating pulses in software. This is not the correct approach, as any additional latency caused by any part of your program or driver or system will mess up your pulses, as you could see. To generate fast pulses, you must use hardware. For example, on the Pi, instead of turning the output on/off every cycle (this is called bit-banging), you could just use the PWM output, that generates steady pulses in hardware. I don't know if the PiXtend platform has any hardware to generate pulses. If it doesn't, then you're out of luck. But even if it does, I don't know if that was implemented in the OpenPLC driver.
Reply 0 0
Harsha_Nagaraja
Thank you. That makes sense.
 
The PiXtend 2L has six PWM outputs, and according to the documentation, it supports four operating modes for PWM control: Servo Mode, Duty Cycle Mode, Universal Mode, and Frequency Mode.

I attempted to set a value of 32768 (50% duty cycle) to %QW2, but I did not observe any voltage changes on the PWM output pin.
I am confused about how to configure the frequency and duty cycle correctly to generate pulses for the stepper motor driver. Could you please guide me through the initial setup for controlling a stepper motor using the PWM?

Please check the image attached of PiXtend2l Physical addressing and let me know if it is correct.
Reply 0 0
thiagoralves
This seems to be correct. The official pin mapping can be found here: https://autonomylogic.com/docs/2-4-physical-addressing/
 
However it seems that you will only be able to change the duty cycle, not the frequency. Perhaps, if everything fails, instead of using PiXtend driver, you could write your own using PSM. PSM is a nice driver interface for OpenPLC that allows you to write drivers using Python code. It is very simple and straightforward. Since PiXtend has several Python example code to follow, you can use one of these examples to address the IOs you need, and then just store the IO values in %IX, %QX, %QW or %IW locations. To enable PSM, on the hardware tab in the OpenPLC webserver, select PSM on Linux. This will open a code window below where you can insert your code. Here is the documentation for PSM that is embedded in the code itself:
 
#                  - OpenPLC Python SubModule (PSM) -
#
# PSM is the bridge connecting OpenPLC core to Python programs. PSM allows
# you to directly interface OpenPLC IO using Python and even write drivers
# for expansion boards using just regular Python.
#
# PSM API is quite simple and just has a few functions. When writing your
# own programs, avoid touching on the "__main__" function as this regulates
# how PSM works on the PLC cycle. You can write your own hardware initialization
# code on hardware_init(), and your IO handling code on update_inputs() and
# update_outputs()
#
# To manipulate IOs, just use PSM calls psm.get_var([location name]) to read
# an OpenPLC location and psm.set_var([location name], [value]) to write to
# an OpenPLC location. For example:
#     psm.get_var("QX0.0")
# will read the value of %QX0.0. Also:
#     psm.set_var("IX0.0", True)
# will set %IX0.0 to true.
#
# Below you will find a simple example that uses PSM to switch OpenPLC's
# first digital input (%IX0.0) every second. Also, if the first digital
# output (%QX0.0) is true, PSM will display "QX0.0 is true" on OpenPLC's
# dashboard. Feel free to reuse this skeleton to write whatever you want.
 
Reply 0 0
Harsha_Nagaraja
Thank you for your suggestion.

I found out that precisely controlling the stepper motor using PiXtend's PWM signal is nearly impossible, as there is no way to interrupt the pulses generated by the system. I tried to perform the control with timer constraints but it still it wasn't precise enough. Therefore, I am now using an Arduino for stepper motor control while handling the rest of the control tasks with PiXtend.
 
 
I am considering uploading all necessary motor control logic to the Arduino via the Arduino IDE and then triggering specific functions from the main program through Modbus.
Does this approach seem viable, or is there a better way to integrate Arduino with OpenPLC and PiXtend for stepper motor control?
Any insights or alternative solutions would be greatly appreciated.
Reply 0 0
thiagoralves
This seems like a good approach. If you use OpenPLC Editor for the Arduino code, you will have that Modbus link granted with no code. You can have your stepper motor code being written on an Arduino Extension inside the OpenPLC Editor, and then, since the Arduino board will run the OpenPLC Runtime as well, you can add the Arduino board as a slave device of your PiXtend board. Once you do that, all located variables of the Arduino board will be mapped as located variables on your PiXtend! I have a video that might be useful for you, where I explain how to do this using a Raspberry Pi and an Arduino Uno: https://youtu.be/dQ8GcfpGDuA?si=lI4dgzdiBXT3TnJo
Reply 0 0
Harsha_Nagaraja
Thank you your suggestion.

I wrote a test program where the stepper motor control logic is handled in the Arduino extension. In the Main program of the PLC, I created an external variable and linked it to the Arduino program to control the start and stop functions.

I'm unsure about the correct method to upload the code. Should I use "Transfer to PLC" in OPENPLC, or do I need to download the st.prg file and upload it via the web interface?. Because according to my understanding using "Transfer to PLC" will upload program to arduino and through web interface we can upload program to the PiXtend.

Please check the code attached to this post.
Reply 0 0
Harsha_Nagaraja
Hi,

Is it like, we upload the logic we want the arduino to control at first and later we upload the PLC code to the PiXtend through web interface?. I'm consfused 😕.
Reply 0 0
thiagoralves
You have two devices, therefore you must have two programs. The program with your Arduino Extension code must go to your Arduino board. You upload it using the orange circuit icon on the toolbar "Transfer to PLC". Select your Arduino board from the list and enable Modbus RTU so that it can communicate with a master. Then you write another program for your PiXtend, with the logic for the master. Since the PiXtend runs the Linux runtime, you upload the program through the web portal (as an .st file).
Reply 0 0
Harsha_Nagaraja
vor 2 Minuten · Bearbeitet
Thank you.

I created a MotorRun variable in the arduino extension and in the PLC main program (as External). The Motor turns on and off based on the bool state of MotorRun variable set before the uploading to the Arduino.

##UNABLE TO CHANGE THE STATE OF EXTERNAL MOTOR CONTROL VARIABLE VIA MODBUS TCP SLAVE

I have two slave devices one is 'Modbus TCP device' and the other as 'Arduino Mega'.

I created one more variable named MotorStart to control the MotorRun state and linked the MotorStart Variable to Modbus TCP slave %QX100.0.
But changing the state of MotorStart does not have any effect on the MotorRun state. Please can you check the image attached to this post and the project file and let me know what is the problem?

I had a issue in uploading program to arduino when the slave device (Modbus TCP) was linked to a variable?
Why is it not possible to upload the program to arduino when the slave device address are linked to a variable in the main program?
 

##CONFLICT IN ADDRESSING

I also found a conflict that setting the Modbus TCP slave coil %QX100.0 to 'HIGH' sets the coil to 1 and also switches the PiXtend DO pin '0' to high. The local hardware pin variable %QX0.0 also sets the output HIGH and LOW. Can you please explain what is causing this issue?


I understand it is better to create a seperate discussion for these topics. But at the moment I thought it is best to post it here.
Later on i will create a seperate discussion and post the answers myself. Thank you.
Reply 0 0
thiagoralves
You're mixing up concepts here. You're using %QX100.0 address on your Arduino program, which is not a valid address. %QX100.0 is out of range for the Arduino board. This address should be used on your master, not on the slave. You must use %QX0.0 on your Arduino board, which will be mapped to somewhere in the %QX100+ range in your master, depending on your slave configuration.
Reply 0 0
Harsha_Nagaraja
Thank you.

I noticed a performance difference when I uploaded the following code to Arduino Mega through 'Transfer To PLC' and also through Arduino IDE.
 
This is a short program written to control the stepper motor using AccelStepper library

#define STEP_PIN 9
#define DIR_PIN 8

AccelStepper stepper(AccelStepper:😃RIVER, STEP_PIN, DIR_PIN);

void setup() {
stepper.setMaxSpeed(1000);
stepper.setAcceleration(500);
stepper.moveTo(3200);
}
 
void loop() {
stepper.run();
}

Note-
void setup() and void loop() were changed to void sketch_setup() and void sketch_loop() for Openplc interface.

The differences noted were,
- No acceleration and deceleration effects seen.
- Direction of rotation was reverse.
- Vibrating noise.


Question
- Are these libraries installed? If no, How to install them?
- If it is installed , why there is a performance difference?
Reply 0 0
thiagoralves
Reply 0 0
Harsha_Nagaraja
Thank you, the FastAccelStepper library is working as intended.

Can you please give me brief overview of how the program variables created in the Arduino extension are linked to the variables in the master program?
 
So far I have understood that I should have two seperate program, Program one for arduino control that is uploded to arduino and another master program. I understood that located variables (hardware pins) of arduino are made accessible to master as slave device (%QX100.X) but how to change the state of variables defined (nor pyhsical addressinfg) in the arduino from the master program.

For example,
//In Master program,

MotorState := True or False;

//Arduino extension program
MotorRun := Bool (External)

if (MotorRun) {
//Execute motor command
}

By changing the state of the MotorRun variable in the master program I should be able to execute the motor control. Can you please explain how can this be implemented?
Reply 0 0
thiagoralves
Variables you create in the variable table at the top of the Arduino extension are shared between the Arduino side and the PLC side. All you have to do is declare the same variable (same name and type) on the PLC side as external
Reply 0 0
Reply