Skip to content

Commit 01aa028

Browse files
MitchBradleybdring
andauthored
Implement stepping through Motors class (#636)
* Implement stepping through Motors class WIP for discussion and review - not ready to merge yet * Document Motor methods and variables .. and remove some unused ones and move some that are subclass-specific * Move position_min/max to Limits.cpp ... and coalesced other uses thereof into a unified scheme. * Call motor ->init() explicitly instead of implicitly This makes it possible to inherit constructors without spurious config messages. * Fixed problems with I2S * Changes in class method override syntax per atlaste * Fixed oops * More Motors simplification a) Eliminated can_home() in favor of a return value from set_homing_mode() b) Eliminated axis_name() in favor of reportAxisNameMsg() * Fixes to RcServo and Trinamic - RC Servo was not handling disable ... probably old issue - Display test after config * More tweaks * Define that variable! * Move functions from Motors.cpp to subclasses Created a Servo base class from which RcServo and Dynamixel2 are derived. This gets the servo update task out of Motors. It also eliminates the need for type_id. Now all of the functions that are specific to particular kinds of motors are within their subclasses * Adding Dynamixel to ABC axes. * Removed second #ifndef SPINDLE_TYPE * Fixed potential leak in Report.cpp as reported by @atlaste * Some servo cleanup. Has errors! * min should be max * Removed test rcservo machine definition. * Removed obsolete #defines in machine defs for RcServo cal Co-authored-by: bdring <barton.dring@gmail.com>
1 parent d9dba2c commit 01aa028

39 files changed

+882
-1055
lines changed

Grbl_Esp32/src/Grbl.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
// Grbl versioning system
2424

2525
const char* const GRBL_VERSION = "1.3a";
26-
const char* const GRBL_VERSION_BUILD = "20201004";
26+
const char* const GRBL_VERSION_BUILD = "20201015";
2727

2828
//#include <sdkconfig.h>
2929
#include <Arduino.h>
@@ -87,9 +87,7 @@ const char* const GRBL_VERSION_BUILD = "20201004";
8787
# endif
8888
#endif
8989

90-
#ifdef USE_I2S_OUT
91-
# include "I2SOut.h"
92-
#endif
90+
#include "I2SOut.h"
9391

9492
void grbl_init();
9593
void run_once();

Grbl_Esp32/src/I2SOut.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,6 @@
4242
*/
4343
#include "Config.h"
4444

45-
#ifdef USE_I2S_OUT
46-
4745
# include <FreeRTOS.h>
4846
# include <driver/periph_ctrl.h>
4947
# include <rom/lldesc.h>
@@ -566,12 +564,14 @@ uint8_t IRAM_ATTR i2s_out_read(uint8_t pin) {
566564
return (!!(port_data & bit(pin)));
567565
}
568566

