Skip to content

Commit 6ea016e

Browse files
authored
add bltouch/servo support (#49)
1 parent 635f3a4 commit 6ea016e

File tree

6 files changed

+106
-7
lines changed

6 files changed

+106
-7
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#define BLTOUCH_DEPLOY 10
2+
#define BLTOUCH_STOW 90
3+
#define BLTOUCH_SW_MODE 60
4+
#define BLTOUCH_SELFTEST 120
5+
#define BLTOUCH_MODE_STORE 130
6+
#define BLTOUCH_5V_MODE 140
7+
#define BLTOUCH_OD_MODE 150
8+
#define BLTOUCH_RESET 160
9+
10+
#include "print_bed.h"
11+
#include "bed_probe.h"
12+
#include "pwm_reader.h"
13+
14+
#include "BLTouch.h"
15+
16+
void BLTouchProbe::interrupt(GpioEvent& ev) {
17+
if (ev.pin_id == m_servo_pin) {
18+
auto angle = dutycycle_to_degrees(m_servo->pwm_duty);
19+
if (is_close_enough(angle, BLTOUCH_DEPLOY)) {
20+
m_probe->enabled = true;
21+
m_deployed = true;
22+
} else if (is_close_enough(angle, BLTOUCH_STOW)) {
23+
m_probe->enabled = false;
24+
m_deployed = false;
25+
}
26+
}
27+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#pragma once
2+
#include <imgui.h>
3+
#include "../virtual_printer.h"
4+
5+
constexpr uint16_t cmap(uint16_t x, uint16_t in_min, uint16_t in_max, uint16_t out_min, uint16_t out_max) {
6+
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
7+
}
8+
constexpr uint16_t dutycycle_to_degrees(uint16_t value) {
9+
return cmap((float(value) / std::numeric_limits<uint16_t>::max()) * 20000, 540, 2400, 0, 180);
10+
}
11+
constexpr bool is_close_enough(uint16_t value, uint16_t target, uint16_t max_error = 1) {
12+
return (value >= target - max_error) && (value <= target + max_error);
13+
}
14+
15+
class BLTouchProbe : public VirtualPrinter::Component {
16+
public:
17+
BLTouchProbe(pin_type servo_pin, pin_type probe_pin, glm::vec3 offset, glm::vec4& position, PrintBed& bed) : VirtualPrinter::Component("BLTouch") {
18+
m_servo_pin = servo_pin;
19+
m_servo = add_component<PWMReader>("PWM Control Signal", servo_pin);
20+
m_probe = add_component<BedProbe>("Probe Signal", probe_pin, offset, position, bed);
21+
Gpio::attach(servo_pin, [this](GpioEvent& event){ this->interrupt(event); });
22+
}
23+
virtual ~BLTouchProbe() {}
24+
25+
void ui_widget() {
26+
if (m_deployed) {
27+
ImGui::Text("Probe Deployed");
28+
} else {
29+
ImGui::Text("Probe Stowed");
30+
}
31+
ImGui::Text("Command Angle: %d", dutycycle_to_degrees(m_servo->pwm_duty));
32+
}
33+
34+
void interrupt(GpioEvent& ev);
35+
36+
private:
37+
bool m_deployed = false;
38+
pin_type m_servo_pin;
39+
std::shared_ptr<PWMReader> m_servo;
40+
std::shared_ptr<BedProbe> m_probe;
41+
};

src/MarlinSimulator/hardware/bed_probe.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,19 @@ class BedProbe : public VirtualPrinter::Component {
1616

1717
void ui_widget() {
1818
auto has_triggered = triggered();
19-
ImGui::Checkbox("Triggered State", &has_triggered);
20-
ImGui::Text("Nozzle Distance Above Bed: %f", position.z - bed.calculate_z({position.x + offset.x, position.y + offset.y}));
19+
if (enabled) {
20+
ImGui::Checkbox("Triggered State", &has_triggered);
21+
ImGui::Text("Nozzle Distance Above Bed: %f", position.z - bed.calculate_z({position.x + offset.x, position.y + offset.y}));
22+
} else {
23+
ImGui::Text("Disabled");
24+
}
2125
}
2226

2327
bool triggered() {
24-
return position.z <= bed.calculate_z({position.x + offset.x, position.y + offset.y}) - offset.z;
28+
return enabled && position.z <= bed.calculate_z({position.x + offset.x, position.y + offset.y}) - offset.z;
2529
}
2630

31+
bool enabled = true;
2732
pin_type probe_pin;
2833
glm::vec3 offset;
2934
glm::vec4& position;

src/MarlinSimulator/hardware/pwm_reader.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ void PWMReader::ui_widget() {
2828
ImGui::Text("Software PWM (%.2fHz) Duty Cycle: %.2f%%", pwm_period ? float(Kernel::TimeControl::ONE_BILLION) / Kernel::TimeControl::ticksToNanos(pwm_period) : 0, pwm_period ? float(pwm_duty * 100) / pwm_period : 0);
2929
break;
3030
case Hardware:
31-
ImGui::Text("Hardware PWM Duty Cycle : %.2f%%", pwm_period ? float(pwm_duty * 100) / pwm_period : 0);
31+
ImGui::Text("Hardware PWM Duty Cycle : (%ld) %.2f%%", pwm_duty, pwm_period ? float(pwm_duty * 100) / pwm_period : 0);
3232
break;
3333
}
3434
}
@@ -48,7 +48,7 @@ void PWMReader::interrupt(GpioEvent& ev) {
4848
} else if ( ev.event == ev.SET_VALUE) { // Hardware PWM
4949
pwm_mode = Hardware;
5050
pwm_duty = Gpio::get_pin_value(pwm_pin);
51-
pwm_period = 255;
51+
pwm_period = std::numeric_limits<uint16_t>::max();
5252
}
5353
}
5454
}

