I need to implement bidirectional RPC calls for communication between the M4 and M7 cores on the Portenta H7. The goal is for the M4 core to call a function on the M7 core to run a motor. If the motor hits an endstop, the M7 core should then call a function on the M4 core to trigger an error. The start command and error messages are sent via Modbus TCP to my HMI.
Currently, calling the function to run the motor works well. However, when the motor hits an endstop, the network communication and the Portenta H7 freeze, causing the motor to continue running without stopping. Notably, I do receive the error message on the HMI, indicating that the RPC call from the M7 core is successfully made. However, after this point, everything stops working.
This indicates that while the RPC call from the M7 core to the M4 core is executed and the error is reported to the HMI, the system becomes unresponsive, leading to continuous motor operation and communication issues.
M7 Core
#include <Ethernet.h>
#include <SPI.h>
#include <RPC.h>
#include <Arduino_PortentaMachineControl.h>
#include <ArduinoModbus.h>
#include <pinDefinitions.h>
#include <ArduinoRS485.h>
#include "config.h"
// Network configuration
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
IPAddress ipAddress(169, 254, 101, 102);
IPAddress serverAddress[2] = {
IPAddress(169, 254, 101, 110),
IPAddress(169, 254, 101, 111)
};
EthernetClient ethernetClient[2];
ModbusTCPClient modbusTCPClient[2] = {
ModbusTCPClient(ethernetClient[0]),
ModbusTCPClient(ethernetClient[1])
};
void setup() {
Serial.begin(9600);
while (!Serial) {
; // Wait for serial port to connect. Needed for native USB port only
}
Serial.println("Starting Ethernet and Modbus TCP client...");
Ethernet.begin(mac, ipAddress);
MachineControl_DigitalOutputs.begin();
ethernetClient[0].setTimeout(10000);
ethernetClient[1].setTimeout(10000);
RPC.begin();
RPC.bind("throwError", throwError);
Serial.println("Setup complete.");
}
void loop() {
if (!modbusTCPClient[0].connected()) {
Serial.println("Connecting to Modbus server...");
if (modbusTCPClient[0].begin(serverAddress[0])) {
Serial.println("Connected to Modbus server.");
} else {
Serial.println("Failed to connect to Modbus server.");
}
} else {
manualProcess();
}
}
void manualProcess() {
int result;
int speed;
if (modbusTCPClient[0].coilRead(HMI::coilRegisters::GUIDE_LEFT)) {
speed = modbusTCPClient[0].holdingRegisterRead(HMI::holdingRegisters::GUIDE_SPEED);
result = RPC.call("updateStepperMotorState", speed, 1, true).as<int>();
delay(100);
} else if (modbusTCPClient[0].coilRead(HMI::coilRegisters::GUIDE_RIGHT)) {
speed = modbusTCPClient[0].holdingRegisterRead(HMI::holdingRegisters::GUIDE_SPEED);
result = RPC.call("updateStepperMotorState", speed, 0, true).as<int>();
delay(100);
} else {
result = RPC.call("updateStepperMotorState", MANUAL_GUIDE_SPEED, 0, false).as<int>();
delay(100);
}
}
int throwError(int errorCode) {
modbusTCPclient[0].coilWrite(0x00, 0x05);
return 0; // Return a value to complete the RPC call
}
M4
#include <RPC.h>
#include <Arduino_PortentaMachineControl.h>
#include <AccelStepper.h>
#include <pinDefinitions.h>
#include "config.h"
AccelStepper GuideStepper(AccelStepper::DRIVER, PinNameToIndex(MC_DO_DO0_PIN), PinNameToIndex(MC_DO_DO1_PIN));
bool autoMode = false;
int GuideStepperDirection = false;
int moveStepperMotor = false;
int stepperMotorSpeed = 500;
void setup() {
RPC.begin();
RPC.bind("updateStepperMotorState", updateStepperMotorState);
GuideStepper.enableOutputs();
GuideStepper.setMinPulseWidth(130);
GuideStepper.setAcceleration(1000);
GuideStepper.setMaxSpeed(1500);
GuideStepper.setSpeed(1500);
MachineControl_DigitalOutputs.begin();
GuideStepper.setMaxSpeed(GUIDE_MAX_SPEED);
GuideStepper.setAcceleration(GUIDE_ACCELERATION);
}
void loop() {
if (moveStepperMotor)
{
if (GuideStepperDirection)
{
GuideStepper.setSpeed(stepperMotorSpeed);
} else
{
GuideStepper.setSpeed(-stepperMotorSpeed);
}
GuideStepper.runSpeed();
}
if (GuideStepperDirection)
{
int result = RPC.call("throwError", (int)ERROR_ENDSTOP_HIT).as<int>();
delay(100);
}
}
int updateStepperMotorState(int speed, int direction, bool state)
{
GuideStepperDirection = direction;
stepperMotorSpeed = speed;
moveStepperMotor = state;
}
Initially, I suspected that my software couldn’t handle two simultaneous RPC calls in different directions. However, after adding delays and trying various methods, I determined that this was not the issue. If I replace the Modbus call in the throwError function with a simple Serial print, it works fine. We’ve also confirmed that the RPC call to the throwError function is indeed being executed.
This led me to consider that the issue might be related to losing the connection while handling asynchronous routines. I attempted adding yield() calls and tried to understand and implement mutexes, but nothing resolved the problem.