ESPNOW two way communication error

curious007
Posts: 6
Joined: Thu May 11, 2023 7:25 pm

ESPNOW two way communication error

Postby curious007 » Thu May 25, 2023 6:20 pm

hi, I'm using ESPNOW two way communication between two esp32s to control a robot with a remote . There are three stepper motors, 1 servo and 1 dc motor, 1 fsr on the robot (one dc motor at the remote, the speed of which is controlled by the fsr at the robot). Everything is working fine except the fsr, dc motor at the remote and the servo motor. below are the codes for robot and remote;

Remote:

Code: Select all


#include <esp_now.h>
#include <WiFi.h>
#include <ESP32MX1508.h>

#define open_pin 12

//FEEDBACK MOTOR
#define ROTA 4
#define ROTB 15
#define CH1 0
#define CH2 1
#define RES 12

MX1508 motorA(ROTA, ROTB, CH1, CH2, RES); //object for yrot motor

int motorSpeedMin = 0;
int motorSpeedMax = 4095;

// Define FSR threshold and motor speed limits
const int fsrThreshold = 20000;  // Adjust thresho
int fsrVal = 0;



//// Yrotation button pins
#define CW_BUTTON_PIN  25
#define ACW_BUTTON_PIN  26

//joystick pins
#define x  32
#define y  33
#define z 34
//#define y2 35


//STATES
#define FWD     2
#define REV     1
#define STOP    0
#define OPEN    0
#define CLOSE   1
#define CW      1
#define ACW     2

byte xState = 0;
byte yState = 0;
byte zState = 0;
byte jawState = 0;
byte rotState = 0;

byte lastxState = 0;
byte lastyState = 0;
byte lastzState = 0; 
byte lastjawState = 0;
byte lastrotState = 0;

bool move = false;

#define dead_pos 700
#define dead_neg -700


int map_x = 0;
int map_y = 0;
int map_z = 0;
int map_y2 = 0;
int val_jaw = 0;
int val_cw = 0;
int val_acw = 0;

// REPLACE WITH YOUR RECEIVER MAC Address
uint8_t broadcastAddress[] = {0xC0, 0x49, 0xEF, 0xD0, 0xDB, 0xB8};

// Structure example to send data
// Must match the receiver structure
typedef struct struct_message {
  byte xData = STOP;
  byte yData = STOP;
  byte zData = STOP;
  bool jaw = CLOSE;
  byte rot = STOP;
  int fsr;
} struct_message;

struct_message xTX;
struct_message xRX;

esp_now_peer_info_t peerInfo;

/*
  // callback when data is sent
  void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
  Serial.print("\r\nLast Packet Send Status:\t");
  Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail");
  }
*/

void setup() {
  // Init Serial Monitor
  Serial.begin(115200);
  pinMode(open_pin, INPUT_PULLUP);
    pinMode(CW_BUTTON_PIN, INPUT_PULLUP);
    pinMode(ACW_BUTTON_PIN, INPUT_PULLUP);
    
 motorA.motorBrake();  

  // Set device as a Wi-Fi Station
  WiFi.mode(WIFI_STA);

  // Init ESP-NOW
  if (esp_now_init() != ESP_OK) {
    Serial.println("Error initializing ESP-NOW");
    return;
  }

  // Once ESPNow is successfully Init, we will register for Send CB to
  // get the status of Trasnmitted packet
  //esp_now_register_send_cb(OnDataSent);

  // Register peer
  memcpy(peerInfo.peer_addr, broadcastAddress, 6);
  peerInfo.channel = 0;
  peerInfo.encrypt = false;

  // Add peer
  if (esp_now_add_peer(&peerInfo) != ESP_OK) {
    Serial.println("Failed to add peer");
    return;
    
  }
   esp_now_register_recv_cb(OnDataRecv);
}


void OnDataRecv(const uint8_t *mac_addr, const uint8_t *data, int data_len) {
  memcpy(&xRX, data, sizeof(xRX));

  fsrVal = xRX.fsr; 
  
// Map FSR value to motor speed
  int motorSpeed = map(fsrVal, fsrThreshold, 32767, motorSpeedMin, motorSpeedMax);
  
  // Limit motor speed within the defined range
  motorSpeed = constrain(motorSpeed, motorSpeedMin, motorSpeedMax);

  // Set motor speed
  motorA.motorGo(motorSpeed);

}

