Skip to content

Commit

Permalink
Merge branch 'Klipper3d:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
besser authored Jan 23, 2023
2 parents 2f71d2b + 6026a99 commit 8c2e155
Show file tree
Hide file tree
Showing 21 changed files with 239 additions and 340 deletions.
14 changes: 11 additions & 3 deletions docs/G-Codes.md
Original file line number Diff line number Diff line change
Expand Up @@ -811,9 +811,17 @@ The following command is available when an
enabled.

#### SET_PIN
`SET_PIN PIN=config_name VALUE=<value> CYCLE_TIME=<cycle_time>`:
Note - hardware PWM does not currently support the CYCLE_TIME
parameter and will use the cycle time defined in the config.
`SET_PIN PIN=config_name VALUE=<value> [CYCLE_TIME=<cycle_time>]`: Set
the pin to the given output `VALUE`. VALUE should be 0 or 1 for
"digital" output pins. For PWM pins, set to a value between 0.0 and
1.0, or between 0.0 and `scale` if a scale is configured in the
output_pin config section.

Some pins (currently only "soft PWM" pins) support setting an explicit
cycle time using the CYCLE_TIME parameter (specified in seconds). Note
that the CYCLE_TIME parameter is not stored between SET_PIN commands
(any SET_PIN command without an explicit CYCLE_TIME parameter will use
the `cycle_time` specified in the output_pin config section).

### [palette2]

Expand Down
4 changes: 2 additions & 2 deletions docs/RPi_microcontroller.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ must run before the klippy process.
After installing Klipper, install the script. run:
```
cd ~/klipper/
sudo cp "./scripts/klipper-mcu-start.sh" /etc/init.d/klipper_mcu
sudo update-rc.d klipper_mcu defaults
sudo cp ./scripts/klipper-mcu.service /etc/systemd/system/
sudo systemctl enable klipper-mcu.service
```

## Building the micro-controller code
Expand Down
4 changes: 2 additions & 2 deletions klippy/chelper/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,13 @@
double x_r, y_r, z_r;
};
struct trapq *trapq_alloc(void);
void trapq_free(struct trapq *tq);
void trapq_append(struct trapq *tq, double print_time
, double accel_t, double cruise_t, double decel_t
, double start_pos_x, double start_pos_y, double start_pos_z
, double axes_r_x, double axes_r_y, double axes_r_z
, double start_v, double cruise_v, double accel);
struct trapq *trapq_alloc(void);
void trapq_free(struct trapq *tq);
void trapq_finalize_moves(struct trapq *tq, double print_time);
void trapq_set_position(struct trapq *tq, double print_time
, double pos_x, double pos_y, double pos_z);
Expand Down
1 change: 1 addition & 0 deletions klippy/chelper/serialqueue.c
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,7 @@ kick_event(struct serialqueue *sq, double eventtime)
pollreactor_update_timer(sq->pr, SQPT_COMMAND, PR_NOW);
}

// OS write of data to be sent to the mcu
static void
do_write(struct serialqueue *sq, void *buf, int buflen)
{
Expand Down
96 changes: 48 additions & 48 deletions klippy/chelper/trapq.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,54 +20,6 @@ move_alloc(void)
return m;
}

// Fill and add a move to the trapezoid velocity queue
void __visible
trapq_append(struct trapq *tq, double print_time
, double accel_t, double cruise_t, double decel_t
, double start_pos_x, double start_pos_y, double start_pos_z
, double axes_r_x, double axes_r_y, double axes_r_z
, double start_v, double cruise_v, double accel)
{
struct coord start_pos = { .x=start_pos_x, .y=start_pos_y, .z=start_pos_z };
struct coord axes_r = { .x=axes_r_x, .y=axes_r_y, .z=axes_r_z };
if (accel_t) {
struct move *m = move_alloc();
m->print_time = print_time;
m->move_t = accel_t;
m->start_v = start_v;
m->half_accel = .5 * accel;
m->start_pos = start_pos;
m->axes_r = axes_r;
trapq_add_move(tq, m);

print_time += accel_t;
start_pos = move_get_coord(m, accel_t);
}
if (cruise_t) {
struct move *m = move_alloc();
m->print_time = print_time;
m->move_t = cruise_t;
m->start_v = cruise_v;
m->half_accel = 0.;
m->start_pos = start_pos;
m->axes_r = axes_r;
trapq_add_move(tq, m);

print_time += cruise_t;
start_pos = move_get_coord(m, cruise_t);
}
if (decel_t) {
struct move *m = move_alloc();
m->print_time = print_time;
m->move_t = decel_t;
m->start_v = cruise_v;
m->half_accel = -.5 * accel;
m->start_pos = start_pos;
m->axes_r = axes_r;
trapq_add_move(tq, m);
}
}

