16extern FLASH flash_memory;
17extern SCPI* scpi_instrument;
21void Identify(SCPI_C commands, SCPI_P parameters, Stream& interface) {
22 static char response[100];
23 snprintf(response,
sizeof(response),
"%s, %s, V%s, #%s", DEVICE_MANUFACTURER, DEVICE_NAME, DEVICE_VERSION, DEVICE_SERIAL);
24 interface.print(response);
25 interface.write(
'\n');
28void Reset(SCPI_C commands, SCPI_P parameters, Stream& interface) {
32void OPC(SCPI_C commands, SCPI_P parameters, Stream& interface) {
34 interface.write(
'\n');
37void GetError(SCPI_C commands, SCPI_P parameters, Stream& interface) {
38 const char* response = scpi_instrument->getError();
39 interface.print(response);
40 interface.write(
'\n');
43void GetHelp(SCPI_C commands, SCPI_P parameters, Stream& interface) {
44 scpi_instrument->printAllHeaders(interface);
47void GetHelpSyntax(SCPI_C commands, SCPI_P parameters, Stream& interface) {
48 scpi_instrument->printHeaderSyntax(interface, parameters[0]);
51void GetVersion(SCPI_C commands, SCPI_P parameters, Stream& interface) {
52 static char response[10];
53 snprintf(response,
sizeof(response),
"V%s", DEVICE_VERSION);
54 interface.print(response);
55 interface.write(
'\n');
58void GetDebug(SCPI_C commands, SCPI_P parameters, Stream& interface) {
59 scpi_instrument->PrintDebugInfo(interface);
60 interface.write(
'\n');
63void Preset(SCPI_C commands, SCPI_P parameters, Stream& interface) {
65 flash_memory.preferences.clear();
69void SetPort(SCPI_C commands, SCPI_P parameters, Stream& interface) {
71 String portString = String(parameters[0]);
72 int port = portString.toInt();
75 if (port < 0 || port > 65535) {
76 scpi_instrument->logError(220,
"Invalid port number");
80 flash_memory.preferences.putInt(
"SCPI-PORT", port);
83void GetPort(SCPI_C commands, SCPI_P parameters, Stream& interface) {
85 int port = scpi_instrument->getPort();
86 interface.print(port);
87 interface.write(
'\n');
90void SetIP(SCPI_C commands, SCPI_P parameters, Stream &interface) {
92 String ipString = String(parameters[0]);
96 if (!ip.fromString(ipString)) {
97 scpi_instrument->logError(220,
"Invalid IP address");
101 flash_memory.preferences.putString(
"IP-ADDRESS", ipString);
105void GetIP(SCPI_C commands, SCPI_P parameters, Stream &interface) {
106 IPAddress myAddress = Ethernet.localIP();
107 interface.print(myAddress);
108 interface.write(
'\n');
111void SetMAC(SCPI_C commands, SCPI_P parameters, Stream &interface) {
113 String macString = String(parameters[0]);
115 sscanf(macString.c_str(),
"%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", &myAddress[0], &myAddress[1], &myAddress[2], &myAddress[3], &myAddress[4], &myAddress[5]);
118 flash_memory.preferences.putBytes(
"MAC-ADDRESS", myAddress,
sizeof(myAddress));
121void GetMAC(SCPI_C commands, SCPI_P parameters, Stream &interface) {
123 Ethernet.MACAddress(myAddress);
124 for (
byte octet = 0; octet < 6; octet++) {
125 interface.print(myAddress[octet], HEX);
127 interface.print(
':');
130 interface.write(
'\n');
133void GetGW(SCPI_C commands, SCPI_P parameters, Stream &interface) {
134 IPAddress myGateWay = Ethernet.gatewayIP();
135 interface.print(myGateWay);
136 interface.write(
'\n');
139void Perpetual(SCPI_C commands, SCPI_P parameters, Stream &interface) {
148void Step(SCPI_C commands, SCPI_P parameters, Stream &interface) {
150 String header = String(commands.Last());
151 header.toUpperCase();
152 interface.print(header);
153 interface.write(
'\n');
155 sscanf(header.c_str(),
"%*[STEP]%u", &suffix);
158 if (suffix != 1 && suffix != 2) {
159 scpi_instrument->logError(131,
"Invalid Suffix");
164 for (
int i = 0; i < 3200; i++) {
168 interface.print(F(
"STEP1"));
169 interface.write(
'\n');
173 for (
int i = 0; i < 19200; i++) {
180 interface.print(F(
"STEP2"));
181 interface.write(
'\n');
185void SetRelativePosition(SCPI_C commands, SCPI_P parameters, Stream &interface) {
187 String positionString = String(parameters[0]);
188 int position = positionString.toInt();
191 if (position < 0 || position > 360) {
192 scpi_instrument->logError(220,
"Invalid position value");
197 String header = String(commands.Last());
198 header.toUpperCase();
200 sscanf(header.c_str(),
"%*[RELATIVE]%u", &suffix);
203 if (suffix != 1 && suffix != 2) {
204 char errorMessage[30];
205 sprintf(errorMessage,
"Invalid Suffix: %d", suffix);
206 scpi_instrument->logError(131, errorMessage);
211 int speed = flash_memory.preferences.getInt(
"SPEED", 1500);
212 int acceleration = flash_memory.preferences.getInt(
"ACCELERATION", 500);
215 stepper1->move(speed, acceleration, position, (STEPS_PER_REV * AXIS1_GEAR_RATIO * MICROSTEPS));
219 stepper2->move(speed, acceleration, position, (STEPS_PER_REV * AXIS2_GEAR_RATIO * MICROSTEPS), stepper1, 6);
223void GetAbsolutePosition(SCPI_C commands, SCPI_P parameters, Stream &interface) {
225 String header = String(commands.Last());
226 header.toUpperCase();
228 sscanf(header.c_str(),
"%*[ABSOLUTE]%u", &suffix);
231 if (suffix != 1 && suffix != 2) {
232 scpi_instrument->logError(131,
"Invalid Suffix");
234 interface.print(F(
"NaN"));
235 interface.write(
'\n');
240 float position = stepper1->getCurrentPosition();
242 interface.print(position);
243 interface.write(
'\n');
247 float position = stepper2->getCurrentPosition();
249 interface.print(position);
250 interface.write(
'\n');
254void SetAbsolutePosition(SCPI_C commands, SCPI_P parameters, Stream &interface) {
256 String positionString = String(parameters[0]);
257 int position = positionString.toInt();
260 if (position < 0 || position > 360) {
261 scpi_instrument->logError(220,
"Invalid position value");
266 String header = String(commands.Last());
267 header.toUpperCase();
269 sscanf(header.c_str(),
"%*[ABSOLUTE]%u", &suffix);
272 if (suffix != 1 && suffix != 2) {
273 char errorMessage[30];
274 sprintf(errorMessage,
"Invalid Suffix: %d", suffix);
275 scpi_instrument->logError(131, errorMessage);
279 int speed = flash_memory.preferences.getInt(
"SPEED", 1500);
280 int acceleration = flash_memory.preferences.getInt(
"ACCELERATION", 500);
283 stepper1->absoluteMove(speed, acceleration, position, (STEPS_PER_REV * AXIS1_GEAR_RATIO * MICROSTEPS));
287 stepper2->absoluteMove(speed, acceleration, position, (STEPS_PER_REV * AXIS2_GEAR_RATIO * MICROSTEPS), stepper1, 6);
291void SetDirection(SCPI_C commands, SCPI_P parameters, Stream &interface) {
293 String header = String(commands.Last());
294 header.toUpperCase();
296 sscanf(header.c_str(),
"%*[DIRECTION]%u", &suffix);
299 if (suffix != 1 && suffix != 2) {
300 char errorMessage[30];
301 sprintf(errorMessage,
"Invalid Suffix: %d", suffix);
302 scpi_instrument->logError(131, errorMessage);
307 if (String(parameters[0]) ==
"CW") {
308 stepper1->SetDirection(
false);
310 else if (String(parameters[0]) ==
"CCW") {
311 stepper1->SetDirection(
true);
314 char errorMessage[30];
315 sprintf(errorMessage,
"Illegal parameter value: %s", String(parameters[0]));
316 scpi_instrument->logError(224, errorMessage);
320 String param = String(parameters[0]);
321 if (param ==
"CW" || param ==
"CCW") {
322 bool newDir = (param ==
"CCW");
323 bool oldDir = stepper2->GetDirection();
324 stepper2->SetDirection(newDir);
326 if (newDir != oldDir) {
328 float cal_factor = flash_memory.preferences.getFloat(
"AXIS2-DIR-CHANGE-CAL-FACTOR", AXIS2_DIR_CHANGE_CAL_FACTOR);
329 int cal_steps = (int)(cal_factor * STEPS_PER_REV * AXIS2_GEAR_RATIO * MICROSTEPS / 360.0);
330 for (
int i = 0; i < cal_steps; i++) {
332 delayMicroseconds(200);
337 char errorMessage[30];
338 sprintf(errorMessage,
"Illegal parameter value: %s", String(parameters[0]));
339 scpi_instrument->logError(224, errorMessage);
344void GetDirection(SCPI_C commands, SCPI_P parameters, Stream &interface) {
346 String header = String(commands.Last());
347 header.toUpperCase();
349 sscanf(header.c_str(),
"%*[DIRECTION]%u", &suffix);
352 if (suffix != 1 && suffix != 2) {
353 char errorMessage[30];
354 sprintf(errorMessage,
"Invalid Suffix: %d", suffix);
355 scpi_instrument->logError(131, errorMessage);
357 interface.print(F(
"ERR"));
358 interface.write(
'\n');
363 if (stepper1->GetDirection()) {
364 interface.print(F(
"CCW"));
365 interface.write(
'\n');
368 interface.print(F(
"CW"));
369 interface.write(
'\n');
374 if (stepper2->GetDirection()) {
375 interface.print(F(
"CCW"));
376 interface.write(
'\n');
379 interface.print(F(
"CW"));
380 interface.write(
'\n');
385void SetRMSCurrent(SCPI_C commands, SCPI_P parameters, Stream &interface) {
387 String rmsString = String(parameters[0]);
388 int rms = rmsString.toInt();
391 if (rms < 0 || rms > 2400) {
392 scpi_instrument->logError(220,
"Invalid RMS current value");
396 flash_memory.preferences.putInt(
"RMS-CURRENT", rms);
399 stepper1->updateRMSCurrent();
400 stepper2->updateRMSCurrent();
403void GetRMSCurrent(SCPI_C commands, SCPI_P parameters, Stream &interface) {
405 interface.print(stepper1->getRMSCurrent());
406 interface.write(
'\n');
409void SetHoldCurrent(SCPI_C commands, SCPI_P parameters, Stream &interface) {
411 String holdString = String(parameters[0]);
412 int hold = holdString.toInt();
415 if (hold < 0 || hold > 32) {
416 scpi_instrument->logError(220,
"Invalid hold current value");
420 flash_memory.preferences.putInt(
"HOLD-CURRENT", hold);
423 stepper1->updateHoldCurrent();
424 stepper2->updateHoldCurrent();
427void GetHoldCurrent(SCPI_C commands, SCPI_P parameters, Stream &interface) {
429 interface.print(stepper1->getHoldCurrent());
430 interface.write(
'\n');
433void SetMicrosteps(SCPI_C commands, SCPI_P parameters, Stream &interface) {
435 String microstepsString = String(parameters[0]);
436 int microsteps = microstepsString.toInt();
439 if (microsteps < 1 || microsteps > 256) {
440 scpi_instrument->logError(220,
"Invalid microsteps value");
444 flash_memory.preferences.putInt(
"MICROSTEPS", microsteps);
447 stepper1->updateMicrosteps();
448 stepper2->updateMicrosteps();
451void GetMicrosteps(SCPI_C commands, SCPI_P parameters, Stream &interface) {
453 int microsteps = flash_memory.preferences.getInt(
"MICROSTEPS", MICROSTEPS);
454 interface.print(microsteps);
455 interface.write(
'\n');
458void SetSpeed(SCPI_C commands, SCPI_P parameters, Stream &interface) {
460 String speedString = String(parameters[0]);
461 int speed = speedString.toInt();
464 if (speed < 0 || speed > 100000) {
465 scpi_instrument->logError(220,
"Invalid speed value");
469 flash_memory.preferences.putInt(
"SPEED", speed);
472void GetSpeed(SCPI_C commands, SCPI_P parameters, Stream &interface) {
474 int speed = flash_memory.preferences.getInt(
"SPEED", SPEED);
475 interface.print(speed);
476 interface.write(
'\n');
479void SetAcceleration(SCPI_C commands, SCPI_P parameters, Stream &interface) {
481 String accelerationString = String(parameters[0]);
482 int acceleration = accelerationString.toInt();
485 if (acceleration < 0 || acceleration > 10000) {
486 scpi_instrument->logError(220,
"Invalid acceleration value");
490 flash_memory.preferences.putInt(
"ACCELERATION", acceleration);
493void GetAcceleration(SCPI_C commands, SCPI_P parameters, Stream &interface) {
495 int acceleration = flash_memory.preferences.getInt(
"ACCELERATION", ACCELERATION);
496 interface.print(acceleration);
497 interface.write(
'\n');
500void DebugMotorPins(SCPI_C commands, SCPI_P parameters, Stream &interface) {
501 int step_pin1 = flash_memory.preferences.getInt(
"MOTOR1-STEP-PIN", MOTOR1_STEP_PIN);
502 int enable_pin1 = flash_memory.preferences.getInt(
"MOTOR1-ENABLE-PIN", MOTOR1_ENABLE_PIN);
503 int step_pin2 = flash_memory.preferences.getInt(
"MOTOR2-STEP-PIN", MOTOR2_STEP_PIN);
504 int enable_pin2 = flash_memory.preferences.getInt(
"MOTOR2-ENABLE-PIN", MOTOR2_ENABLE_PIN);
505 interface.print(
"Motor 1 Step Pin: "); interface.print(step_pin1); interface.write(
'\n');
506 interface.print(
"Motor 1 Enable Pin: "); interface.print(enable_pin1); interface.write(
'\n');
507 interface.print(
"Motor 2 Step Pin: "); interface.print(step_pin2); interface.write(
'\n');
508 interface.print(
"Motor 2 Enable Pin: "); interface.print(enable_pin2); interface.write(
'\n');
511void GetMotorDebug(SCPI_C commands, SCPI_P parameters, Stream &interface) {
512 MOTOR_SERIAL_PORT.flush();
515 int status1 = stepper1->Debug();
518 int status2 = stepper2->Debug();
520 interface.print(
"Motor 1 Status: "); interface.print(status1, BIN); interface.write(
'\n');
521 interface.print(
"Motor 2 Status: "); interface.print(status2, BIN); interface.write(
'\n');
524void ResetEthernet(SCPI_C commands, SCPI_P parameters, Stream &interface) {
525 int ethernet_reset_pin = flash_memory.preferences.getInt(
"ETHERNET-RESET-PIN", ETHERNET_RESET_PIN);
528 digitalWrite(ethernet_reset_pin, LOW);
530 digitalWrite(ethernet_reset_pin, HIGH);
534 scpi_instrument->reinitEthernet();
536 interface.print(ethernet_reset_pin);
537 interface.print(
" Ethernet module reset");
538 interface.write(
'\n');
542 scpi_instrument->printSessionList(interface);
546 scpi_instrument->printSessionLog(interface);
550 scpi_instrument->printSocketList(interface);
554 scpi_instrument->printSystemDiagnostics(interface);
This class handles access to the flash memory of the ESP32. It is also used to initialize the flash m...
This class handles the SCPI communication with the instrument. It is responsible for parsing SCPI com...
The STEPPER class is used to manage stepper motor instances. It provides methods to control the motor...
Handles the flash memory for the ESP32.
This file contains the functions and classes related to SCPI communication.
void GetHelpSyntax(SCPI_C commands, SCPI_P parameters, Stream &interface)
Sends syntax help for a single command header, given as a parameter (e.g. SYSTem:HELP:SYNTax?...
void GetSocketList(SCPI_C commands, SCPI_P parameters, Stream &interface)
Sends the raw state of all W5500 hardware sockets over the given interface.
void GetSessionLog(SCPI_C commands, SCPI_P parameters, Stream &interface)
Sends the ring buffer of recent SCPI session closures (with reason) over the given interface.
void GetDebug(SCPI_C commands, SCPI_P parameters, Stream &interface)
Sends the debug information from Vrekrer-SCPI-Parser over the given interface.
void Preset(SCPI_C commands, SCPI_P parameters, Stream &interface)
Rewrites the parameters stored in flash memory with the defaults from the config.h file.
void OPC(SCPI_C commands, SCPI_P parameters, Stream &interface)
Sends a 1 over the given interface to indicate that the previous command has finished executing.
void Identify(SCPI_C commands, SCPI_P parameters, Stream &interface)
Sends an identification message over the given interface.
void GetSessionList(SCPI_C commands, SCPI_P parameters, Stream &interface)
Sends the live SCPI Ethernet session table over the given interface.
void GetVersion(SCPI_C commands, SCPI_P parameters, Stream &interface)
Sends the current firmware version of the device over the given interface.
void GetDiagSystem(SCPI_C commands, SCPI_P parameters, Stream &interface)
Sends system/reset health diagnostics (uptime, heap, reset reason, boot count) over the given interfa...
void GetHelp(SCPI_C commands, SCPI_P parameters, Stream &interface)
Sends a help message with all available SCPI commands over the given interface.
void Reset(SCPI_C commands, SCPI_P parameters, Stream &interface)
Triggers a full reset of the ESP32.
void GetError(SCPI_C commands, SCPI_P parameters, Stream &interface)
Sends the oldest error message over the given interface.