Gloworm72
Hello, I'm working on a project that will involve 2 Arduino Mega's and potentially an HMI.

One will contain the Ladder Program running on OpenPLC, which will control indicator lights, detect limit switches, as well as communicate its status to an HMI of some sort.

Due to the limitations of the PWM controller inside openPLC for controlling a stepper motor with precision, I will need a second Arduino that can receive and send signals from the 1st Arduino loaded with my openPLC program. If the 1st Arduino determines it's ok to move the stepper motor through its ladder logic, the idea is it would send a flag to the 2nd Arduino that would start a function like "move Forward" which uses the usual accelstepper.h libraries.

My questions are:

1. Should I try using Modbus RTU to have the 1st Arduino communicate with the 2nd Arduino and is that possible? Or is it only possible to have Arduino's be slave devices to a version of runtime running on a real Linux system or similar?

2. Will it be more simple to simply send digital outputs to the other Arduino's digital inputs?

3. Would it be possible to have an HMI controlling and monitoring the 1st Arduino and if so what resources are there to learn how to do this? And if I used an HMI, would that mean I should go down the route of learning Modbus RTU for everything?
Reply 0 0
thiagoralves
OpenPLC only supports Modbus slave on Arduino boards. Two slaves cannot communicate without a master, so you cannot send signals from one Arduino to another. Given your specific scenario, why don't you just write the servo motor code in an Arduino Extension instead of using a completely separate Arduino board just for that? You can have the Arduino Extension running any Arduino code you want, and your PLC program also running all together on the same board.
Reply 0 0
Gloworm72
OpenPLC only supports Modbus slave on Arduino boards. Two slaves cannot communicate without a master, so you cannot send signals from one Arduino to another. Given your specific scenario, why don't you just write the servo motor code in an Arduino Extension instead of using a completely separate Arduino board just for that? You can have the Arduino Extension running any Arduino code you want, and your PLC program also running all together on the same board.
Thank you for the quick reply. I tried this at first, however, I ran into an issue. Using the same speed and acceleration controls that I gave the stepper motor in the normal Arduino IDE, using an extension seemed to limit the speed after a certain point. My guess was that having a scan time of 20ms was blocking the Arduino code enough to make it unable to send a fast enough PWM signal. I then decreased the scan time to 1ms, and the motor moved much faster, but when accelerating would have an audible "jump" to higher speeds in a number of steps.

I also calculated the time the motor would need to be spinning for to move 200 steps for example, and it would take longer than expected to reach that number of steps while using the arduino extension.

If there is some way to get around this, that would simplify my project immensely, so if there's anything you can think of to make the extension file run better please let me know.
Reply 0 0
thiagoralves
If you're depending on the scan cycle to control a stepper motor, then you're doing it wrong. Stepper motors require precise control. You must use hardware timers to do that properly. These timers will work independently of the scan cycle, delays and other things in your code. I'm pretty sure that there are arduino libraries out there that can control stepper motors using hardware timers. Just use one of those and you'll be good.
Reply 0 0
thiagoralves
Reply 0 0
Gloworm72
If you're depending on the scan cycle to control a stepper motor, then you're doing it wrong. Stepper motors require precise control. You must use hardware timers to do that properly. These timers will work independently of the scan cycle, delays and other things in your code. I'm pretty sure that there are arduino libraries out there that can control stepper motors using hardware timers. Just use one of those and you'll be good.
Interesting, here is the test code I was using for the extension file:
//////////////////////////////// sketch section ////////////////////////////////
//outside setup and loop
#include <AccelStepper.h>
AccelStepper testMotor(AccelStepper::DRIVER, 2, 23); //step 2, dir 23
long currPos = 0;
long destinationForward = currPos + 1000;
long destinationBackward = currPos - 1000;

void sketch_setup()
{
testMotor.setMaxSpeed(400);
testMotor.setAcceleration(400);
testMotor.setCurrentPosition(0);
}

void sketch_loop()
{
if (moveForward) {
testMotor.moveTo(destinationForward);
} else if (moveBackward) {
testMotor.moveTo(destinationBackward);
} else {
currPos = testMotor.currentPosition();
destinationForward = currPos + 1000;
destinationBackward = currPos - 1000;
}
testMotor.runSpeed();
    

}

So you are saying the accelstepper library internally creates a delay rather than using the millis() function?

To test if your suggestion works, would I need to include any timers myself?
Reply 0 0
Gloworm72
Interesting, here is the test code I was using for the extension file:
  1. //////////////////////////////// sketch section ////////////////////////////////
  2. //outside setup and loop
  3. #include <AccelStepper.h>
  4. AccelStepper testMotor(AccelStepper::DRIVER, 2, 23); //step 2, dir 23
  5. long currPos = 0;
  6. long destinationForward = currPos + 1000;
  7. long destinationBackward = currPos - 1000;
  8. void sketch_setup()
  9. {
  10. testMotor.setMaxSpeed(400);
  11. testMotor.setAcceleration(400);
  12. testMotor.setCurrentPosition(0);
  13. }
  14. void sketch_loop()
  15. {
  16. if (moveForward) {
  17. testMotor.moveTo(destinationForward);
  18. } else if (moveBackward) {
  19. testMotor.moveTo(destinationBackward);
  20. } else {
  21. currPos = testMotor.currentPosition();
  22. destinationForward = currPos + 1000;
  23. destinationBackward = currPos - 1000;
  24. }
  25. testMotor.runSpeed();
  26.     
  27. }