//state change for x link
void change_x() {
  if (map_x < dead_neg || map_x > dead_pos) {
    if (map_x > dead_pos) {
      xState = FWD;
    } else if (map_x < dead_neg) {
      xState = REV;
    }
  }
  else {
    xState = STOP;
  }
  return;
}




//state change for y link
void change_y() {
  if (map_y < dead_neg || map_y > dead_pos) {
    if (map_y > dead_pos) {
      yState = FWD;
    } else if (map_y < dead_neg) {
      yState = REV;
    }
  }
  else {
    yState = STOP;
  }
  return;
}

//state change for z link
void change_z() {
  if (map_z < dead_neg || map_z > dead_pos) {
    if (map_z > dead_pos) {
      zState = FWD;
    } else if (map_z < dead_neg) {
      zState = REV;
    }
  }
  else {
    zState = STOP;
  }
  return;
}


//state change for jaw
void change_jaw() {
  if (val_jaw == LOW ) {
    jawState = OPEN;
  }
  else {
    jawState = CLOSE;
  }
  //return;
}


//state change for ROTATION
void change_rot() {
  if (val_cw == LOW and val_acw == HIGH ) {
    rotState = CW;
  }
  else if (val_cw == HIGH and val_acw == LOW ){
    rotState = ACW;
  }
 else {
  rotState = STOP;
  }
  //return;
}



// preparing and sending data
void cast() {
  if (xState != lastxState || yState != lastyState || zState != lastzState || jawState != lastjawState || rotState != lastrotState) {            //This makes the async xTX data flow
    xTX.xData = xState;
    xTX.yData = yState;
    xTX.zData = zState;
    xTX.jaw = jawState;
     xTX.rot = rotState;//Cast the changed state to the xTX callback

    //sending
    esp_err_t result = esp_now_send(broadcastAddress, (uint8_t *) &xTX, sizeof(xTX));


    // getting feedback message
    //feedback();
    if (result == ESP_OK) {
      char x_buf[20];
      char y_buf[20];
      char z_buf[20];
      char j_buf[20];
      char r_buf[20];
      String sx, sy, sz, sj, sr = "";

      if (xState == FWD) {
        sx = "X_REV";
      } else if (xState == REV) {
        sx = "X_FWD";
      } else if (xState == STOP) {
        sx = "X_STOP";
      }

      if (yState == FWD) {
        sy = "Y_REV";
      } else if (yState == REV) {
        sy = "Y_FWD";
      } else if (yState == STOP) {
        sy = "Y_STOP";
      }

      if (zState == FWD) {
        sz = "Z_REV";
      } else if (zState == REV) {
        sz = "Z_FWD";
      } else if (zState == STOP) {
        sz = "Z_STOP";
      }

      if (jawState == OPEN) {
        sj = "OPEN";
      } else if (jawState == CLOSE) {
        sj = "CLOSED";
      }

      if (rotState == CW) {
        sr = "CW";
      } else if (rotState == ACW) {
        sr = "ACW";
      } else if (rotState == STOP) {
        sr = "NO ROT";
      }

      sprintf(x_buf, "Sending %s", sx);
      sprintf(y_buf, "Sending %s", sy);
      sprintf(z_buf, "Sending %s", sz);
      sprintf(j_buf, "Sending %s", sj);
       sprintf(r_buf, "Sending %s", sr);

      Serial.println(x_buf);
      Serial.println(y_buf);
      Serial.println(z_buf);
      Serial.println(j_buf);
      Serial.println(r_buf);//Only prints when packet is sent
    } else {
      Serial.println("Failed to send data over ESP-NOW");
    }
  }

}





