Bidirectional remote procedure calls fails with Portenta H7

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.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật