un0038998 / ESPNOW_Multiple_Cars

This repository contains code and diagram for ESPNOW Multiple Cars
MIT License
3 stars 1 forks source link

Code error #1

Open vishnu2606 opened 4 hours ago

vishnu2606 commented 4 hours ago

include

include

//Right motor int enableRightMotor=22; int rightMotorPin1=16; int rightMotorPin2=17; //Left motor int enableLeftMotor=23; int leftMotorPin1=18; int leftMotorPin2=19;

define MAX_MOTOR_SPEED 200

const int PWMFreq = 1000; / 1 KHz / const int PWMResolution = 8; const int rightMotorPWMSpeedChannel = 4; const int leftMotorPWMSpeedChannel = 5;

define SIGNAL_TIMEOUT 1000 // This is signal timeout in milli seconds. We will reset the data if no signal

unsigned long lastRecvTime = 0;

struct PacketData { byte xAxisValue; byte yAxisValue; byte switchPressed; }; PacketData receiverData;

bool throttleAndSteeringMode = false;

// callback function that will be executed when data is received void OnDataRecv(const uint8_t mac, const uint8_t incomingData, int len) { if (len == 0) { return; } memcpy(&receiverData, incomingData, sizeof(receiverData)); String inputData ; inputData = inputData + "values " + receiverData.xAxisValue + " " + receiverData.yAxisValue + " " + receiverData.switchPressed; Serial.println(inputData); if (receiverData.switchPressed == true) { if (throttleAndSteeringMode == false) { throttleAndSteeringMode = true; } else { throttleAndSteeringMode = false; } }

if (throttleAndSteeringMode) { throttleAndSteeringMovements(); } else { simpleMovements(); }

lastRecvTime = millis();
}

void simpleMovements() { if (receiverData.yAxisValue <= 75) //Move car Forward { rotateMotor(MAX_MOTOR_SPEED, MAX_MOTOR_SPEED); } else if (receiverData.yAxisValue >= 175) //Move car Backward { rotateMotor(-MAX_MOTOR_SPEED, -MAX_MOTOR_SPEED); } else if (receiverData.xAxisValue >= 175) //Move car Right { rotateMotor(-MAX_MOTOR_SPEED, MAX_MOTOR_SPEED); } else if (receiverData.xAxisValue <= 75) //Move car Left { rotateMotor(MAX_MOTOR_SPEED, -MAX_MOTOR_SPEED); } else //Stop the car { rotateMotor(0, 0); }
}

void throttleAndSteeringMovements() { int throttle = map( receiverData.yAxisValue, 254, 0, -255, 255); int steering = map( receiverData.xAxisValue, 0, 254, -255, 255);
int motorDirection = 1;

if (throttle < 0) //Move car backward { motorDirection = -1;
}

int rightMotorSpeed, leftMotorSpeed; rightMotorSpeed = abs(throttle) - steering; leftMotorSpeed = abs(throttle) + steering; rightMotorSpeed = constrain(rightMotorSpeed, 0, 255); leftMotorSpeed = constrain(leftMotorSpeed, 0, 255);

rotateMotor(rightMotorSpeed motorDirection, leftMotorSpeed motorDirection); }

void rotateMotor(int rightMotorSpeed, int leftMotorSpeed) { if (rightMotorSpeed < 0) { digitalWrite(rightMotorPin1,LOW); digitalWrite(rightMotorPin2,HIGH);
} else if (rightMotorSpeed > 0) { digitalWrite(rightMotorPin1,HIGH); digitalWrite(rightMotorPin2,LOW);
} else { digitalWrite(rightMotorPin1,LOW); digitalWrite(rightMotorPin2,LOW);
}

if (leftMotorSpeed < 0) { digitalWrite(leftMotorPin1,LOW); digitalWrite(leftMotorPin2,HIGH);
} else if (leftMotorSpeed > 0) { digitalWrite(leftMotorPin1,HIGH); digitalWrite(leftMotorPin2,LOW);
} else { digitalWrite(leftMotorPin1,LOW); digitalWrite(leftMotorPin2,LOW);
}

ledcWrite(rightMotorPWMSpeedChannel, abs(rightMotorSpeed)); ledcWrite(leftMotorPWMSpeedChannel, abs(leftMotorSpeed));
}

void setUpPinModes() { pinMode(enableRightMotor,OUTPUT); pinMode(rightMotorPin1,OUTPUT); pinMode(rightMotorPin2,OUTPUT);

pinMode(enableLeftMotor,OUTPUT); pinMode(leftMotorPin1,OUTPUT); pinMode(leftMotorPin2,OUTPUT);

//Set up PWM for motor speed ledcSetup(rightMotorPWMSpeedChannel, PWMFreq, PWMResolution); ledcSetup(leftMotorPWMSpeedChannel, PWMFreq, PWMResolution);
ledcAttachPin(enableRightMotor, rightMotorPWMSpeedChannel); ledcAttachPin(enableLeftMotor, leftMotorPWMSpeedChannel);

rotateMotor(0, 0); }

void setup() { setUpPinModes();

Serial.begin(115200); WiFi.mode(WIFI_STA);

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

esp_now_register_recv_cb(OnDataRecv); }