void loop() {
  //reading values
  val_jaw = digitalRead(open_pin);
   val_cw = digitalRead(CW_BUTTON_PIN);
   val_acw = digitalRead(ACW_BUTTON_PIN);
  int val_x = analogRead(x);
  int val_y = analogRead(y);
  int val_z = analogRead(z);
  //int val_y2 = analogRead(y2);

  //mapping
  map_x = map(val_x, 0, 4095, -1000, 1000);
  map_y = map(val_y, 0, 4095, -1000, 1000);
  map_z = map(val_z, 0, 4095, -1000, 1000);
  //map_y2 = map(val_y2, 0, 4095, -1000, 1000);

  // observing state change
  change_x();
  change_y();
  change_z();
  change_jaw();
  change_rot();


  //casting, sending and getting feedback
  cast();


  lastxState = xState;
  lastyState = yState;
  lastzState = zState;
  lastjawState = jawState;
  lastrotState = rotState;
}
Robot:

Code: Select all

#include "AccelStepper.h"
#include <ESP32MX1508.h>
#include <ESP32Servo.h>
#include <esp_now.h>
#include <WiFi.h>
#include <Wire.h>
#include <Adafruit_ADS1X15.h>

// Create ADS1115 instance
Adafruit_ADS1115 ads;


//#define open_pin 12

// Yrotation driver pins
#define ROTA 12
#define ROTB 14
#define CH1 0
#define CH2 1
#define RES 12

MX1508 motorA(ROTA, ROTB, CH1, CH2, RES); //object for yrot motor


#define XSTEP   23
#define XDIR    22
#define YSTEP   33
#define YDIR    32
#define ZSTEP   26
#define ZDIR    25

#define DRIVER  1

AccelStepper stepperX(DRIVER, XSTEP, XDIR);
AccelStepper stepperY(DRIVER, YSTEP, YDIR);
AccelStepper stepperZ(DRIVER, ZSTEP, ZDIR);

int servoPin = 5;
Servo myservo;

int fsrValue = 0;



//STATES
#define FWD     2
#define REV     1
#define STOP    0
#define OPEN    0
#define CLOSE   1
#define CW      1
#define ACW     2



// REPLACE WITH YOUR RECEIVER MAC Address
uint8_t broadcastAddress[] = {0xEC, 0x62, 0x60, 0x9A, 0x62, 0x60};


typedef struct struct_message {
byte xData = STOP;
  byte yData = STOP;
  byte zData = STOP;
  bool jaw = CLOSE;
  byte rot = STOP;
  int fsr;
} struct_message;

struct_message xRX;
struct_message xTX;


byte lastxState = 0;
byte lastyState = 0;
byte lastzState = 0;
byte lastjawState = 0;
byte lastrotState = 0;


bool xmove = false;
bool ymove = false;
bool zmove = false;



void OnDataRecv(const uint8_t *mac_addr, const uint8_t *data, int data_len) {
  memcpy(&xRX, data, sizeof(xRX));

  if (lastxState != xRX.xData || lastyState != xRX.yData || lastzState != xRX.zData || lastjawState != xRX.jaw || lastrotState != xRX.rot) {

    // moving x
    if (xRX.xData == FWD) {
      stepperX.setSpeed(500);
      xmove = true;
    } else if (xRX.xData == REV) {
      stepperX.setSpeed(-500);
      xmove = true;
    } else if (xRX.xData == STOP) {
      xmove = false;
      stepperX.stop();
    }

    // moving y
    if (xRX.yData == FWD) {
      stepperY.setSpeed(500);
      ymove = true;
    } else if (xRX.yData == REV) {
      stepperY.setSpeed(-500);
      ymove = true;
    } else if (xRX.yData == STOP) {
      ymove = false;
      stepperY.stop();
    }


    //Moving Z
    if (xRX.zData == FWD) {
      stepperZ.setSpeed(500);
      zmove = true;
    } else if (xRX.zData == REV) {
      stepperZ.setSpeed(-500);
      zmove = true;
    } else if (xRX.zData == STOP) {
      zmove = false;
      stepperZ.stop();
    }

    // jaw
    if (xRX.jaw == OPEN) {
      myservo.write(0);
    }else if (xRX.jaw == CLOSE){
      myservo.write(180);
    }

    // rot
    if (xRX.rot == CW) {
      motorA.motorGo(4095);
    } else if (xRX.rot == ACW) {
       motorA.motorRev(4095);
    }
    else if (xRX.rot == STOP){
      motorA.motorBrake(); 
      }
  }

  lastxState = xRX.xData;
  lastyState = xRX.yData;
  lastzState = xRX.zData;
  lastjawState = xRX.jaw;
  lastrotState = xRX.rot;
}



