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

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

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

Raspberry Piは、ラズベリーパイ財団が開発した、名刺サイズのLinuxコンピュータです。 学校で基本的なコンピュータ科学の教育を促進することを意図しています。

Python

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

Q&A

解決済

1回答

6883閲覧

TCS34725(RGBカラーセンサ)のデータロギングについて

hachigo0525

総合スコア11

Raspberry Pi

Raspberry Piは、ラズベリーパイ財団が開発した、名刺サイズのLinuxコンピュータです。 学校で基本的なコンピュータ科学の教育を促進することを意図しています。

Python

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

0グッド

0クリップ

投稿2017/11/28 22:59

編集2017/12/01 16:36

こんにちは、昨日に続いての初心者質問をお許しください。
ただいまRaspberry Pi3とRGBカラーセンサーモジュール(TCS34725)を使いカラーのデータを記録しようとしています。このセンサーはRed,Green,Blue,Clearの他にcolor tempurture,Luxを取得できます。
サンプルでデータの取得はできるのですが、それを継続し記録していく方法がわかりません。

python

1# Simple demo of reading color data with the TCS34725 sensor. 2# Will read the color from the sensor and print it out along with lux and 3# color temperature. 4# Author: Tony DiCola 5# License: Public Domain 6import time 7 8# Import the TCS34725 module. 9import Adafruit_TCS34725 10 11 12# Create a TCS34725 instance with default integration time (2.4ms) and gain (4x). 13import smbus 14tcs = Adafruit_TCS34725.TCS34725() 15 16# You can also override the I2C device address and/or bus with parameters: 17#tcs = Adafruit_TCS34725.TCS34725(address=0x30, busnum=2) 18 19# Or you can change the integration time and/or gain: 20#tcs = Adafruit_TCS34725.TCS34725(integration_time=Adafruit_TCS34725.TCS34725_INTEGRATIONTIME_700MS, 21# gain=Adafruit_TCS34725.TCS34725_GAIN_60X) 22# Possible integration time values: 23# - TCS34725_INTEGRATIONTIME_2_4MS (2.4ms, default) 24# - TCS34725_INTEGRATIONTIME_24MS 25# - TCS34725_INTEGRATIONTIME_50MS 26# - TCS34725_INTEGRATIONTIME_101MS 27# - TCS34725_INTEGRATIONTIME_154MS 28# - TCS34725_INTEGRATIONTIME_700MS 29# Possible gain values: 30# - TCS34725_GAIN_1X 31# - TCS34725_GAIN_4X 32# - TCS34725_GAIN_16X 33# - TCS34725_GAIN_60X 34 35# Disable interrupts (can enable them by passing true, see the set_interrupt_limits function too). 36tcs.set_interrupt(False) 37 38# Read the R, G, B, C color data. 39r, g, b, c = tcs.get_raw_data() 40 41# Calculate color temperature using utility functions. You might also want to 42# check out the colormath library for much more complete/accurate color functions. 43color_temp = Adafruit_TCS34725.calculate_color_temperature(r, g, b) 44 45# Calculate lux with another utility function. 46lux = Adafruit_TCS34725.calculate_lux(r, g, b) 47 48# Print out the values. 49print('Color: red={0} green={1} blue={2} clear={3}'.format(r, g, b, c)) 50 51# Print out color temperature. 52if color_temp is None: 53 print('Too dark to determine color temperature!') 54else: 55 print('Color Temperature: {0} K'.format(color_temp)) 56 57# Print out the lux. 58print('Luminosity: {0} lux'.format(lux)) 59 60# Enable interrupts and put the chip back to low power sleep/disabled. 61tcs.set_interrupt(True) 62tcs.disable()

これを実行すると
Color: red=540 green=563 blue=413 clear=1024
Color Temperature: 4545 K
Luminosity: 411 lux
と出ます。
http://deviceplus.jp/hobby/raspberrypi_entry_039/ この方のやられているようにこのサンプルに書き加えてやろうとしたのですが、readDataの付け方の失敗だったりとうまくいきません。何かアドバイスや参考になるサイトなどを教えていただけませんか?

温度センサなどと違いこのセンサーはあまり他のサイトなどで例になっていなく、初心者には全くどこをどう変えていけばいいかの方向性が見つかりません、、何卒ご協力お願いいたします、、

https://github.com/adafruit/Adafruit_Python_TCS34725.git ここにあったサンプルです。

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

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

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

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

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

hachigo0525

2017/11/29 16:14 編集

ご指摘ありがとうございます!正直、bme280.pyの中身のどれが必要でどれが要らないか、どれが何の役割を果たしているのかっていうところすあやふやでお話になっていませんでした。あるサイトでbme280.pyを詳しく解説しているのを見つけたので、今一度自分で書いてみて質問いたします汗
KSwordOfHaste

2017/11/29 16:22

大事なファイルを上書きしてしまったりといった失敗をするとまずいので、何をやっているか研究してみてくださいね。解説記事が役立つといいですね。
guest

回答1

0

ベストアンサー

温度センサなどと違いこのセンサーはあまり他のサイトなどで例になっていなく、

気しないといけないことは、入力と処理と出力です。入力がカラーセンサーでも、スイッチでも、温度センサーでも、pythonに数値として突っ込んでしまえばあとは同じ(?)です。

解決方法に繋がるヒントを書きます。

必要になる処理

