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

Add TCode Support #19

Draft
wants to merge 8 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
84 changes: 84 additions & 0 deletions PlatformIO ESP32 code/OSSM_ESP32/src/OSSMTCode.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#include "OSSMTCode.h"

#include <ESP_FlexyStepper.h> // Current Motion Control
#include <WiFiUdp.h>

#include "OSSM_Config.h"
#include "TCode.cpp"

#define FIRMWARE_ID "OSSMTCode.cpp" // Device and firmware version
#define TCODE_VER "TCode v0.3" // Current version of TCode

#define TCODE_PORT 6969

OSSMTCode::OSSMTCode(ESP_FlexyStepper &stepper, float maxStrokeLengthMm) : stepper(stepper), tcode(FIRMWARE_ID, TCODE_VER)
{
tcode.RegisterAxis("L0", "Up");
this->maxStrokeLengthMm = maxStrokeLengthMm;

// Acceleration calculation/motion planning is supposed to be handled by the TCode generator.
stepper.setAccelerationInMillimetersPerSecondPerSecond(maxSpeedMmPerSecond * 100.0);
stepper.setDecelerationInMillimetersPerSecondPerSecond(maxSpeedMmPerSecond * 100.0);
stepper.setSpeedInMillimetersPerSecond(maxSpeedMmPerSecond);
wifiUdp.begin(TCODE_PORT);

xTaskCreatePinnedToCore(OSSMTCode::inputTask, /* Task function. */
"TCodeInputTask", /* String with name of task (by default max 16 characters long) */
2000, /* Stack size in bytes. */
this, /* Parameter passed as input of the task */
1, /* Priority of the task, 1 seems to work just fine for us */
&this->xInputTaskHandle, /* Task handle. */
0);
};

#define TCODE_PRECISION 4
#define TCODE_KEEPOUT 5

void OSSMTCode::loop()
{
while ((stepper.getDistanceToTargetSigned() != 0))
{
vTaskDelay(5);
LogDebugFormatted("%d,%f,%ld,%f,%f,wait\n", xLin, stepper.getCurrentPositionInMillimeters(),
stepper.getDistanceToTargetSigned(), stepper.getTargetPositionInMillimeters(),
stepper.getCurrentVelocityInMillimetersPerSecond());
}

xLin = tcode.AxisRead("L0");
auto targetMm = map(xLin, 0, 9999, (0 + TCODE_KEEPOUT) * TCODE_PRECISION,
(maxStrokeLengthMm - TCODE_KEEPOUT) * TCODE_PRECISION) /
float(TCODE_PRECISION);
stepper.setTargetPositionInMillimeters(targetMm);
LogDebugFormatted("%d,%f,%ld,%f,%f,command\n", xLin, stepper.getCurrentPositionInMillimeters(),
stepper.getDistanceToTargetSigned(), stepper.getTargetPositionInMillimeters(),
stepper.getCurrentVelocityInMillimetersPerSecond());
};

void OSSMTCode::inputTask(void *parameter)
{
char packetBuffer[255];
OSSMTCode *tRef = static_cast<OSSMTCode *>(parameter);
for (;;)
{
int packetSize = tRef->wifiUdp.parsePacket();
if (packetSize)
{
int len = tRef->wifiUdp.read(packetBuffer, 255);
if (len > 0)
{
packetBuffer[len] = 0;
}
for (int i = 0; i < len; i++)
{
tRef->tcode.ByteInput(packetBuffer[i]);
}
}
// Read serial and send to tcode class
while (Serial.available() > 0)
{
// Send the serial bytes to the t-code object
tRef->tcode.ByteInput(Serial.read());
}
vTaskDelay(100);
}
}
29 changes: 29 additions & 0 deletions PlatformIO ESP32 code/OSSM_ESP32/src/OSSMTCode.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#pragma once
#include <Arduino.h>
#include <ESP_FlexyStepper.h> // Current Motion Control
#include <WiFiUdp.h>

#include "OSSM_Config.h"
#include "TCode.cpp"

class OSSMTCode
{
int xLin;
ESP_FlexyStepper& stepper;
TCode tcode;

float maxSpeedMmPerSecond = hardcode_maxSpeedMmPerSecond;
float maxStrokeLengthMm = hardcode_maxStrokeLengthMm;

WiFiUDP wifiUdp;
bool udpInitialized = false;

TaskHandle_t xInputTaskHandle = NULL;

public:
OSSMTCode(ESP_FlexyStepper& stepper, float maxStrokeLengthMm);
void loop();

private:
void static inputTask(void* parameter);
};
3 changes: 2 additions & 1 deletion PlatformIO ESP32 code/OSSM_ESP32/src/OSSM_Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@

#define SW_VERSION "0.21"
#define HW_VERSION 21 //divide by 10 for real hw version
#define EEPROM_SIZE 200
// EEPROM layout is 28 bytes of ossm config and 320 bytes of TCode axis config
#define EEPROM_SIZE 28+320

//#define INITIAL_SETUP //should only be defined at initial burn to configure HW version

Expand Down
4 changes: 4 additions & 0 deletions PlatformIO ESP32 code/OSSM_ESP32/src/OSSM_Main.ino
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,10 @@ void motionCommandTask(void *pvParameters)
case ossm.strokeEngineMode:
ossm.runStrokeEngine();
break;

case ossm.tCodeMode:
ossm.runTCode();
break;
}
}
}
Expand Down
Loading