// Return the distance moved given a time in a move
inline double
move_get_distance(struct move *m, double move_time)
Expand Down Expand Up @@ -163,6 +115,54 @@ trapq_add_move(struct trapq *tq, struct move *m)
tail_sentinel->print_time = 0.;
}

// Fill and add a move to the trapezoid velocity queue
void __visible
trapq_append(struct trapq *tq, double print_time
, double accel_t, double cruise_t, double decel_t
, double start_pos_x, double start_pos_y, double start_pos_z
, double axes_r_x, double axes_r_y, double axes_r_z
, double start_v, double cruise_v, double accel)
{
struct coord start_pos = { .x=start_pos_x, .y=start_pos_y, .z=start_pos_z };
struct coord axes_r = { .x=axes_r_x, .y=axes_r_y, .z=axes_r_z };
if (accel_t) {
struct move *m = move_alloc();
m->print_time = print_time;
m->move_t = accel_t;
m->start_v = start_v;
m->half_accel = .5 * accel;
m->start_pos = start_pos;
m->axes_r = axes_r;
trapq_add_move(tq, m);

print_time += accel_t;
start_pos = move_get_coord(m, accel_t);
}
if (cruise_t) {
struct move *m = move_alloc();
m->print_time = print_time;
m->move_t = cruise_t;
m->start_v = cruise_v;
m->half_accel = 0.;
m->start_pos = start_pos;
m->axes_r = axes_r;
trapq_add_move(tq, m);

print_time += cruise_t;
start_pos = move_get_coord(m, cruise_t);
}
if (decel_t) {
struct move *m = move_alloc();
m->print_time = print_time;
m->move_t = decel_t;
m->start_v = cruise_v;
m->half_accel = -.5 * accel;
m->start_pos = start_pos;
m->axes_r = axes_r;
trapq_add_move(tq, m);
}
}

#define HISTORY_EXPIRE (30.0)

// Expire any moves older than `print_time` from the trapezoid velocity queue
Expand Down
10 changes: 5 additions & 5 deletions klippy/chelper/trapq.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,17 @@ struct pull_move {
};

struct move *move_alloc(void);
void trapq_append(struct trapq *tq, double print_time
, double accel_t, double cruise_t, double decel_t
, double start_pos_x, double start_pos_y, double start_pos_z
, double axes_r_x, double axes_r_y, double axes_r_z
, double start_v, double cruise_v, double accel);
double move_get_distance(struct move *m, double move_time);
struct coord move_get_coord(struct move *m, double move_time);
struct trapq *trapq_alloc(void);
void trapq_free(struct trapq *tq);
void trapq_check_sentinels(struct trapq *tq);
void trapq_add_move(struct trapq *tq, struct move *m);
void trapq_append(struct trapq *tq, double print_time
, double accel_t, double cruise_t, double decel_t
, double start_pos_x, double start_pos_y, double start_pos_z
, double axes_r_x, double axes_r_y, double axes_r_z
, double start_v, double cruise_v, double accel);
void trapq_finalize_moves(struct trapq *tq, double print_time);
void trapq_set_position(struct trapq *tq, double print_time
, double pos_x, double pos_y, double pos_z);
Expand Down
9 changes: 2 additions & 7 deletions klippy/console.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#
# This file may be distributed under the terms of the GNU GPLv3 license.
import sys, optparse, os, re, logging
import util, reactor, serialhdl, pins, msgproto, clocksync
import util, reactor, serialhdl, msgproto, clocksync

