質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

1回答

397閲覧

収得したアナログ値をCSVで保存したい

fuzball_yumi

総合スコア13

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

0クリップ

投稿2018/07/13 06:46

下記のサンプルプログラムがあります。
ここで収得した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

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

以下のような流れでできます。
現在日時を取得(して文字列化)する、リストの内容をCSV出力する処理は
検索すればたくさん見つかりますので、ご自身でお考え下さい。

Python

1def do_measurement(): 2 lst = [] # 出力データをためておくリスト 3 while True: 4 # 現在日時を取得(して文字列化) 5 now = (略) 6 lst.append((now,AIN2,AIN3)) # 1行をタプルとして保持 7 if len(lst) >= 10000: 8 # lstの内容をCSV出力 9 lst = [] # リスト内容をリセット 10 11 ### STEP 4: DONE. Have fun! 12 nice_output(raw_channels, voltages)

投稿2018/07/13 07:22

can110

総合スコア38234

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

fuzball_yumi

2018/07/17 05:58 編集

File "test.py", line 78, in do_measurement csvfile.write(lst) TypeError: expected a character buffer objectage in V for the channels: とエラーが出てしまいCSVファイルが書き込めませんでした。 CSVの書込は参考にして下のようにしました。 def do_measurement(): ### STEP 1: Initialise ADC object: ads = ADS1256() ### STEP 2: Gain and offset self-calibration: ads.cal_self() lst = [] # 出力データをためておくリスト while True: ### STEP 3: Get data: raw_channels = ads.read_sequence(CH_SEQUENCE) voltages = [i * ads.v_per_digit for i in raw_channels] now = datetime.datetime.now().strftime("%H:%M:%S") lst.append((now,EXT2,EXT3)) # 1行をタプルとして保持 if len(lst) >= 10000: csvfile = open('test.csv', 'w') csvfile.write(lst) csvfile.write(',') csvfile.write('\n') csvfile.flush() csvfile.close() # lstの内容をCSV出力 lst = [] # リスト内容をリセット ### STEP 4: DONE. Have fun! nice_output(raw_channels, voltages) ### END EXAMPLE ###
can110

2018/07/17 07:20

まずは単純な「lst = [('1:00:00',1,2),('2:00:00',3,4)]」のようなリストを CSV出力するコード(部分)を完成させましょう。 サンプルコードや詳細な解説は検索すればたくさん見つかります。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問