Open pchiang5 opened 5 months ago
I managed to achieve this by modifying the MM_vLStep_Z.ino
as follows. Now, I can add this RAMPS1.4/Arduino Mega setup to Micro-Manager through the Hardware Configuration Wizard
and control the z-axis movement via Stage Control
. However, several issues remain:
2024-06-24T18:01:43.371442 tid8272 [dbg,dev:COM6] GetAnswer <- 112.8\r 2024-06-24T18:01:43.371458 tid8272 [IFO,dev:ZAxis] ZStage::GetPositionUm() 112.8 2024-06-24T18:01:43.409350 tid8272 [dbg,Core] Will start relative move of ZAxis by offset 1.10000 um 2024-06-24T18:01:43.409367 tid8272 [dbg,dev:COM6] SetCommand -> !dim z 1\r 2024-06-24T18:01:43.409371 tid18036 [dbg,App] [UI] JButton clicked in StageControlFrame. 2024-06-24T18:01:43.409375 tid8272 [dbg,dev:COM6] SetCommand -> !mor z 1.1\r 2024-06-24T18:01:43.409380 tid8272 [dbg,dev:COM6] SetCommand -> ?err\r 2024-06-24T18:01:43.463036 tid8272 [dbg,dev:COM6] GetAnswer <- 0\r 2024-06-24T18:01:43.463055 tid8272 [dbg,Core] Waiting for device ZAxis... 2024-06-24T18:01:43.463063 tid8272 [dbg,dev:COM6] SetCommand -> ?statusaxis\r 2024-06-24T18:01:43.510520 tid8272 [dbg,dev:COM6] GetAnswer <- @\r 2024-06-24T18:01:43.510531 tid8272 [dbg,Core] Finished waiting for device ZAxis 2024-06-24T18:01:43.510546 tid8272 [dbg,dev:COM6] SetCommand -> !dim z 1\r 2024-06-24T18:01:43.510551 tid8272 [dbg,dev:COM6] SetCommand -> ?pos z\r 2024-06-24T18:01:43.555610 tid8272 [dbg,dev:COM6] GetAnswer <- 112.8\r 2024-06-24T18:01:43.555628 tid8272 [IFO,dev:ZAxis] ZStage::GetPositionUm() 112.8 2024-06-24T18:01:43.577342 tid8272 [dbg,Core] Will start relative move of ZAxis by offset 1.10000 um 2024-06-24T18:01:43.577355 tid8272 [dbg,dev:COM6] SetCommand -> !dim z 1\r 2024-06-24T18:01:43.577361 tid18036 [dbg,App] [UI] JButton clicked in StageControlFrame. 2024-06-24T18:01:43.577363 tid8272 [dbg,dev:COM6] SetCommand -> !mor z 1.1\r 2024-06-24T18:01:43.577367 tid8272 [dbg,dev:COM6] SetCommand -> ?err\r 2024-06-24T18:01:43.632655 tid8272 [dbg,dev:COM6] GetAnswer <- 0\r 2024-06-24T18:01:43.632673 tid8272 [dbg,Core] Waiting for device ZAxis... 2024-06-24T18:01:43.632683 tid8272 [dbg,dev:COM6] SetCommand -> ?statusaxis\r 2024-06-24T18:01:43.678655 tid8272 [dbg,dev:COM6] GetAnswer <- @\r 2024-06-24T18:01:43.678664 tid8272 [dbg,Core] Finished waiting for device ZAxis 2024-06-24T18:01:43.678687 tid8272 [dbg,dev:COM6] SetCommand -> !dim z 1\r 2024-06-24T18:01:43.678692 tid8272 [dbg,dev:COM6] SetCommand -> ?pos z\r 2024-06-24T18:01:43.723782 tid8272 [dbg,dev:COM6] GetAnswer <- 112.8\r 2024-06-24T18:01:43.723798 tid8272 [IFO,dev:ZAxis] ZStage::GetPositionUm() 112.8 2024-06-24T18:01:43.753332 tid8272 [dbg,Core] Will start relative move of ZAxis by offset 1.10000 um 2024-06-24T18:01:43.753348 tid8272 [dbg,dev:COM6] SetCommand -> !dim z 1\r 2024-06-24T18:01:43.753351 tid18036 [dbg,App] [UI] JButton clicked in StageControlFrame. 2024-06-24T18:01:43.753357 tid8272 [dbg,dev:COM6] SetCommand -> !mor z 1.1\r 2024-06-24T18:01:43.753361 tid8272 [dbg,dev:COM6] SetCommand -> ?err\r 2024-06-24T18:01:43.816045 tid8272 [dbg,dev:COM6] GetAnswer <- 0\r 2024-06-24T18:01:43.816063 tid8272 [dbg,Core] Waiting for device ZAxis... 2024-06-24T18:01:43.816074 tid8272 [dbg,dev:COM6] SetCommand -> ?statusaxis\r 2024-06-24T18:01:43.862167 tid8272 [dbg,dev:COM6] GetAnswer <- @\r 2024-06-24T18:01:43.862176 tid8272 [dbg,Core] Finished waiting for device ZAxis 2024-06-24T18:01:43.862191 tid8272 [dbg,dev:COM6] SetCommand -> !dim z 1\r 2024-06-24T18:01:43.862197 tid8272 [dbg,dev:COM6] SetCommand -> ?pos z\r 2024-06-24T18:01:43.908262 tid8272 [dbg,dev:COM6] GetAnswer <- 112.8\r 2024-06-24T18:01:43.908278 tid8272 [IFO,dev:ZAxis] ZStage::GetPositionUm() 112.8
Go to
for long-distance movement.2024-06-24T18:05:35.921479 tid18036 [dbg,App] [UI] JButton "Mark" clicked in MMPositionListDlg. 2024-06-24T18:05:37.425562 tid18036 [dbg,App] [UI] clicked in MMPositionListDlg. 2024-06-24T18:05:38.097630 tid18036 [dbg,App] [UI] clicked in MMPositionListDlg. 2024-06-24T18:05:39.641661 tid18036 [dbg,Core] Will start absolute move of ZAxis to position 427.29999 um 2024-06-24T18:05:39.641681 tid18036 [IFO,dev:ZAxis] ZStage::SetPositionUm() 427.3 2024-06-24T18:05:39.641697 tid18036 [dbg,dev:COM6] SetCommand -> !dim z 1\r 2024-06-24T18:05:39.641706 tid18036 [dbg,dev:COM6] SetCommand -> !moa z 427.3\r 2024-06-24T18:05:39.641712 tid18036 [dbg,dev:COM6] SetCommand -> ?err\r 2024-06-24T18:05:40.142462 tid18036 [IFO,dev:COM6] TERM_TIMEOUT error occured! 2024-06-24T18:05:40.142490 tid18036 [ERR,Core] Error occurred in device COM6: Error in device "COM6": (Error message unavailable) (107) 2024-06-24T18:05:40.142542 tid18036 [ERR,Core] Error occurred in device ZAxis: Error in device "ZAxis": Serial command failed. Is the device connected to the serial port? (14)
digitalPinToInterrupt
in my firmware? 2024-06-24T18:10:25.763577 tid18036 [dbg,dev:COM6] SetCommand -> !dim z 1\r 2024-06-24T18:10:25.763592 tid18036 [dbg,dev:COM6] SetCommand -> ?pos z\r 2024-06-24T18:10:25.763599 tid18036 [dbg,dev:COM6] GetAnswer <- 0\r 2024-06-24T18:10:25.763624 tid18036 [IFO,dev:ZAxis] ZStage::GetPositionUm() 0 2024-06-24T18:10:25.764259 tid18036 [dbg,dev:COM6] SetCommand -> !dim z 1\r 2024-06-24T18:10:25.764266 tid18036 [dbg,dev:COM6] SetCommand -> ?pos z\r 2024-06-24T18:10:25.842758 tid18036 [dbg,dev:COM6] GetAnswer <- -347.5\r 2024-06-24T18:10:25.842794 tid18036 [IFO,dev:ZAxis] ZStage::GetPositionUm() -347.5 2024-06-24T18:10:25.843745 tid18036 [dbg,dev:COM6] SetCommand -> !dim z 1\r 2024-06-24T18:10:25.843753 tid18036 [dbg,dev:COM6] SetCommand -> ?pos z\r 2024-06-24T18:10:25.843758 tid18036 [dbg,dev:COM6] GetAnswer <- -347.5\r 2024-06-24T18:10:25.843769 tid18036 [IFO,dev:ZAxis] ZStage::GetPositionUm() -347.5 2024-06-24T18:10:25.844456 tid18036 [dbg,dev:COM6] SetCommand -> !dim z 1\r 2024-06-24T18:10:25.844463 tid18036 [dbg,dev:COM6] SetCommand -> ?pos z\r 2024-06-24T18:10:25.920864 tid18036 [dbg,dev:COM6] GetAnswer <- -347.5\r 2024-06-24T18:10:25.920895 tid18036 [IFO,dev:ZAxis] ZStage::GetPositionUm() -347.5 2024-06-24T18:10:25.921542 tid18036 [dbg,dev:COM6] SetCommand -> !dim z 1\r 2024-06-24T18:10:25.921549 tid18036 [dbg,dev:COM6] SetCommand -> ?pos z\r 2024-06-24T18:10:25.921553 tid18036 [dbg,dev:COM6] GetAnswer <- -347.5\r 2024-06-24T18:10:25.921564 tid18036 [IFO,dev:ZAxis] ZStage::GetPositionUm() -347.5 2024-06-24T18:10:25.922094 tid18036 [dbg,dev:COM6] SetCommand -> !dim z 1\r 2024-06-24T18:10:25.922100 tid18036 [dbg,dev:COM6] SetCommand -> ?pos z\r 2024-06-24T18:10:25.998989 tid18036 [dbg,dev:COM6] GetAnswer <- -347.5\r 2024-06-24T18:10:25.999018 tid18036 [IFO,dev:ZAxis] ZStage::GetPositionUm() -347.5 2024-06-24T18:10:26.010733 tid18036 [dbg,App] [UI] JButton clicked in MainFrame. 2024-06-24T18:10:27.145892 tid18036 [dbg,App] [UI] JButton clicked in StageControlFrame. 2024-06-24T18:10:27.145900 tid8272 [dbg,Core] Will start relative move of ZAxis by offset -11.10000 um 2024-06-24T18:10:27.145921 tid8272 [dbg,dev:COM6] SetCommand -> !dim z 1\r 2024-06-24T18:10:27.145932 tid8272 [dbg,dev:COM6] SetCommand -> !mor z -11.1\r 2024-06-24T18:10:27.145937 tid8272 [dbg,dev:COM6] SetCommand -> ?err\r 2024-06-24T18:10:27.145942 tid8272 [dbg,dev:COM6] GetAnswer <- -347.5\r 2024-06-24T18:10:27.145965 tid8272 [ERR,Core] Error occurred in device ZAxis: Error in device "ZAxis": Unexpected response from serial port. Is the device connected to the correct serial port? (16)
Thanks!
#include <Arduino.h>
#include <RotaryEncoder.h>
#define Z_STEP_PIN 46
#define Z_DIR_PIN 48
#define Z_ENABLE_PIN 62
#define Z_MIN_PIN 18
#define Z_MAX_PIN 19
#define PIN_IN1 20
#define PIN_IN2 21
#define LED_PIN 13
#define FAN_PIN 9
RotaryEncoder *encoder = nullptr;
void checkPosition()
{
encoder->tick();
}
String cmd = "" ;
//23910
void setup() {
Serial.begin(9600);
pinMode(LED_PIN , OUTPUT);
pinMode(Z_STEP_PIN , OUTPUT);
pinMode(Z_DIR_PIN , OUTPUT);
pinMode(Z_ENABLE_PIN , OUTPUT);
digitalWrite(Z_ENABLE_PIN , HIGH);
pinMode(PIN_IN1, INPUT);
pinMode(PIN_IN2, INPUT);
Serial.println("InterruptRotator example for the RotaryEncoder library.");
encoder = new RotaryEncoder(PIN_IN1, PIN_IN2, RotaryEncoder::LatchMode::TWO03);
attachInterrupt(digitalPinToInterrupt(PIN_IN1), checkPosition, CHANGE);
attachInterrupt(digitalPinToInterrupt(PIN_IN2), checkPosition, CHANGE);
}
char c = '*';
void loop()
{
if (Serial.available()) {
cmd = Serial.readStringUntil('\r');
processCommand(cmd);
cmd = "";
}
}
float z = 0.0000;
float tz = 0.0000;
void processCommand(String s) {
if (s.startsWith("?ver")) {
reply ("Vers:LS");
} else if (s.startsWith("!autostatus 0")) {
delay(5);
} else if (s.startsWith("?det")) {
reply ("60");
} else if (s.startsWith("?pitch z")) {
reply ("0.6096");
} else if (s.startsWith("?vel z")) {
reply ("100.0");
} else if (s.startsWith("?accel z")) {
reply ("1.0");
} else if (s.startsWith("!dim z 1")) {
delay(5);
} else if (s.startsWith("!dim z 2")) {
delay(5);
} else if (s.startsWith("?statusaxis")) {
reply ("@");
} else if (s.startsWith("!vel z")) {
delay(5);
} else if (s.startsWith("!accel z")) {
delay(5);
} else if (s.startsWith("?pos z")) {
String zs = String(z, 1);
reply (zs);
} else if (s.startsWith("?lim z")) {
reply ("0.0 100.0");
} else if (s.startsWith("!pos z")) {
delay(5);
} else if (s.startsWith("?status")) {
reply ("OK...");
} else if (s.startsWith("!dim z 0")) {
delay(5);
} else if (s.startsWith("!speed z")) {
delay(5);
} else if (s.startsWith("!mor z")) {
// position();
String delta = s.substring(s.indexOf("z") + 1);
tz = z + delta.toFloat();
moveToTargetPosition();
} else if (s.startsWith("!moa z")) {
// position();
String apos = s.substring(s.indexOf("z") + 1);
tz = apos.toFloat();
moveToTargetPosition();
} else if (s.startsWith("?err")) {
reply ("0");
}
}
void reply(String s) {
Serial.print(s);
Serial.print("\r");
}
void moveToTargetPosition() {
float tolerance = 2;
// Check if the target position is different from the current position
if (abs(tz - z) > tolerance) {
// Set the direction based on whether the target position is higher or lower than the current position
int direction = (tz > z) ? LOW : HIGH;
digitalWrite(Z_DIR_PIN, direction); // Set the direction
digitalWrite(Z_ENABLE_PIN, LOW); // Enable the motor driver
// position();
// Continue sending step pulses until the target position is reached
while (abs(tz - z) > tolerance) {
digitalWrite(Z_STEP_PIN, HIGH); // Step signal on
delayMicroseconds(500); // Adjust delay as needed for step pulse duration
digitalWrite(Z_STEP_PIN, LOW); // Step signal off
delayMicroseconds(500); // Adjust delay as needed for step pulse duration
// position();
// Update the current position from the encoder
encoder->tick();
z = encoder->getPosition()*0.6096;
}
// Once the target position is reached, disable the motor driver
digitalWrite(Z_ENABLE_PIN, HIGH);
} else {
// If already at the target position, just disable the motor driver
digitalWrite(Z_ENABLE_PIN, HIGH);
}
}
// Add delay or other code as needed
static int lastPos = 0;
void position()
{
encoder->tick();
int newPos = encoder->getPosition();
if (lastPos != newPos)
{
Serial.print("pos:");
Serial.print(newPos);
Serial.print(" dir:");
Serial.println((int)(encoder->getDirection()));
lastPos = newPos;
}
}
Thanks for the update. I'm not familiar enough with the RAMPS device adapter to have much insight, but you might try increasing the timeout on COM6 (it's settable in the Hardware Configuration Wizard, or the .cfg file). You probably have it at the default 500 ms; try setting it to be longer than the longest time it takes for the stage travel.
Thank you for the great tool.
I have a z-axis stepper and a rotary encoder controllable using an Arduino Mega with Marlin firmware. However, when I tried using the Hardware Configuration Wizard, it showed the following error message:
Because this firmware was tested and found to be working by inputting 'M114' in the serial terminal, with the output shown below. I wonder if the error is caused by the
\n
in theSetCommand -> M114\r
? If it is the case, how to avoid the inclusion of this line terminal?Besides, this stepper can be controlled with the Arduino script below, which provides real-time position reports from a rotary encoder:
A little tweak of the
Arduino-Hub
sketch could control the stepper movement and report the position below. Is it possible to interface theStage Control
with this repurposedArduino-Hub
to control stages like the native z-stage control function in μManager? Thanks again.