help_txt = """
This is a debugging console for the Klipper micro-controller.
Expand Down Expand Up @@ -43,7 +43,6 @@ def __init__(self, reactor, serialport, baud, canbus_iface, canbus_nodeid):
self.fd = sys.stdin.fileno()
util.set_nonblock(self.fd)
self.mcu_freq = 0
self.pins = pins.PinResolver(validate_aliases=False)
self.data = ""
reactor.register_fd(self.fd, self.process_kbd)
reactor.register_callback(self.connect)
Expand Down Expand Up @@ -223,11 +222,7 @@ def translate(self, line, eventtime):
return None
line = ''.join(evalparts)
self.output("Eval: %s" % (line,))
try:
line = self.pins.update_command(line).strip()
except:
self.output("Unable to map pin: %s" % (line,))
return None
line = line.strip()
if line:
parts = line.split()
if parts[0] in self.local_commands:
Expand Down
28 changes: 17 additions & 11 deletions klippy/extras/replicape.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@ def __init__(self, replicape, channel, pin_type, pin_params):
raise pins.error("Pin type not supported on replicape")
self._mcu = replicape.host_mcu
self._mcu.register_config_callback(self._build_config)
self._reactor = self._mcu.get_printer().get_reactor()
self._bus = REPLICAPE_PCA9685_BUS
self._address = REPLICAPE_PCA9685_ADDRESS
self._cycle_time = REPLICAPE_PCA9685_CYCLE_TIME
self._max_duration = 2.
self._oid = None
self._invert = pin_params['invert']
self._start_value = self._shutdown_value = float(self._invert)
self._is_enable = not not self._start_value
self._is_static = False
self._last_clock = 0
self._pwm_max = 0.
Expand All @@ -53,6 +55,7 @@ def setup_start_value(self, start_value, shutdown_value, is_static=False):
self._is_static = is_static
self._replicape.note_pwm_start_value(
self._channel, self._start_value, self._shutdown_value)
self._is_enable = not not self._start_value
def _build_config(self):
self._pwm_max = self._mcu.get_constant_float("PCA9685_MAX")
cycle_ticks = self._mcu.seconds_to_clock(self._cycle_time)
Expand Down Expand Up @@ -80,7 +83,12 @@ def set_pwm(self, print_time, value, cycle_time=None):
if self._invert:
value = 1. - value
value = int(max(0., min(1., value)) * self._pwm_max + 0.5)
self._replicape.note_pwm_enable(print_time, self._channel, value)
is_enable = not not value
if is_enable != self._is_enable:
self._is_enable = is_enable
self._reactor.register_async_callback(
(lambda e, s=self, pt=print_time, ie=is_enable:
s._replicape.note_pwm_enable(pt, s._channel, ie)))
self._set_cmd.send([self._oid, clock, value],
minclock=self._last_clock, reqclock=clock)
self._last_clock = clock
Expand Down Expand Up @@ -148,7 +156,7 @@ def __init__(self, replicape, pin_params):
pin_resolver.reserve_pin(resv1, config_name)
pin_resolver.reserve_pin(resv2, config_name)
def setup_cycle_time(self, cycle_time, hardware_pwm=False):
self.mcu_pwm.setup_cycle_time(cycle_time, True);
self.mcu_pwm.setup_cycle_time(cycle_time, True)

ReplicapeStepConfig = {
'disable': None,
Expand All @@ -171,6 +179,7 @@ def __init__(self, config):
self.mcu_pwm_enable = ppins.setup_pin('digital_out', enable_pin)
self.mcu_pwm_enable.setup_max_duration(0.)
self.mcu_pwm_start_value = self.mcu_pwm_shutdown_value = False
self.last_pwm_enable_time = 0.
# Setup power pins
self.pins = {
"power_e": (pca9685_pwm, 5), "power_h": (pca9685_pwm, 3),
Expand All @@ -180,7 +189,6 @@ def __init__(self, config):
self.servo_pins = {
"servo0": 3, "servo1": 2 }
# Setup stepper config
self.last_stepper_time = 0.
self.stepper_dacs = {}
shift_registers = [1, 0, 0, 1, 1]
for port, name in enumerate('xyzeh'):
Expand Down Expand Up @@ -227,18 +235,17 @@ def note_pwm_start_value(self, channel, start_value, shutdown_value):
self.mcu_pwm_enable.setup_start_value(
self.mcu_pwm_start_value, self.mcu_pwm_shutdown_value)
self.enabled_channels[channel] = not not start_value
def note_pwm_enable(self, print_time, channel, value):
is_enable = not not value
if self.enabled_channels[channel] == is_enable:
# Nothing to do
return
def note_pwm_enable(self, print_time, channel, is_enable):
self.enabled_channels[channel] = is_enable
# Check if need to set the pca9685 enable pin
pe_time = max(print_time, self.last_pwm_enable_time + PIN_MIN_TIME)
on_channels = [1 for c, e in self.enabled_channels.items() if e]
if not on_channels:
self.mcu_pwm_enable.set_digital(print_time, 0)
self.mcu_pwm_enable.set_digital(pe_time, 0)
self.last_pwm_enable_time = pe_time
elif is_enable and len(on_channels) == 1:
self.mcu_pwm_enable.set_digital(print_time, 1)
self.mcu_pwm_enable.set_digital(pe_time, 1)
self.last_pwm_enable_time = pe_time
# Check if need to set the stepper enable lines
if channel not in self.stepper_dacs:
return
Expand All @@ -250,7 +257,6 @@ def note_pwm_enable(self, print_time, channel, value):
sr = self.sr_enabled
else:
return
print_time = max(print_time, self.last_stepper_time + PIN_MIN_TIME)
clock = self.host_mcu.print_time_to_clock(print_time)
self.sr_spi.spi_send(sr, minclock=clock, reqclock=clock)
def setup_pin(self, pin_type, pin_params):
Expand Down
2 changes: 1 addition & 1 deletion klippy/extras/z_tilt.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def adjust_steppers(self, adjustments, speed):
s.set_trapq(None)
# Move each z stepper (sorted from lowest to highest) until they match
positions = [(-a, s) for a, s in zip(adjustments, self.z_steppers)]
positions.sort()
positions.sort(key=(lambda k: k[0]))
first_stepper_offset, first_stepper = positions[0]
z_low = curpos[2] - first_stepper_offset
for i in range(len(positions)-1):
Expand Down
4 changes: 2 additions & 2 deletions klippy/mcu.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def _build_config(self):
"trsync_state oid=%c can_trigger=%c trigger_reason=%c clock=%u")
ffi_main, ffi_lib = chelper.get_ffi()
self._trdispatch_mcu = ffi_main.gc(ffi_lib.trdispatch_mcu_alloc(
self._trdispatch, mcu._serial.serialqueue, # XXX
self._trdispatch, mcu._serial.get_serialqueue(), # XXX
self._cmd_queue, self._oid, set_timeout_tag, trigger_tag,
state_tag), ffi_lib.free)
def _shutdown(self):
Expand Down Expand Up @@ -746,7 +746,7 @@ def _connect(self):
raise error("Too few moves available on MCU '%s'" % (self._name,))
ffi_main, ffi_lib = chelper.get_ffi()
self._steppersync = ffi_main.gc(
ffi_lib.steppersync_alloc(self._serial.serialqueue,
ffi_lib.steppersync_alloc(self._serial.get_serialqueue(),
self._stepqueues, len(self._stepqueues),
move_count-self._reserved_move_slots),
ffi_lib.steppersync_free)
Expand Down
2 changes: 2 additions & 0 deletions klippy/serialhdl.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,8 @@ def get_reactor(self):
return self.reactor
def get_msgparser(self):
return self.msgparser
def get_serialqueue(self):
return self.serialqueue
def get_default_command_queue(self):
return self.default_cmd_queue
# Serial response callbacks
Expand Down
Loading

0 comments on commit 8c2e155

Please sign in to comment.