void loop() { //Check Signal lost. unsigned long now = millis(); if ( now - lastRecvTime > SIGNAL_TIMEOUT ) { rotateMotor(0, 0); } }

When I run this code I suffer with error please solve this...

un0038998 commented 4 hours ago

What error do you get ?

Thanks And Regards,

Ujwal Nandanwar

On Tue, 22 Oct 2024 at 2:54 PM, vishnu2606 @.***> wrote:

include

include

//Right motor int enableRightMotor=22; int rightMotorPin1=16; int rightMotorPin2=17; //Left motor int enableLeftMotor=23; int leftMotorPin1=18; int leftMotorPin2=19;

define MAX_MOTOR_SPEED 200

const int PWMFreq = 1000; / 1 KHz / const int PWMResolution = 8; const int rightMotorPWMSpeedChannel = 4; const int leftMotorPWMSpeedChannel = 5;

define SIGNAL_TIMEOUT 1000 // This is signal timeout in milli seconds. We

will reset the data if no signal unsigned long lastRecvTime = 0;

struct PacketData { byte xAxisValue; byte yAxisValue; byte switchPressed; }; PacketData receiverData;

bool throttleAndSteeringMode = false;

// callback function that will be executed when data is received void OnDataRecv(const uint8_t mac, const uint8_t incomingData, int len) { if (len == 0) { return; } memcpy(&receiverData, incomingData, sizeof(receiverData)); String inputData ; inputData = inputData + "values " + receiverData.xAxisValue + " " + receiverData.yAxisValue + " " + receiverData.switchPressed; Serial.println(inputData); if (receiverData.switchPressed == true) { if (throttleAndSteeringMode == false) { throttleAndSteeringMode = true; } else { throttleAndSteeringMode = false; } }

if (throttleAndSteeringMode) { throttleAndSteeringMovements(); } else { simpleMovements(); }

lastRecvTime = millis(); }

void simpleMovements() { if (receiverData.yAxisValue <= 75) //Move car Forward { rotateMotor(MAX_MOTOR_SPEED, MAX_MOTOR_SPEED); } else if (receiverData.yAxisValue >= 175) //Move car Backward { rotateMotor(-MAX_MOTOR_SPEED, -MAX_MOTOR_SPEED); } else if (receiverData.xAxisValue >= 175) //Move car Right { rotateMotor(-MAX_MOTOR_SPEED, MAX_MOTOR_SPEED); } else if (receiverData.xAxisValue <= 75) //Move car Left { rotateMotor(MAX_MOTOR_SPEED, -MAX_MOTOR_SPEED); } else //Stop the car { rotateMotor(0, 0); } }

void throttleAndSteeringMovements() { int throttle = map( receiverData.yAxisValue, 254, 0, -255, 255); int steering = map( receiverData.xAxisValue, 0, 254, -255, 255); int motorDirection = 1;

if (throttle < 0) //Move car backward { motorDirection = -1; }

int rightMotorSpeed, leftMotorSpeed; rightMotorSpeed = abs(throttle) - steering; leftMotorSpeed = abs(throttle) + steering; rightMotorSpeed = constrain(rightMotorSpeed, 0, 255); leftMotorSpeed = constrain(leftMotorSpeed, 0, 255);

rotateMotor(rightMotorSpeed motorDirection, leftMotorSpeed motorDirection); }

void rotateMotor(int rightMotorSpeed, int leftMotorSpeed) { if (rightMotorSpeed < 0) { digitalWrite(rightMotorPin1,LOW); digitalWrite(rightMotorPin2,HIGH); } else if (rightMotorSpeed > 0) { digitalWrite(rightMotorPin1,HIGH); digitalWrite(rightMotorPin2,LOW); } else { digitalWrite(rightMotorPin1,LOW); digitalWrite(rightMotorPin2,LOW); }

if (leftMotorSpeed < 0) { digitalWrite(leftMotorPin1,LOW); digitalWrite(leftMotorPin2,HIGH); } else if (leftMotorSpeed > 0) { digitalWrite(leftMotorPin1,HIGH); digitalWrite(leftMotorPin2,LOW); } else { digitalWrite(leftMotorPin1,LOW); digitalWrite(leftMotorPin2,LOW); }

ledcWrite(rightMotorPWMSpeedChannel, abs(rightMotorSpeed)); ledcWrite(leftMotorPWMSpeedChannel, abs(leftMotorSpeed)); }

void setUpPinModes() { pinMode(enableRightMotor,OUTPUT); pinMode(rightMotorPin1,OUTPUT); pinMode(rightMotorPin2,OUTPUT);

pinMode(enableLeftMotor,OUTPUT); pinMode(leftMotorPin1,OUTPUT); pinMode(leftMotorPin2,OUTPUT);

//Set up PWM for motor speed ledcSetup(rightMotorPWMSpeedChannel, PWMFreq, PWMResolution); ledcSetup(leftMotorPWMSpeedChannel, PWMFreq, PWMResolution); ledcAttachPin(enableRightMotor, rightMotorPWMSpeedChannel); ledcAttachPin(enableLeftMotor, leftMotorPWMSpeedChannel);

rotateMotor(0, 0); }

void setup() { setUpPinModes();

Serial.begin(115200); WiFi.mode(WIFI_STA);

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

esp_now_register_recv_cb(OnDataRecv); }

void loop() { //Check Signal lost. unsigned long now = millis(); if ( now - lastRecvTime > SIGNAL_TIMEOUT ) { rotateMotor(0, 0); } }

When I run this code I suffer with error please solve this...

— Reply to this email directly, view it on GitHub https://github.com/un0038998/ESPNOW_Multiple_Cars/issues/1, or unsubscribe https://github.com/notifications/unsubscribe-auth/ASDSL5ZDF3YRBNJP6UE3YSDZ4YKU7AVCNFSM6AAAAABQMA2M7GVHI2DSMVQWIX3LMV43ASLTON2WKOZSGYYDIOBSHA2DMMY . You are receiving this because you are subscribed to this thread.Message ID: @.***>