void setup() {
  // put your setup code here, to run once:
  //Serial.begin(115200);

  WiFi.mode(WIFI_STA);
  if (esp_now_init() != ESP_OK) {
    Serial.println("Error initializing ESP-NOW");
    return;
  }

  esp_now_register_recv_cb(OnDataRecv);
  myservo.attach(servoPin, 500, 2400);

  stepperX.setMaxSpeed(1000);
  stepperX.setAcceleration(30);
  stepperX.setSpeed(50);
  stepperX.stop();

  stepperY.setMaxSpeed(1000);
  stepperY.setAcceleration(30);
  stepperY.setSpeed(50);
  stepperY.stop();

  stepperZ.setMaxSpeed(1000);
  stepperZ.setAcceleration(30);
  stepperZ.setSpeed(50);
  stepperZ.stop();

}

void loop() {
  // put your main code here, to run repeatedly:
  if (xmove) {
    stepperX.runSpeed();
  }

   if (ymove) {
    stepperY.runSpeed();
  }

   if (zmove) {
    stepperZ.runSpeed();
  }

  fsrValue = ads.readADC_SingleEnded(0);
  xTX.fsr = fsrValue;
  
   //sending
    esp_err_t result = esp_now_send(broadcastAddress, (uint8_t *) &xTX, sizeof(xTX));


      if (result == ESP_OK) {
     Serial.println("fsr value sent");
    } else {
      Serial.println("Failed to send data over ESP-NOW");
    }
}
also the esp32 at the robot is showing error:
Rebooting... ets Jul 29 2019 12:21:46
rst:Oxc (SW_CPU_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT) configsip: 0, SPIWP:Oxee clk_drv:Ox00,q_drv:Ox00,d_drv:Ox00,csO_drv:Ox00,hd_drv:Ox00,wp_drv:Ox00 mode:DIO, clock div:1 load:Ox3fff0030,1en:1344 load:0x40078000,1en:13864 load:0x40080400,1en:3608 entry 0x400805f0 Guru Meditation Error: Core 1 panic'ed (LoadProhibited). Exception was unhandled.
Core 1 register dump: PC : Ox400d4acf PS A2 : Ox00000000 A3 A6 : Ox00000000 A7 A10 : 0x40b75624 All A14 : Ox7ff00000 A15 EXCVADDR: Ox0000000c LBEG
: 0x00060b30 AO : Ox3ffc2ela A4 : Ox00000000 AS : 0x482a739d Al2 : 0x7ff4d555 SAR : Ox000000la : 0x400029ac LEND : 0x400029cb
: 0x800d49d8 Al : 0x00000003 A5 : 0x800d2b54 A9 : 0x00000006 A13
: 0x3ffb2780 : Ox00000001 : 0x3ffb2750 : Ox00000029
EXCCAUSE: Ox000000lc LCOUNT : Ox00000000
Backtrace:Ox400d4acc:0x3ffb27800x400d49d5:0x3ffb27a0 0x400d4a07:0x3ffb27c0 Ox400d4ab6:0x3ffb27e0 0x400d278c:Ox3ffb2800 Ox400d5ecd:0x3ffb2820
Attachments
Capture.PNG
Capture.PNG (41.66 KiB) Viewed 1091 times
Capture.PNG
Capture.PNG (41.66 KiB) Viewed 1091 times

ESP_Sprite
Posts: 9730
Joined: Thu Nov 26, 2015 4:08 am

Re: ESPNOW two way communication error

Postby ESP_Sprite » Fri May 26, 2023 12:27 pm

Suggest you decode that backtrace.

Who is online

Users browsing this forum: Bing [Bot] and 100 guests