-
Liam484
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.