569-
uint32_t IRAM_ATTR i2s_out_push_sample(uint32_t num) {
567+
uint32_t IRAM_ATTR i2s_out_push_sample(uint32_t usec) {
568+
uint32_t num = usec/I2S_OUT_USEC_PER_PULSE;
569+
570570
# ifdef USE_I2S_OUT_STREAM_IMPL
571571
if (num > SAMPLE_SAFE_COUNT) {
572572
return 0;
573573
}
574-
// push at least one sample (even if num is zero)
574+
// push at least one sample, even if num is zero)
575575
uint32_t port_data = atomic_load(&i2s_out_port_data);
576576
uint32_t n = 0;
577577
do {
@@ -960,5 +960,3 @@ int IRAM_ATTR i2s_out_init() {
960960
};
961961
return i2s_out_init(default_param);
962962
}
963-
964-
#endif

Grbl_Esp32/src/I2SOut.h

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,6 @@
4141
// It should be included at the outset to know the machine configuration.
4242
#include "Config.h"
4343

44-
#ifdef USE_I2S_OUT
45-
4644
# include <stdint.h>
4745

4846
/* Assert */
@@ -126,12 +124,14 @@ void i2s_out_write(uint8_t pin, uint8_t val);
126124
/*
127125
Set current pin state to the I2S bitstream buffer
128126
(This call will generate a future I2S_OUT_USEC_PER_PULSE μs x N bitstream)
129-
num: Number of samples to be generated
127+
usec: The length of time that the pulse should be repeated.
128+
That time will be converted to an integer number of pulses of
129+
length I2S_OUT_USEC_PER_PULSE.
130130
The number of samples is limited to (20 / I2S_OUT_USEC_PER_PULSE).
131-
return: number of puhsed samples
131+
return: number of pushed samples
132132
0 .. no space for push
133133
*/
134-
uint32_t i2s_out_push_sample(uint32_t num);
134+
uint32_t i2s_out_push_sample(uint32_t usec);
135135

136136
/*
137137
Set pulser mode to passtrough
@@ -184,8 +184,6 @@ i2s_out_pulser_status_t IRAM_ATTR i2s_out_get_pulser_status();
184184
*/
185185
int i2s_out_reset();
186186

187-
#endif
188-
189187
/*
190188
Reference: "ESP32 Technical Reference Manual" by Espressif Systems
191189
https://www.espressif.com/sites/default/files/documentation/esp32_technical_reference_manual_en.pdf

Grbl_Esp32/src/Jog.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ Error jog_execute(plan_line_data_t* pl_data, parser_block_t* gc_block) {
3333
pl_data->line_number = gc_block->values.n;
3434
#endif
3535
if (soft_limits->get()) {
36-
if (system_check_travel_limits(gc_block->values.xyz)) {
36+
if (limitsCheckTravel(gc_block->values.xyz)) {
3737
return Error::TravelExceeded;
3838
}
3939
}

Grbl_Esp32/src/Limits.cpp

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -79,18 +79,12 @@ void limits_go_home(uint8_t cycle_mask) {
7979
return; // Block if system reset has been issued.
8080
}
8181
// Initialize plan data struct for homing motion. Spindle and coolant are disabled.
82-
motors_set_homing_mode(cycle_mask, true); // tell motors homing is about to start
8382

84-
// Remove any motor that cannot be homed from the mask
85-
// Motors with non standard homing can do that during motors_set_homing_mode(...) above
86-
auto n_axis = number_axis->get();
87-
for (uint8_t idx = 0; idx < n_axis; idx++) {
88-
if (bit_istrue(cycle_mask, bit(idx))) {
89-
if (!motor_can_home(idx)) {
90-
bit_false(cycle_mask, bit(idx));
91-
}
92-
}
93-
}
83+
// Put motors on axes listed in cycle_mask in homing mode and
84+
// replace cycle_mask with the list of motors that are ready for homing.
85+
// Motors with non standard homing can home during motors_set_homing_mode(...)
86+
cycle_mask = motors_set_homing_mode(cycle_mask, true); // tell motors homing is about to start
87+
9488
// see if any motors are left
9589
if (cycle_mask == 0) {
9690
return;
@@ -111,6 +105,7 @@ void limits_go_home(uint8_t cycle_mask) {
111105
float target[MAX_N_AXIS];
112106
float max_travel = 0.0;
113107

108+
auto n_axis = number_axis->get();
114109
for (uint8_t idx = 0; idx < n_axis; idx++) {
115110
// Initialize step pin masks
116111
step_pin[idx] = bit(idx);
@@ -337,9 +332,8 @@ void limits_init() {
337332
if (limit_sw_queue == NULL) {
338333
grbl_msg_sendf(CLIENT_SERIAL,
339334
MsgLevel::Info,
340-
"%c%s Axis limit switch on pin %s",
341-
report_get_axis_letter(axis),
342-
gang_index ? "2" : " ",
335+
"%s limit switch on pin %s",
336+
reportAxisNameMsg(axis, gang_index),
343337
pinName(pin).c_str());
344338
}
345339
}
@@ -376,7 +370,7 @@ void limits_disable() {
376370
// number in bit position, i.e. Z_AXIS is bit(2), and Y_AXIS is bit(1).
377371
AxisMask limits_get_state() {
378372
AxisMask pinMask = 0;
379-
auto n_axis = number_axis->get();
373+
auto n_axis = number_axis->get();
380374
for (int axis = 0; axis < n_axis; axis++) {
381375
for (int gang_index = 0; gang_index < 2; gang_index++) {
382376
uint8_t pin = limit_pins[axis][gang_index];
@@ -399,7 +393,7 @@ AxisMask limits_get_state() {
399393
// the workspace volume is in all negative space, and the system is in normal operation.
400394
// NOTE: Used by jogging to limit travel within soft-limit volume.
401395
void limits_soft_check(float* target) {
402-
if (system_check_travel_limits(target)) {
396+
if (limitsCheckTravel(target)) {
403397
sys.soft_limit = true;
404398
// Force feed hold if cycle is active. All buffered blocks are guaranteed to be within
405399
// workspace volume so just come to a controlled stop so position is not lost. When complete
@@ -435,3 +429,30 @@ void limitCheckTask(void* pvParameters) {
435429
}
436430
}
437431
}
432+
433+
float limitsMaxPosition(uint8_t axis) {
434+
float mpos = axis_settings[axis]->home_mpos->get();
435+
436+
return bitnum_istrue(homing_dir_mask->get(), axis) ? mpos + axis_settings[axis]->max_travel->get() : mpos;
437+
}
438+
439+
float limitsMinPosition(uint8_t axis) {
440+
float mpos = axis_settings[axis]->home_mpos->get();
441+
442+
return bitnum_istrue(homing_dir_mask->get(), axis) ? mpos : mpos - axis_settings[axis]->max_travel->get();
443+
}
444+
445+
// Checks and reports if target array exceeds machine travel limits.
446+
// Return true if exceeding limits
447+
bool limitsCheckTravel(float* target) {
448+
uint8_t idx;
449+
auto n_axis = number_axis->get();
450+
for (idx = 0; idx < n_axis; idx++) {
451+
float max_mpos, min_mpos;
452+
453+
if (target[idx] < limitsMinPosition(idx) || target[idx] > limitsMaxPosition(idx)) {
454+
return true;
455+
}
456+
}
457+
return false;
458+
}

Grbl_Esp32/src/Limits.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,9 @@ void isr_limit_switches();
4848

4949
// A task that runs after a limit switch interrupt.
5050
void limitCheckTask(void* pvParameters);
51+
52+
float limitsMaxPosition(uint8_t axis);
53+
float limitsMinPosition(uint8_t axis);
54+
55+
// Internal factor used by limits_soft_check
56+
bool limitsCheckTravel(float* target);

Grbl_Esp32/src/MachineCommon.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
#pragma once
22

3-
#ifndef SPINDLE_TYPE
4-
# define SPINDLE_TYPE SpindleType::PWM
5-
#endif
6-
73
// Grbl setting that are common to all machines
84
// It should not be necessary to change anything herein
95

Grbl_Esp32/src/Machines/midtbot.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,6 @@
4545
#define Y_LIMIT_PIN GPIO_NUM_4
4646

4747
#define Z_SERVO_PIN GPIO_NUM_27
48-
#define Z_SERVO_CAL_MIN 1.0 // calibration factor for the minimum PWM duty
49-
#define Z_SERVO_CAL_MAX 1.0 // calibration factor for the maximum PWM duty
50-
5148

5249
// Set $Homing/Cycle0=Y and $Homing/Cycle1=X
5350

Grbl_Esp32/src/Machines/pen_laser.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,6 @@
5555

5656
#ifdef USING_SERVO
5757
#define Z_SERVO_PIN GPIO_NUM_27
58-
#define Z_SERVO_CAL_MIN 1.0 // calibration factor for the minimum PWM duty
59-
#define Z_SERVO_CAL_MAX 1.0 // calibration factor for the maximum PWM duty
6058
#endif
6159

6260
#define SPINDLE_TYPE SpindleType::NONE

Grbl_Esp32/src/Machines/polar_coaster.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,6 @@
4949
#define STEPPERS_DISABLE_PIN GPIO_NUM_17
5050

5151
#define Z_SERVO_PIN GPIO_NUM_16
52-
#define Z_SERVO_CAL_MIN 1.0 // calibration factor for the minimum PWM duty
53-
#define Z_SERVO_CAL_MAX 1.0 // calibration factor for the maximum PWM duty
5452

5553
#define X_LIMIT_PIN GPIO_NUM_4
5654

0 commit comments

Comments
 (0)