So you are saying the accelstepper library internally creates a delay rather than using the millis() function?

To test if your suggestion works, would I need to include any timers myself?
Here's my ladder diagram:
[203128%2F9211774%2Fmceclip0]
Reply 0 0
thiagoralves
Interesting, here is the test code I was using for the extension file:
  1. //////////////////////////////// sketch section ////////////////////////////////
  2. //outside setup and loop
  3. #include <AccelStepper.h>
  4. AccelStepper testMotor(AccelStepper::DRIVER, 2, 23); //step 2, dir 23
  5. long currPos = 0;
  6. long destinationForward = currPos + 1000;
  7. long destinationBackward = currPos - 1000;
  8. void sketch_setup()
  9. {
  10. testMotor.setMaxSpeed(400);
  11. testMotor.setAcceleration(400);
  12. testMotor.setCurrentPosition(0);
  13. }
  14. void sketch_loop()
  15. {
  16. if (moveForward) {
  17. testMotor.moveTo(destinationForward);
  18. } else if (moveBackward) {
  19. testMotor.moveTo(destinationBackward);
  20. } else {
  21. currPos = testMotor.currentPosition();
  22. destinationForward = currPos + 1000;
  23. destinationBackward = currPos - 1000;
  24. }
  25. testMotor.runSpeed();
  26.     
  27. }

So you are saying the accelstepper library internally creates a delay rather than using the millis() function?

To test if your suggestion works, would I need to include any timers myself?
No, what I'm saying is that this library requires you to call a method every time you want to step. This is inefficient, as you need to stop what you're doing to call the step function every time. A library that uses hardware timers will setup a hardware timer to automatically step your motor without you having to do anything. In other words, the CPU could be busy doing something else and the steps will still be running in "parallel".
Reply 0 0
fg71
You could use Arduino extension to implement ModbusTCP Client, here I use millis() to demonstrate it with blinking ModbusTCP call.
The approach *anything* is possible if its possible in Arduino. This example have been tested on Arduino Nano Esp32 as Client and connected to ModbusTcp server. Sending ModbusTcp call every second with bool true or false.
 
This library I think is made for ESP-IDF Arduino core but it may work with other board to.
 
#include <WiFi.h>
#include <ModbusIP_ESP8266.h> // ESP-IDF Arduino core boards

// Replace with your network credentials
const char* ssid = "SSID";
const char* password = "PASSWORD";

// Modbus server IP address
IPAddress serverIP(MODBUS_TCP_IP); // Replace with the server's IP address

// Create a ModbusIP object
ModbusIP mb;

void setup() {
  // Initialize Serial for debugging
  Serial.begin(115200);

  // Connect to Wi-Fi
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi...");
  }

  // Initialize Modbus TCP client
  mb.client();
  mb.connect(serverIP);
}

unsigned long previousMillis = 0;
const long interval = 1000;
bool state = false;

void loop() {
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;
    if (state == false) {
      state = true;
    } else {
      state = false;
    }
    mb.writeCoil(serverIP, 8256, state);
    mb.task();
  }
}
 
Reply 0 0
Gloworm72
No, what I'm saying is that this library requires you to call a method every time you want to step. This is inefficient, as you need to stop what you're doing to call the step function every time. A library that uses hardware timers will setup a hardware timer to automatically step your motor without you having to do anything. In other words, the CPU could be busy doing something else and the steps will still be running in "parallel".
Ohh interesting, I will give it a shot. Is it similar to the pwm_controller function block in the ladder diagrams and how it uses the hardware to generate pulses, but with more built in functionality?
Reply 0 0
Gloworm72
You could use Arduino extension to implement ModbusTCP Client, here I use millis() to demonstrate it with blinking ModbusTCP call.
The approach *anything* is possible if its possible in Arduino. This example have been tested on Arduino Nano Esp32 as Client and connected to ModbusTcp server. Sending ModbusTcp call every second with bool true or false.
 
This library I think is made for ESP-IDF Arduino core but it may work with other board to.
 
  1. #include <WiFi.h>
  2. #include <ModbusIP_ESP8266.h> // ESP-IDF Arduino core boards
  3. // Replace with your network credentials
  4. const char* ssid = "SSID";
  5. const char* password = "PASSWORD";
  6. // Modbus server IP address
  7. IPAddress serverIP(MODBUS_TCP_IP); // Replace with the server's IP address
  8. // Create a ModbusIP object
  9. ModbusIP mb;
  10. void setup() {
  11.   // Initialize Serial for debugging
  12.   Serial.begin(115200);
  13.   // Connect to Wi-Fi
  14.   WiFi.begin(ssid, password);
  15.   while (WiFi.status() != WL_CONNECTED) {
  16.     delay(1000);
  17.     Serial.println("Connecting to WiFi...");
  18.   }
  19.   // Initialize Modbus TCP client
  20.   mb.client();
  21.   mb.connect(serverIP);
  22. }
  23. unsigned long previousMillis = 0;
  24. const long interval = 1000;
  25. bool state = false;
  26. void loop() {
  27.   unsigned long currentMillis = millis();
  28.   if (currentMillis - previousMillis >= interval) {
  29.     previousMillis = currentMillis;
  30.     if (state == false) {
  31.       state = true;
  32.     } else {
  33.       state = false;
  34.     }
  35.     mb.writeCoil(serverIP, 8256, state);
  36.     mb.task();
  37.   }
  38. }
 
