Skip to content

Commit

Permalink
U e optical surface probe: MVP (MarlinFirmware#9)
Browse files Browse the repository at this point in the history
* WIP

* bump stm32 targets to c++17

* add more methods, add type safety enums, fix syntax

* add WIP config and source file

* more aggressive cpp version bump

* improve type aliasing and namespacing

* add pin initialization, implement simple analog reader

* fix missing semicolon

* add optical surface probe pin assignments

* add instansiation and Mcode for reading sensor value

* add interval reporting option

* add comments, change scope of variables used for parsing

* add position to probe interval report
  • Loading branch information
arades79 authored Dec 6, 2021
1 parent 8920266 commit f102185
Show file tree
Hide file tree
Showing 8 changed files with 188 additions and 12 deletions.
5 changes: 5 additions & 0 deletions Marlin/Configuration_adv.h
Original file line number Diff line number Diff line change
Expand Up @@ -4258,6 +4258,11 @@
#define POST_AUTOCAL_SAFE_Z_HEIGHT -20.0
#endif

#define OPTICAL_SURFACE_PROBE
#if ENABLED(OPTICAL_SURFACE_PROBE)
#define ____HELLO____ :)
#endif

// @section develop

//
Expand Down
2 changes: 1 addition & 1 deletion Marlin/src/HAL/STM32/MarlinSerial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
#endif
#if USING_HW_SERIAL7
#ifdef I_DONT_TRUST_MSERIAL
HardwareSerial MSerial7(PE7, PE8)
HardwareSerial MSerial7(PE7, PE8);
#else
DECLARE_SERIAL_PORT(7)
#endif
Expand Down
19 changes: 19 additions & 0 deletions Marlin/src/feature/optical_surface_probe.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include "../inc/MarlinConfig.h"

#if ENABLED(OPTICAL_SURFACE_PROBE)
#include "optical_surface_probe.h"
#include "../gcode/gcode.h"
#include "../gcode/parser.h"

static OpticalSurfaceProbe probe;

void GcodeSuite::M1100()
{
const auto val = probe.get_distance();
SERIAL_ECHOLNPAIR("Probe raw reading: ", val);

const auto ms = parser.intval('P', 0);
probe.interval_report(ms);
}

#endif // OPTICAL_SURFACE_PROBE
130 changes: 130 additions & 0 deletions Marlin/src/feature/optical_surface_probe.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
#pragma once

#include "../inc/MarlinConfig.h"

#include "../module/planner.h"

#include <string_view>
// using namespace std::string_view_literals;

struct OpticalSurfaceProbe
{
OpticalSurfaceProbe()
{
SET_INPUT(OPT_SURF_IN_PIN);
SET_INPUT(OPT_SURF_ERR_PIN);
SET_OUTPUT(OPT_SURF_MFI_PIN);
OUT_WRITE(OPT_SURF_LED_ON_PIN, HIGH);
}

void init();

const auto get_distance() const
{
return analogRead(OPT_SURF_IN_PIN);
}

/**
* @brief reports analog sensor reading at a set interval
* using a hardware timer based interrupt
*
* @param ms milliseconds between reports
*/
void interval_report(const int ms)
{
// requires static lifetime for interrupts to trigger,
// and to ensure we can stop the timer later.
static HardwareTimer timer{TIM12};
timer.pause();
if (ms <= 0)
return;

timer.setOverflow(ms * 1000, TimerFormat_t::MICROSEC_FORMAT);
timer.attachInterrupt([this]
{
const auto position = planner.get_axis_positions_mm();
SERIAL_ECHOLNPAIR("prb:", get_distance(), ",X:", position.x, ",Y:", position.y, ",Z:", position.z);
});
timer.resume();
}

/**
* @brief TODO: unimplemented, intended to alter sensor angle
*
*/
void deploy() {}

/**
* @brief TODO: unimplemented, intended to alter sensor angle
*
*/
void retract() {}

private:
constexpr static size_t READ_BUFFER_SIZE{255};

/**
* @brief Private inner struct for holding serial communication
* protocols and other sensor specific implementation details
*
*/
struct
{
using str = std::string_view;

void begin() { serial.begin(115200); }

// user functions
enum class User
{
User,
Professional
};
void login(const User, const str);
void set_password(const str);
const User get_user() const;
void set_default_user(const User);

enum class Trigger
{
None,
Pulse,
Edge,
Software
};
void set_trigger(const Trigger);

enum class MFILevel
{
High,
Low
};
void set_mfi_level(const MFILevel);

enum class OutputMode
{
None,
Uart,
Analog
};
void set_output_mode(const OutputMode);

void set_analog_scale_default();
void set_analog_scale(const float min, const float max);

void get_info() const;
void get_all_settings() const { serial.println("PRINT"); }
void set_all_factory_defaults();

private:
HardwareSerial &serial{OPT_SURF_HW_SERIAL};

void read_response()
{
char read_buf[READ_BUFFER_SIZE] = {0}; // zero initialized buffer
serial.readBytesUntil('>', read_buf, READ_BUFFER_SIZE);
str parse_string{read_buf, READ_BUFFER_SIZE};
}

} protocol;
};
4 changes: 4 additions & 0 deletions Marlin/src/gcode/gcode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1029,6 +1029,10 @@ void GcodeSuite::process_parsed_command(const bool no_ok/*=false*/) {
case 7219: M7219(); break; // M7219: Set LEDs, columns, and rows
#endif

#if ENABLED(OPTICAL_SURFACE_PROBE)
case 1100: M1100(); break;
#endif

default: parser.unknown_command_warning(); break;
}
break;
Expand Down
5 changes: 5 additions & 0 deletions Marlin/src/gcode/gcode.h
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@
* M995 - Touch screen calibration for TFT display
* M997 - Perform in-application firmware update
* M999 - Restart after being stopped by error
* M1100 - Read optical surface probe
* D... - Custom Development G-code. Add hooks to 'gcode_D.cpp' for developers to test features. (Requires MARLIN_DEV_MODE)
*
* "T" Codes
Expand Down Expand Up @@ -1142,6 +1143,10 @@ class GcodeSuite {
static void M710();
#endif

#if ENABLED(OPTICAL_SURFACE_PROBE)
static void M1100();
#endif

static void T(const int8_t tool_index);

};
Expand Down
31 changes: 22 additions & 9 deletions Marlin/src/pins/stm32f7/pins_NUCLEO_F746ZG.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,13 @@
* AVDD | · · | PF13 (BTN_EN1)
* _CN9_ AGND | · · | PE9 (BTN_EN2)
* (TEMP_0) PA3 | · · | PD7 GND | · · | PE11 (BTN_ENC)
* (TEMP_BED) PC0 | · · | PD6 (XYZ_RX) PB1 | · · | PF14
* PC3 | · · | PD5 (XYZ_TX) PC2 | · · | PE13
* PF3 | · · | PD4 (Y2_EN) PF4 | · · | PF15
* PF5 | · · | PD3 (Y2_STEP) PB6 | · · | PG14 TX
* PF10 | · · | GND (Y2_DIR) PB2 | · · | PG9 RX
* NC | · · | PE2 (LG_CS) GND | · · | PE8 TX
* PA7 | · · | PE4 (LG_EN) PD13 | · · | PE7 RX
* (TEMP_BED) PC0 | · · | PD6 (XYZ_RX) PB1 | · · | PF14 (PROBE_LED_ON)
* (PROBE) PC3 | · · | PD5 (XYZ_TX) PC2 | · · | PE13 (PROBE_MFI)
* PF3 | · · | PD4 (Y2_EN) PF4 | · · | PF15 (PROBE_ERR)
* PF5 | · · | PD3 (Y2_STEP) PB6 | · · | PG14 LG_TX
* PF10 | · · | GND (Y2_DIR) PB2 | · · | PG9 LG_RX
* NC | · · | PE2 (LG_CS) GND | · · | PE8 PROBE_TX
* PA7 | · · | PE4 (LG_EN) PD13 | · · | PE7 PROBE_RX
* (LG_DIR) PF2 | · · | PE5 (LG_STOP) LG_STEP PD12 | · · | GND
* (Y_STEP) PF1 | · · | PE6 (Y_EN) (Z_STEP) PD11 | · · | PE10 (Z_EN)
* (Y_DIR) PF0 | · · | PE3 (Y_CS/UART) (Z_DIR) PE2 | · · | PE12 (Z_CS/UART)
Expand Down Expand Up @@ -180,9 +180,22 @@
//#define USING_HW_SERIAL4 1
#endif

#define OPTICAL_SENSOR_1_PIN PD0
#define OPTICAL_SENSOR_2_PIN PD1
#if ENABLED(OPTICAL_SURFACE_PROBE)
#define OPT_SURF_IN_PIN PC3 // white
#define OPT_SURF_ERR_PIN PF15 // brown
#define OPT_SURF_MFI_PIN PE13// violet
#define OPT_SURF_LED_ON_PIN PF14 // black

#define OPT_SURF_HW_SERIAL MSerial7
#ifndef USING_HW_SERIAL7
#define USING_HW_SERIAL7 1
#endif
#endif

#if ENABLED(OPTICAL_AUTOCAL)
#define OPTICAL_SENSOR_1_PIN PD0
#define OPTICAL_SENSOR_2_PIN PD1
#endif
//
// Temperature Sensors
//
Expand Down
4 changes: 2 additions & 2 deletions ini/stm32-common.ini
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
platform = ststm32@^13.0.0
board_build.core = stm32
build_flags = ${common.build_flags}
-std=gnu++14 -DHAL_STM32
-std=gnu++17 -DHAL_STM32
-DUSBCON -DUSBD_USE_CDC
-DTIM_IRQ_PRIO=13
-DADC_RESOLUTION=12
build_unflags = -std=gnu++11
build_unflags = -std=gnu++11 -std=gnu++14
src_filter = ${common.default_src_filter} +<src/HAL/STM32> +<src/HAL/shared/backtrace>
extra_scripts = ${common.extra_scripts}
pre:buildroot/share/PlatformIO/scripts/stm32_serialbuffer.py
Expand Down

0 comments on commit f102185

Please sign in to comment.