Skip to content

Commit 21e8f99

Browse files
authored
✨ M282 - Detach Servo (#22760)
1 parent c2e4b16 commit 21e8f99

File tree

11 files changed

+76
-14
lines changed

11 files changed

+76
-14
lines changed

Marlin/Configuration.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2936,9 +2936,9 @@
29362936
* Set this manually if there are extra servos needing manual control.
29372937
* Set to 0 to turn off servo support.
29382938
*/
2939-
//#define NUM_SERVOS 3 // Servo index starts with 0 for M280 command
2939+
//#define NUM_SERVOS 3 // Note: Servo index starts with 0 for M280-M282 commands
29402940

2941-
// (ms) Delay before the next move will start, to give the servo time to reach its target angle.
2941+
// (ms) Delay before the next move will start, to give the servo time to reach its target angle.
29422942
// 300ms is a good value but you can try less delay.
29432943
// If the servo can't reach the requested position, increase it.
29442944
#define SERVO_DELAY { 300 }
@@ -2948,3 +2948,6 @@
29482948

29492949
// Edit servo angles with M281 and save to EEPROM with M500
29502950
//#define EDITABLE_SERVO_ANGLES
2951+
2952+
// Disable servo with M282 to reduce power consumption, noise, and heat when not in use
2953+
//#define SERVO_DETACH_GCODE

Marlin/src/gcode/control/M280.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ void GcodeSuite::M280() {
3939
if (parser.seen('S')) {
4040
const int a = parser.value_int();
4141
if (a == -1)
42-
servo[servo_index].detach();
42+
DETACH_SERVO(servo_index);
4343
else
4444
MOVE_SERVO(servo_index, a);
4545
}

Marlin/src/gcode/control/M282.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* Marlin 3D Printer Firmware
3+
* Copyright (c) 2021 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
4+
*
5+
* Based on Sprinter and grbl.
6+
* Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
7+
*
8+
* This program is free software: you can redistribute it and/or modify
9+
* it under the terms of the GNU General Public License as published by
10+
* the Free Software Foundation, either version 3 of the License, or
11+
* (at your option) any later version.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
* GNU General Public License for more details.
17+
*
18+
* You should have received a copy of the GNU General Public License
19+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
20+
*
21+
*/
22+
23+
#include "../../inc/MarlinConfig.h"
24+
25+
#if ENABLED(SERVO_DETACH_GCODE)
26+
27+
#include "../gcode.h"
28+
#include "../../module/servo.h"
29+
30+
/**
31+
* M282: Detach Servo. P<index>
32+
*/
33+
void GcodeSuite::M282() {
34+
35+
if (!parser.seen('P')) return;
36+
37+
const int servo_index = parser.value_int();
38+
if (WITHIN(servo_index, 0, NUM_SERVOS - 1))
39+
DETACH_SERVO(servo_index);
40+
else
41+
SERIAL_ECHO_MSG("Servo ", servo_index, " out of range");
42+
43+
}
44+
45+
#endif // SERVO_DETACH_GCODE

Marlin/src/gcode/gcode.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -717,6 +717,9 @@ void GcodeSuite::process_parsed_command(const bool no_ok/*=false*/) {
717717
#if ENABLED(EDITABLE_SERVO_ANGLES)
718718
case 281: M281(); break; // M281: Set servo angles
719719
#endif
720+
#if ENABLED(SERVO_DETACH_GCODE)
721+
case 282: M282(); break; // M282: Detach servo
722+
#endif
720723
#endif
721724

722725
#if ENABLED(BABYSTEPPING)

Marlin/src/gcode/gcode.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@
192192
* M261 - i2c Request Data (Requires EXPERIMENTAL_I2CBUS)
193193
* M280 - Set servo position absolute: "M280 P<index> S<angle|µs>". (Requires servos)
194194
* M281 - Set servo min|max position: "M281 P<index> L<min> U<max>". (Requires EDITABLE_SERVO_ANGLES)
195+
* M282 - Detach servo: "M282 P<index>". (Requires SERVO_DETACH_GCODE)
195196
* M290 - Babystepping (Requires BABYSTEPPING)
196197
* M300 - Play beep sound S<frequency Hz> P<duration ms>
197198
* M301 - Set PID parameters P I and D. (Requires PIDTEMP)
@@ -862,6 +863,9 @@ class GcodeSuite {
862863
static void M281();
863864
static void M281_report(const bool forReplay=true);
864865
#endif
866+
#if ENABLED(SERVO_DETACH_GCODE)
867+
static void M282();
868+
#endif
865869
#endif
866870

867871
#if ENABLED(BABYSTEPPING)

Marlin/src/inc/Conditionals_post.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2593,9 +2593,14 @@
25932593
#endif
25942594
#if NUM_SERVOS > 0
25952595
#define HAS_SERVOS 1
2596-
#endif
2597-
#if HAS_SERVOS && defined(PAUSE_SERVO_OUTPUT) && defined(RESUME_SERVO_OUTPUT)
2598-
#define HAS_PAUSE_SERVO_OUTPUT 1
2596+
#if defined(PAUSE_SERVO_OUTPUT) && defined(RESUME_SERVO_OUTPUT)
2597+
#define HAS_PAUSE_SERVO_OUTPUT 1
2598+
#endif
2599+
#else
2600+
#undef SERVO_DELAY
2601+
#undef DEACTIVATE_SERVOS_AFTER_MOVE
2602+
#undef EDITABLE_SERVO_ANGLES
2603+
#undef SERVO_DETACH_GCODE
25992604
#endif
26002605

26012606
// Sensors

Marlin/src/module/servo.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,19 +39,19 @@ HAL_SERVO_LIB servo[NUM_SERVOS];
3939
void servo_init() {
4040
#if NUM_SERVOS >= 1 && HAS_SERVO_0
4141
servo[0].attach(SERVO0_PIN);
42-
servo[0].detach(); // Just set up the pin. We don't have a position yet. Don't move to a random position.
42+
DETACH_SERVO(0); // Just set up the pin. We don't have a position yet. Don't move to a random position.
4343
#endif
4444
#if NUM_SERVOS >= 2 && HAS_SERVO_1
4545
servo[1].attach(SERVO1_PIN);
46-
servo[1].detach();
46+
DETACH_SERVO(1);
4747
#endif
4848
#if NUM_SERVOS >= 3 && HAS_SERVO_2
4949
servo[2].attach(SERVO2_PIN);
50-
servo[2].detach();
50+
DETACH_SERVO(2);
5151
#endif
5252
#if NUM_SERVOS >= 4 && HAS_SERVO_3
5353
servo[3].attach(SERVO3_PIN);
54-
servo[3].detach();
54+
DETACH_SERVO(3);
5555
#endif
5656
}
5757

Marlin/src/module/servo.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@
110110
#endif // HAS_SERVO_ANGLES
111111

112112
#define MOVE_SERVO(I, P) servo[I].move(P)
113+
#define DETACH_SERVO(I) servo[I].detach()
113114

114115
extern HAL_SERVO_LIB servo[NUM_SERVOS];
115116
void servo_init();

buildroot/tests/LPC1768

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ restore_configs
2828
opt_set MOTHERBOARD BOARD_MKS_SBASE \
2929
EXTRUDERS 2 TEMP_SENSOR_1 1 \
3030
NUM_SERVOS 2 SERVO_DELAY '{ 300, 300 }'
31-
opt_enable SWITCHING_NOZZLE SWITCHING_NOZZLE_E1_SERVO_NR ULTIMAKERCONTROLLER REALTIME_REPORTING_COMMANDS FULL_REPORT_TO_HOST_FEATURE
31+
opt_enable SWITCHING_NOZZLE SWITCHING_NOZZLE_E1_SERVO_NR EDITABLE_SERVO_ANGLES SERVO_DETACH_GCODE \
32+
ULTIMAKERCONTROLLER REALTIME_REPORTING_COMMANDS FULL_REPORT_TO_HOST_FEATURE
3233
exec_test $1 $2 "MKS SBASE with SWITCHING_NOZZLE, Grbl Realtime Report" "$3"
3334

3435
restore_configs

ini/features.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ HAS_SMART_EFF_MOD = src_filter=+<src/gcode/config/M672.cpp>
173173
COOLANT_CONTROL|AIR_ASSIST = src_filter=+<src/gcode/control/M7-M9.cpp>
174174
AIR_EVACUATION = src_filter=+<src/gcode/control/M10-M11.cpp>
175175
HAS_SOFTWARE_ENDSTOPS = src_filter=+<src/gcode/control/M211.cpp>
176+
SERVO_DETACH_GCODE = src_filter=+<src/gcode/control/M282.cpp>
176177
HAS_DUPLICATION_MODE = src_filter=+<src/gcode/control/M605.cpp>
177178
LIN_ADVANCE = src_filter=+<src/gcode/feature/advance>
178179
PHOTO_GCODE = src_filter=+<src/gcode/feature/camera>

0 commit comments

Comments
 (0)