下記のサンプルプログラムがあります。
ここで収得したAIN2,AIN3のアナログ値を10000個貯まったらCSVに保存したいのですが方法が解りません。
保存する形式は時間とain2の値、ain3の値です。
ご教授お願いします。
python
1#!/usr/bin/python 2# -*- coding: utf-8 -*- 3"""PiPyADC: Example file for class ADS1256 in module pipyadc: 4 5ADS1256 cycling through eight input channels. 6 7Hardware: Waveshare ADS1256 board interfaced to the Raspberry Pi 3 8 9Ulrich Lukas 2017-03-10 10""" 11import sys 12from ADS1256_definitions import * 13from pipyadc import ADS1256 14 15### START EXAMPLE ### 16################################################################################ 17### STEP 0: CONFIGURE CHANNELS AND USE DEFAULT OPTIONS FROM CONFIG FILE: ### 18# 19# For channel code values (bitmask) definitions, see ADS1256_definitions.py. 20# The values representing the negative and positive input pins connected to 21# the ADS1256 hardware multiplexer must be bitwise OR-ed to form eight-bit 22# values, which will later be sent to the ADS1256 MUX register. The register 23# can be explicitly read and set via ADS1256.mux property, but here we define 24# a list of differential channels to be input to the ADS1256.read_sequence() 25# method which reads all of them one after another. 26# 27# ==> Each channel in this context represents a differential pair of physical 28# input pins of the ADS1256 input multiplexer. 29# 30# ==> For single-ended measurements, simply select AINCOM as the negative input. 31# 32# AINCOM does not have to be connected to AGND (0V), but it is if the jumper 33# on the Waveshare board is set. 34# 35# Input pin for the potentiometer on the Waveshare Precision ADC board: 36POTI = POS_AIN0|NEG_AINCOM 37# Light dependant resistor of the same board: 38LDR = POS_AIN1|NEG_AINCOM 39# The other external input screw terminals of the Waveshare board: 40EXT2, EXT3, EXT4 = POS_AIN2|NEG_AINCOM, POS_AIN3|NEG_AINCOM, POS_AIN4|NEG_AINCOM 41EXT5, EXT6, EXT7 = POS_AIN5|NEG_AINCOM, POS_AIN6|NEG_AINCOM, POS_AIN7|NEG_AINCOM 42 43# You can connect any pin as well to the positive as to the negative ADC input. 44# The following reads the voltage of the potentiometer with negative polarity. 45# The ADC reading should be identical to that of the POTI channel, but negative. 46POTI_INVERTED = POS_AINCOM|NEG_AIN0 47 48# For fun, connect both ADC inputs to the same physical input pin. 49# The ADC should always read a value close to zero for this. 50SHORT_CIRCUIT = POS_AIN0|NEG_AIN0 51 52# Specify here an arbitrary length list (tuple) of arbitrary input channel pair 53# eight-bit code values to scan sequentially from index 0 to last. 54# Eight channels fit on the screen nicely for this example.. 55CH_SEQUENCE = (POTI, LDR, EXT2, EXT3, EXT4, EXT7, POTI_INVERTED, SHORT_CIRCUIT) 56################################################################################ 57 58 59def do_measurement(): 60 ### STEP 1: Initialise ADC object: 61 ads = ADS1256() 62 63 ### STEP 2: Gain and offset self-calibration: 64 ads.cal_self() 65 66 while True: 67 ### STEP 3: Get data: 68 raw_channels = ads.read_sequence(CH_SEQUENCE) 69 voltages = [i * ads.v_per_digit for i in raw_channels] 70 71 ### STEP 4: DONE. Have fun! 72 nice_output(raw_channels, voltages) 73 74### END EXAMPLE ### 75 76 77############################################################################# 78# Format nice looking text output: 79def nice_output(digits, volts): 80 sys.stdout.write( 81 "\0337" # Store cursor position 82 + 83""" 84These are the raw sample values for the channels: 85Poti_CH0, LDR_CH1, AIN2, AIN3, AIN4, AIN7, Poti NEG, Short 0V 86""" 87 + ", ".join(["{: 8d}".format(i) for i in digits]) 88 + 89""" 90 91These are the sample values converted to voltage in V for the channels: 92Poti_CH0, LDR_CH1, AIN2, AIN3, AIN4, AIN7, Poti NEG, Short 0V 93""" 94 + ", ".join(["{: 8.3f}".format(i) for i in volts]) 95 + "\n\033[J\0338" # Restore cursor position etc. 96 ) 97 98 99# Start data acquisition 100try: 101 print("\033[2J\033[H") # Clear screen 102 print(__doc__) 103 print("\nPress CTRL-C to exit.") 104 do_measurement() 105 106except (KeyboardInterrupt): 107 print("\n"*8 + "User exit.\n") 108 sys.exit(0) 109
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/07/17 05:58 編集
2018/07/17 07:20