Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updates for remote #1

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
165 changes: 108 additions & 57 deletions Cumpump/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,17 @@ uint8_t M5_Remote_Address[] = {0x08, 0x3A, 0xF2, 0x68, 0x1E, 0x74};
ESP_FlexyStepper stepper;

// Command States
#define CUMCONN 20
#define CUMTOGGLE 21
#define CUMTIME 22
#define CUMSIZE 23
#define CUMACCEL 24
#define CUMREVERSE 25

#define CUMSPEED 20
#define CUMTIME 21
#define CUMSIZE 22
#define CUMACCEL 23
#define M5_ID 99
#define EJECT_ID 69


unsigned long previousMillis = 0;
long interval = 1000;
#define HEARTBEAT 99

///////////////////////////////////////////
////
Expand All @@ -43,10 +45,18 @@ long interval = 1000;
#define LogDebugFormatted(...) ((void)0)
#endif

float cum_time = 0.0;
float cum_speed = 1000.0;
float cum_size = 0.0;
float cum_accel = 0.0;
float cum_time = 1.0;
float cum_size = 1.0;
float cum_accel = 1.0;
bool continuous = false;
bool ejecting = false;
bool reverse = false;

float steps_per_second = 800;

// max rpm = 500
// 6400 steps per revolution
float MAX_STEPS_PER_SECOND = 500 * 6400 / 60;

// Variable to store if sending data was successful
String success;
Expand All @@ -67,6 +77,7 @@ typedef struct struct_message {
bool esp_connected;
int esp_command;
float esp_value;
int esp_target;
} struct_message;

bool esp_connect = false;
Expand All @@ -80,7 +91,7 @@ esp_now_peer_info_t peerInfo;
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");
if (status ==0){
if (status == ESP_NOW_SEND_SUCCESS){
success = "Delivery Success :)";
}
else{
Expand All @@ -92,85 +103,125 @@ void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) {
memcpy(&incomingcontrol, incomingData, sizeof(incomingcontrol));

LogDebug(incomingcontrol.esp_command);
LogDebug(incomingcontrol.esp_value);

switch(incomingcontrol.esp_command)
{
case CUMSPEED:
case CUMTOGGLE:
{
cum_speed = incomingcontrol.esp_value;
if (incomingcontrol.esp_value == 0) {
stepper.setTargetPositionToStop();
ejecting = false;
} else {
ejecting = true;
}
LogDebug("ejecting: " + String(ejecting));
}
break;
case CUMTIME:
{
interval = incomingcontrol.esp_value * 1000;
cum_time = incomingcontrol.esp_value;
steps_per_second = min(MAX_STEPS_PER_SECOND, cum_size / cum_time);
LogDebug("cum_time: " + String(cum_time));
}
break;
case CUMSIZE:
{
cum_size = incomingcontrol.esp_value * 1500;
cum_size = incomingcontrol.esp_value * 800; // 800 steps per 1/8 revolution
if (cum_size / cum_time > MAX_STEPS_PER_SECOND) {
cum_size = MAX_STEPS_PER_SECOND * cum_time;
}
steps_per_second = min(MAX_STEPS_PER_SECOND, cum_size / cum_time);
LogDebug("cum_size: " + String(cum_size));
}
break;
case CUMACCEL:
{
cum_accel = incomingcontrol.esp_value * 10000;
cum_accel = incomingcontrol.esp_value * 800;
LogDebug("cum_accel: " + String(cum_accel));
}
break;
case CUMREVERSE:
{
reverse = incomingcontrol.esp_value == 1;
LogDebug("reverse: " + String(reverse));
}
break;
case HEARTBEAT:
{
LogDebug("Heartbeat");
}
break;
}
}

bool SendCommand(int Command, float Value){
outgoingcontrol.esp_connected = true;
outgoingcontrol.esp_command = Command;
outgoingcontrol.esp_value = Value;
outgoingcontrol.esp_target = M5_ID;
esp_err_t result = esp_now_send(M5_Remote_Address, (uint8_t *) &outgoingcontrol, sizeof(outgoingcontrol));

if (result == ESP_OK) {
return true;
}
else {
delay(20);
esp_err_t result = esp_now_send(M5_Remote_Address, (uint8_t *) &outgoingcontrol, sizeof(outgoingcontrol));
return false;
}
}

void setup()
{
Serial.begin(115200);
WiFi.mode(WIFI_STA);
LogDebug(WiFi.macAddress());
// Init ESP-NOW
if (esp_now_init() != ESP_OK) {
Serial.begin(115200);
WiFi.mode(WIFI_STA);
LogDebug(WiFi.macAddress());

// 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, M5_Remote_Address, 6);
peerInfo.channel = 0;
peerInfo.encrypt = false;

// Add peer
if (esp_now_add_peer(&peerInfo) != ESP_OK){
}

// 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, M5_Remote_Address, 6);
peerInfo.channel = 0;
peerInfo.encrypt = false;

// Add peer
if (esp_now_add_peer(&peerInfo) != ESP_OK){
Serial.println("Failed to add peer");
return;
}
// Register for a callback function that will be called when data is received
esp_now_register_recv_cb(OnDataRecv);
}
// Register for a callback function that will be called when data is received
esp_now_register_recv_cb(OnDataRecv);

digitalWrite(MOTOR_ENA_PIN, LOW);
stepper.connectToPins(MOTOR_STEP_PIN, MOTOR_DIRECTION_PIN);

// connect and configure the stepper motor to its IO pins
pinMode(MOTOR_ENA_PIN, OUTPUT);
digitalWrite(MOTOR_ENA_PIN, LOW);
stepper.connectToPins(MOTOR_STEP_PIN, MOTOR_DIRECTION_PIN);
SendCommand(CUMCONN, EJECT_ID);
}

void loop()
{
stepper.setSpeedInStepsPerSecond(cum_speed);
stepper.setSpeedInStepsPerSecond(steps_per_second);
stepper.setAccelerationInStepsPerSecondPerSecond(cum_accel);
stepper.setDecelerationInStepsPerSecondPerSecond(cum_accel);
stepper.setDecelerationInStepsPerSecondPerSecond(continuous ? 0 : cum_accel);

unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval)
{
if (cum_size > 0 && ejecting) {
digitalWrite(MOTOR_ENA_PIN, LOW);
previousMillis = currentMillis;
stepper.moveRelativeInSteps(cum_size);
}
if (stepper.getDistanceToTargetSigned() == 0){
digitalWrite(MOTOR_ENA_PIN, HIGH);
stepper.moveRelativeInSteps(reverse ? -cum_size : cum_size);

if (!continuous) {
SendCommand(CUMTOGGLE, EJECT_ID);
ejecting = false;
}
} else {
digitalWrite(MOTOR_ENA_PIN, HIGH);
}

}

delay((continuous && ejecting) ? 100 : 500);
}