How to read "adc_temperature" value in Macro

Basic Information:

Printer Model: N/A
MCU / Printerboard: Custom STM32G0B1 board
Host / SBC: CM4
klippy.log / NA

In my functional test macro, I’m trying to read an adc_temperature sensor.

On my board, there are three “temperature” sensors (two being actual temperature sensors and one being a voltage monitor circuit (PS_voltage). I’ve defined them as:

[heater_generic heater0]
#gcode_id:
#   The id to use when reporting the temperature in the M105 command.
#   This parameter must be provided.
heater_pin: PD6  #$# HEATER0
#max_power:
sensor_type: EPCOS 100K B57560G104F
sensor_pin: PA0  #$# THERM0
#smooth_time:
control: pid
pid_Kp: 67.552
pid_Ki: 1.603
pid_Kd: 711.829
min_temp: 0
max_temp: 60
#   See the "extruder" section for the definition of the above
#   parameters.

[heater_generic heater1]
#gcode_id:
#   The id to use when reporting the temperature in the M105 command.
#   This parameter must be provided.
heater_pin: PB3  #$# HEATER1
#max_power:
sensor_type: EPCOS 100K B57560G104F
sensor_pin: PA1  #$# THERM1
#smooth_time:
control: pid
pid_Kp: 67.552
pid_Ki: 1.603
pid_Kd: 711.829
min_temp: 0
max_temp: 60
#   See the "extruder" section for the definition of the above
#   parameters.

[adc_temperature PS_voltage]
# PSU Voltage Value from Thermistor Value
# Assumuptions:
# 1. Power Supply Voltage from 0V to 70V
# 2. Thermistor ADC Pullup 4.7k
temperature1: 2
voltage1: 0.151
temperature2: 70
voltage2: 3.049
[temperature_sensor PS_voltage]
adc_voltage: 3.3
sensor_pin: PA2  #$# VINMON
sensor_type: PS_voltage
min_temp: 11  # For 12V Power Supply
max_temp: 26  # For 24V Power Supply
gcode_id: PS_voltage 

To read them and display their values, I use the macro code:

RESPOND TYPE=command MSG="Test1: Heater0 Temperature: { printer["heater_generic heater0"].temperature }"
RESPOND TYPE=command MSG="Test1: Heater1 Temperature: { printer["heater_generic heater1"].temperature }"
      
RESPOND TYPE=command MSG="Test1: VINMON Voltage { printer["adc_temperature PS_voltage"].temperature }"

heater0 and heater1 return values without issue (and match what is shown on the Mainsail Dashboard) but the RESPOND line for PS_voltage causes the error:

I’ve looked through klipper/klippy/extras and it looks like the other temperature python objects have a get_status method which is accessed by the macro processor whereas adc_temperature does not.

I think all that’s needed to return the temperature value is to add the following get_status method to klipper/klippy/extras/adc_temperature.py in the PrinterADCtoTemperature class:

 def get_status(self, eventtime):
        return {
            'temperature': round(self.last_temp, 2),
        }

Would this be correct?

The temperature is in the temperature_sensor PS_voltage section: Status reference - Klipper documentation

Cheers,
-Kevin

Hi Kevin,

Thanx for the reply - now when I look at the link:

There is no temperature_sensor PS_voltage section - I’m not sure what you mean by it.


I’m trying to work it out on my own (with the change above and variations) but not having any luck.

I realized that last_temp wasn’t used in adc_temperature.py so I’ve changed it to just temp and also added the min/max temperatures without any luck ie:

    def get_status(self, eventtime):
        return {
            'temperature': round(self.temp, 2),
            'min_temp': round(self.min_temp, 2),
            'max_temp': round(self.max_temp, 2)
        }

Any idea what I’m doing wrong?

I’ve been through the klippy (1).log (1.4 MB) and can see that the problem is at line 61 of /home/biqu/klipper/klippy/extras/gcode_macro.py which is:

return str(self.template.render(context))

Thanx for answering.

In the config snippet you posted you have a [temperature_sensor PS_voltage] config section. That config section should produce a printer["temperature_sensor PS_voltage"].temperature variable that is accessible from gcode macros. It’s documented in the link I posted above. Sometimes there are weird things with case sensitive names - you might want to double check that.

You definitely don’t need to modify the code. Everything that mainsail/fluidd sees is available to macros - if you see the temperature in the GUI it’s available to the macros.

Hope that helps,
-Kevin

1 Like

Okay, I changed the line in the macro to:

      RESPOND TYPE=command MSG="Test1: VINMON Voltage { printer["temperature_sensor PS_voltage"].temperature }"

I also removed the added code in adc_temperature.py and stopped/started the klipper service.

And that worked a treat:

I don’t know if it will be helpful to anybody down the road but the issue is how I visualized the printer.cfg adc_temperature statement.

I treated adc_temperature as the device and not the modifier/descriptor of temperature_sensor.
To make the relationship clearer, I’m going to move adc_temperature to below temperature_sensor.

Thank you for explaining that.

EDIT:

When I reversed the order of the adc_temperature and it’s corresponding temperature_sensor statements and hit SAVE & RESTART I got:

So, I guess this is a case where the order of the statements in printer.cfg matters.

The order of the two statements above is the correct one.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.