Thank you for the example! I'm a student and this would be my first time using Modbus, so I am relatively unfamiliar with some of the basics. If I use one Arduino with openplc along with the arduino extension file for the motors, what would be the best way to connect an HMI to the Arduino without being wireless? Modbus RTU? Would the HMI have to be a device that has the runtime on it?

Ideally this project would have an HMI controlling the inputs of the Arduino, while the Arduino also controls the stepper motors and receives signals from limit switches.
Reply 0 0
fg71
There are 2 concept you are talking about real time (MCU) and general purpose computing (perform wide range of tasks and application).
HMI is general purpose computing task and running on dedicated cpu. It purpose is to communicate with a device to read or write the state of memory address. PLC program is running on real time system there every cycle is executed in order from start to end of the program and begin next cycle when reaching the end of its previous cycle. This type of execution is running forever. There general purpose computing task perform a action and later go to sleep. So other task could be performed and still have resource to not slow down execution.
 
Modbus Server or Slave is a device to collect data, and Client or Master control the data. In other words HMI send a message to Modbus server to get the state of memory address. Or set the value to memory address that is r/w compatible. You use a protocol to make this communication.
Modbus RTU use Serial communication and Modbus TCP use Ethernet/WiFi communication.
 
HMI is running on general purpose computer like Raspberry Pi (SBC), Windows or any hardware with x86-64 or aarch64 cpu.
You also need to consider what software you like to use to build your HMI. If you decide to use Modbus RTU you need to connect your device
to your general purpose computer. So you could make a communication with your HMI and your device (slave).
Reply 0 0
Gloworm72
Just wanted to throw in here that I tried @thiagoralves recommendation of using the fastaccelstepper library and that fixed my problem!

Thank you so much!
Reply 1 0
thiagoralves
Ohh interesting, I will give it a shot. Is it similar to the pwm_controller function block in the ladder diagrams and how it uses the hardware to generate pulses, but with more built in functionality?
Similar. The PWM pulses are also generated using hardware timers. That's why pulses continue perfectly with the exact frequency and duty cycle no matter what you're doing with the CPU. For any kind of precise control, you must use hardware units instead of bit-banging on software. Software makes things customizable, but any random latency kills the fine grained control.
Reply 0 0
Gloworm72
, here I use millis() to demonstrate it with blinking ModbusTCP call.
The approach *anything* is possible if its possible in Arduino. This example have been tested on Arduino Nano Esp32 as Client and connected to ModbusTcp server. Sending ModbusTcp call every second with bool true or false.
 
You could use Arduino extension to implement ModbusTCP Client, here I use millis() to demonstrate it with blinking ModbusTCP call.
The approach *anything* is possible if its possible in Arduino. This example have been tested on Arduino Nano Esp32 as Client and connected to ModbusTcp server. Sending ModbusTcp call every second with bool true or false.
 
This library I think is made for ESP-IDF Arduino core but it may work with other board to.
 
  1. #include <WiFi.h>
  2. #include <ModbusIP_ESP8266.h> // ESP-IDF Arduino core boards
  3. // Replace with your network credentials
  4. const char* ssid = "SSID";
  5. const char* password = "PASSWORD";
  6. // Modbus server IP address
  7. IPAddress serverIP(MODBUS_TCP_IP); // Replace with the server's IP address
  8. // Create a ModbusIP object
  9. ModbusIP mb;
  10. void setup() {
  11.   // Initialize Serial for debugging
  12.   Serial.begin(115200);
  13.   // Connect to Wi-Fi
  14.   WiFi.begin(ssid, password);
  15.   while (WiFi.status() != WL_CONNECTED) {
  16.     delay(1000);
  17.     Serial.println("Connecting to WiFi...");
  18.   }
  19.   // Initialize Modbus TCP client
  20.   mb.client();
  21.   mb.connect(serverIP);
  22. }
  23. unsigned long previousMillis = 0;
  24. const long interval = 1000;
  25. bool state = false;
  26. void loop() {
  27.   unsigned long currentMillis = millis();
  28.   if (currentMillis - previousMillis >= interval) {
  29.     previousMillis = currentMillis;
  30.     if (state == false) {
  31.       state = true;
  32.     } else {
  33.       state = false;
  34.     }
  35.     mb.writeCoil(serverIP, 8256, state);
  36.     mb.task();
  37.   }
  38. }
 
Can we use this library with the serial pins on an Arduino if I do not have wifi on my Arduino?
Reply 0 0
Reply