*** 文字列の表示**
pythonで文字列を表示するにはprint(○○○)を使います。
例えば、Google検索すると一番初めに出てくる解説ページはこちらです。
サンプルでprint()されている部分をファイルに書き出すことができればよいですね。
*** データの出力**
pythonで数値を書き出す方法はいろいろあるかもしれませんが、手っ取り早いのはCSVですね。
例えば、こちらの解説ページが参考になると思います。
※CSVはコンマと改行でできたExcelと相性の良いテキストデータです。
*** 繰り返し処理**
pythonでずっと続ける繰り返し処理をするには、whileが楽です。while 続ける条件という文法です。強制終了させるまで続けるのであれば、while True(TrueがTrueの間=強制終了しない限り無限にループ)が良いと思います。
※ただ、このやり方ではキーボードでCtrl+Cを押して止めないといけません。もし、キーボードがつながっていなければ、(外部にボタンや通信手段があって、それから操作できない限り)電源を引き抜くしかありません。電源OFFによる強制終了ではRPiのOSが破損するので、実装する中でどうするか調整してください。
*** 待機処理**
pythonでwhileループを使って文字列を保存し続けると、間髪入れずにものすごい勢いで処理が進んでしまいます。例えば、0.01秒に1度データが記録されては、1秒動かしただけで100件もデータが残ってしまいます。これでは困るので、sleepを使います。解説ページはこちらが良いと思います。time.sleep(60)とすれば、1分待機=1分に1度しか動かなくなります。

CSVのイメージ

ここで、hachigo0525さんがやりたいのことは、print()で出てくる数値を記録したいということですので、print()で出てくる文字列を数値で保存できれば良いですね。たとえば、以下のような感じでデータが並んだ状態で書き出されればOKと思われます。
|値|値1|値2|値3|値4|値5|値6|値7|
|:--:|:--:|:--:|:--:|:--:|:--:|
|書き出す変数|時刻|r|g|b|c|color_temp|lux|

実際に書き出されるCSVの完成形イメージはこんな感じです。

text

1"時刻","r","g","b","c","color_temp","lux" 20:00,540,563,413,1024,4545,411 30:30,540,563,413,1024,4545,411 41:00,540,563,413,1024,4545,411 51:30,540,563,413,1024,4545,411

完成形イメージ

  1. 必要になるモジュールの読み込み
  2. TCS34725の定義
  3. csvの書き込みを用意する
  4. ループ処理の中で、TCS34725の値を拾い、CSVに書き出す。
  5. 必要であれば何らかのループ終了処理を挟む。

参考)
全部分からない、とするとなかなか回答がもらえませんので、ここまでやったけれども○○という点がよく分からない、と質問するとより良い回答が得られると思います。

投稿2017/11/29 22:26

退会済みユーザー

退会済みユーザー

総合スコア0

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

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

hachigo0525

2017/11/30 22:37

ありがとうございます!とても参考になります! # -*- coding: utf-8 -*- # Simple demo of reading color data with the TCS34725 sensor. # Will read the color from the sensor and record it out along with lux and # color temperature. import time # Import the TCS34725 module. import Adafruit_TCS34725 # Create a TCS34725 instance with default integration time (2.4ms) and gain (4x). import smbus tcs = Adafruit_TCS34725.TCS34725() # Disable interrupts (can enable them by passing true, see the set_interrupt_limits function too). tcs.set_interrupt(False) # Read the R, G, B, C color data. r, g, b, c = tcs.get_raw_data() # Calculate color temperature using utility functions. You might also want to # check out the colormath library for much more complete/accurate color functions. color_temp = Adafruit_TCS34725.calculate_color_temperature(r, g, b) # Calculate lux with another utility function. lux = Adafruit_TCS34725.calculate_lux(r, g, b) import csv file = open("RGBdata.csv","a") writer = csv.writer(file,lineterminator='\n') writer.writerow((r,g,b,c,color_temp,lux)) file.close() # Enable interrupts and put the chip back to low power sleep/disabled. tcs.set_interrupt(True) tcs.disable() これでデータをcsvファイルにためていくことに成功いたしました! しかし、現在時間を読み込むことができずにいます。今そちらについて調べています。(しばらくするとまた質問をするかもしれません汗)  ありがとうございます!
hachigo0525

2017/12/01 14:09

# -*- coding: utf-8 -*- # Simple demo of reading color data with the TCS34725 sensor. # Will read the color from the sensor and record it out along with lux and # color temperature. import time # Import the TCS34725 module. import Adafruit_TCS34725 # Create a TCS34725 instance with default integration time (2.4ms) and gain (4x). import smbus tcs = Adafruit_TCS34725.TCS34725() # Disable interrupts (can enable them by passing true, see the set_interrupt_limits function too). tcs.set_interrupt(False) # Read the R, G, B, C color data. r, g, b, c = tcs.get_raw_data() # Calculate color temperature using utility functions. You might also want to # check out the colormath library for much more complete/accurate color functions. color_temp = Adafruit_TCS34725.calculate_color_temperature(r, g, b) # Calculate lux with another utility function. lux = Adafruit_TCS34725.calculate_lux(r, g, b) from datetime import datetime now = datetime.now().strftime("%Y/%m/%d %H:%M") import csv file = open("RGBdata.csv","a") writer = csv.writer(file,lineterminator='\n') writer.writerow([now,r,g,b,c,color_temp,lux]) file.close() # Enable interrupts and put the chip back to low power sleep/disabled. tcs.set_interrupt(True) tcs.disable() これでいけました泣 writerow(now, r,,,,)で失敗続きでしたがよく見るとwriterow([now, r,,,,]) [ ]←このかっこが他のプログラミングについていていました。それに気づいて、成功した時の爽快感たるやいなやでした泣  ハマりそうです!ありがとうございました!!!!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問