Python Forum
High-Precision Board Voltage Reading from Python into PD
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
High-Precision Board Voltage Reading from Python into PD
#1
I am looking for some advice on python scripting into pure data. The plan is to receive an incoming voltage via the High-Precision AD/DA Board into the Raspberry Pi, then have that changing voltage modulate parameters within Pure Data via Python. I have bastardised code from both an example from the AD board to read voltage and a tutorial to send messages from Python to Pure Data which both work. My question is, how would I go about writing the python code to send the voltage readings from one channel of the AD board to manipulate in real time a fader in pure data? Here's the Python code I am currently working with:
import os
import sys
import time
import numpy as np
import itertools
from pipyadc.ADS1256_definitions import *
from pipyadc import ADS1256
import pipyadc.ADS1256_default_config as myconfig_2

def send2pd(message=''):

os.system("echo '" + message + "' | pdsend 3000")
def AudioOn():
message = '0 1;'
send2Pd(message)

def Start_Stop():
message = '2 1;'
send2Pd(message)

def Set_Fader():
message = '1 ' + str() + ';'
send2Pd(message)

send2pd('0 1;')
send2pd('2 1;')

POTI = POS_AIN0|NEG_AINCOM

LDR = POS_AIN1|NEG_AINCOM

EXT2, EXT3, EXT4 = POS_AIN2|NEG_AINCOM, POS_AIN3|NEG_AINCOM, POS_AIN4|NEG_AINCOM
EXT5, EXT6, EXT7 = POS_AIN5|NEG_AINCOM, POS_AIN6|NEG_AINCOM, POS_AIN7|NEG_AINCOM

POTI_INVERTED = POS_AINCOM|NEG_AIN0

SHORT_CIRCUIT = POS_AIN0|NEG_AIN0

CH_SEQUENCE = (POTI, LDR, EXT2, EXT3, EXT4, EXT7, POTI_INVERTED, SHORT_CIRCUIT)

CH_OFFSET = np.array((-10, 0, -85, 0, 750, 0, 0, 0), dtype=np.int)
GAIN_CAL = np.array((1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0), dtype=np.float)

FILTER_SIZE = 32

def do_measurement():

ads2 = ADS1256(myconfig_2)   
ads2.drate = DRATE_30000
ads2.cal_self()

CH_GAIN = ads2.v_per_digit * GAIN_CAL

rows, columns = FILTER_SIZE, len(CH_SEQUENCE)
filter_buffer = np.zeros((rows, columns), dtype=np.int)

timestamp = time.time() 
for data_row in itertools.cycle(filter_buffer):

    ads2.read_sequence(CH_SEQUENCE, data_row)

    elapsed = time.time() - timestamp
    if elapsed > 1:
        timestamp += 1

        
        ch_unscaled = np.average(filter_buffer, axis=0) - CH_OFFSET
        ch_volts = ch_unscaled * CH_GAIN

        nice_output([int(i) for i in ch_unscaled], ch_volts)
def nice_output(digits, volts):
sys.stdout.write(
"\0337"
+

"""

These are the sample values converted to voltage in V for the channels:
Poti_CH0, LDR_CH1, AIN2, AIN3, AIN4, AIN7, Poti NEG, Short 0V
"""
+ ", ".join(["{: 8.0f}".format(i) for i in volts])
+ "\n\033[J\0338"
)

send2pd
('1 ' + str() + ';') #fader send

try:
print("\033[2J\033[H")
print(doc)
print("\nPress CTRL-C to exit.")
do_measurement()

except (KeyboardInterrupt):
print("\n"*8 + "User exit.\n")
send2pd('2 0;')
send2pd('0 0;')
Any advice on how to do this or a better/simpler way of achieving this will be much appreciated Thanks.
Larz60+ write Mar-29-2021, 10:48 AM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.

Fixed for you this time. Please use bbcode tags on future posts.
Reply
#2
I'm not familiar with Pure Data but I see there are modules available like purity and pylibpd which seem designed to pass data from python to Pure Data. Have you checked them out yet?

https://pypi.org/project/purity/#:~:text...0interface.

https://github.com/libpd/libpd
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Gmpy2 Newbie Working on Precision charlesrkiss 5 557 Jan-23-2024, 04:23 PM
Last Post: charlesrkiss
  Precision with Decimal Type charlesrkiss 9 734 Jan-18-2024, 06:30 PM
Last Post: charlesrkiss
  Trying to make a board with turtle, nothing happens when running script Quascia 3 672 Nov-01-2023, 03:11 PM
Last Post: deanhystad
  How to test the high-score in my Python-game Pluviometer 2 579 Oct-02-2023, 06:55 PM
Last Post: deanhystad
  Precision conversion garynewport 2 951 Oct-19-2022, 10:47 AM
Last Post: garynewport
Photo HOW FIX MY BOARD GAME LAZABI 3 1,470 Apr-01-2022, 04:23 PM
Last Post: BashBedlam
  The game should now run and terminate once the board is full, but still can’t identif rango 0 1,459 Jul-22-2021, 03:24 AM
Last Post: rango
  Enabling interrupt on Adafruits button/led board Moris526 0 2,020 Apr-30-2021, 03:29 PM
Last Post: Moris526
Photo Filtering data and precision during calculations Scientifix 0 1,787 Mar-30-2021, 01:00 PM
Last Post: Scientifix
  Interrupt for Adafruits Neotrellis button/led board Moris526 0 1,801 Dec-28-2020, 05:42 AM
Last Post: Moris526

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020