src/MarlinSimulator/virtual_printer.cpp

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1+
#include <algorithm>
12
#include <imgui.h>
23

34
#include "hardware/Button.h"
45
#include "hardware/StepperDriver.h"
56
#include "hardware/EndStop.h"
67
#include "hardware/Heater.h"
78
#include "hardware/print_bed.h"
8-
#include "hardware/print_bed.h"
99
#include "hardware/bed_probe.h"
1010
#include "hardware/ST7796Device.h"
1111
#include "hardware/HD44780Device.h"
@@ -16,6 +16,7 @@
1616
#include "hardware/NeoPixelDevice.h"
1717
#include "hardware/KinematicSystem.h"
1818
#include "hardware/pwm_reader.h"
19+
#include "hardware/BLTouch.h"
1920

2021
#include "virtual_printer.h"
2122

@@ -48,6 +49,8 @@ void VirtualPrinter::Component::ui_widgets() {
4849
}
4950
}
5051

52+
std::map<uint64_t, uint64_t> servo_pin_lookup { {0, SERVO0_PIN}, {1, SERVO1_PIN}, {2, SERVO2_PIN}, {3, SERVO3_PIN}};
53+
5154
void VirtualPrinter::build() {
5255
root = add_component<Component>("root");
5356

@@ -69,7 +72,11 @@ void VirtualPrinter::build() {
6972
auto print_bed = root->add_component<PrintBed>("Print Bed", glm::vec2{X_BED_SIZE, Y_BED_SIZE});
7073

7174
#if HAS_BED_PROBE
72-
root->add_component<BedProbe>("Probe",
75+
#if ENABLED(BLTOUCH)
76+
root->add_component<BLTouchProbe>("BLTouch", servo_pin_lookup[Z_PROBE_SERVO_NR],
77+
#else
78+
root->add_component<BedProbe>("Probe",
79+
#endif
7380
#ifdef Z_MIN_PROBE_USES_Z_MIN_ENDSTOP_PIN
7481
Z_MIN_PIN
7582
#else
@@ -140,6 +147,24 @@ void VirtualPrinter::build() {
140147
root->add_component<PWMReader>("Fan0", FAN0_PIN);
141148
#endif
142149

150+
#ifdef Z_PROBE_SERVO_NR
151+
#define SERVO_TEST(N) Z_PROBE_SERVO_NR != N
152+
#else
153+
#define SERVO_TEST(N) 1
154+
#endif
155+
#if NUM_SERVOS > 0 && SERVO_TEST(0)
156+
root->add_component<PWMReader>("Servo0", SERVO0_PIN);
157+
#endif
158+
#if NUM_SERVOS > 1 && SERVO_TEST(1)
159+
root->add_component<PWMReader>("Servo1", SERVO1_PIN);
160+
#endif
161+
#if NUM_SERVOS > 2 && SERVO_TEST(2)
162+
root->add_component<PWMReader>("Servo2", SERVO2_PIN);
163+
#endif
164+
#if NUM_SERVOS > 3 && SERVO_TEST(3)
165+
root->add_component<PWMReader>("Servo3", SERVO3_PIN);
166+
#endif
167+
143168
#if ENABLED(SPI_FLASH)
144169
root->add_component<W25QxxDevice>("SPI Flash", spi_bus_by_pins<SPI_FLASH_SCK_PIN, SPI_FLASH_MOSI_PIN, SPI_FLASH_MISO_PIN>(), SPI_FLASH_CS_PIN, SPI_FLASH_SIZE);
145170
#endif

src/MarlinSimulator/virtual_printer.h

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

3+
#include <algorithm>
34
#include <string>
45
#include <memory>
56
#include <map>

0 commit comments

Comments
 (0)