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

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

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

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

Q&A

解決済

1回答

20209閲覧

TypeError: can't multiply sequence by non-int of type 'float'

aiai8976

総合スコア112

Python

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

0グッド

0クリップ

投稿2019/06/05 04:22

前提・実現したいこと

現在、ラズパイでpythonを使ってジャイロセンサーから値を取得しようとしています。
その時にでたエラーで困っています。
処理にかかった時間を取得して角速度から角度を算出したいです。
floatだとlistに格納されないのでしょうか。
お分かりの方がいましたらコメントお願いします。

発生している問題・エラーメッセージ

Traceback (most recent call last): File "gyro.py", line 143, in <module> rotation_degree.append(gyro_z*elapsed_time) TypeError: can't multiply sequence by non-int of type 'float'

該当のソースコード

# -*- coding: utf-8 -*- #!/usr/bin/python # import module import smbus # use I2C import math # mathmatics import time from time import sleep # time module # # define # # slave address DEV_ADDR = 0x68 # device address # register address ACCEL_XOUT = 0x3b ACCEL_YOUT = 0x3d ACCEL_ZOUT = 0x3f TEMP_OUT = 0x41 GYRO_XOUT = 0x43 GYRO_YOUT = 0x45 GYRO_ZOUT = 0x47 PWR_MGMT_1 = 0x6b # PWR_MGMT_1 PWR_MGMT_2 = 0x6c # PWR_MGMT_2 new_degree=0 sum_degree=0 time=0.018 bus = smbus.SMBus(1) # Sleep解除. bus.write_byte_data(DEV_ADDR, PWR_MGMT_1, 0) # # Sub function # # 1byte read def read_byte(adr): return bus.read_byte_data(DEV_ADDR, adr) # 2byte read def read_word(adr): high = bus.read_byte_data(DEV_ADDR, adr) low = bus.read_byte_data(DEV_ADDR, adr+1) val = (high << 8) + low return val # Sensor data read def read_word_sensor(adr): val = read_word(adr) if (val >= 0x8000): # minus return -((65535 - val) + 1) else: # plus return val # # 温度 # def get_temp(): temp = read_word_sensor(TEMP_OUT) x = temp / 340 + 36.53 # data sheet(register map)記載の計算式. return x # # 角速度(full scale range ±250 deg/s # LSB sensitivity 131 LSB/deg/s # -> ±250 x 131 = ±32750 LSB[16bitで表現]) # Gyroscope Configuration GYRO_CONFIG (reg=0x1B) # FS_SEL(Bit4-Bit3)でfull scale range/LSB sensitivityの変更可. # # get gyro data def get_gyro_data_lsb(): x = read_word_sensor(GYRO_XOUT) y = read_word_sensor(GYRO_YOUT) z = read_word_sensor(GYRO_ZOUT) return [x, y, z] def get_gyro_data_deg(): x,y,z = get_gyro_data_lsb() x = x / 131.0 y = y / 131.0 z = z / 131.0 return [x, y, z] # # 加速度(full scale range ±2g # LSB sensitivity 16384 LSB/g) # -> ±2 x 16384 = ±32768 LSB[16bitで表現]) # Accelerometer Configuration ACCEL_CONFIG (reg=0x1C) # AFS_SEL(Bit4-Bit3)でfull scale range/LSB sensitivityの変更可. # # get accel data def get_accel_data_lsb(): x = read_word_sensor(ACCEL_XOUT) y = read_word_sensor(ACCEL_YOUT) z = read_word_sensor(ACCEL_ZOUT) return [x, y, z] # get accel data def get_accel_data_g(): x,y,z = get_accel_data_lsb() x = x / 16384.0 y = y / 16384.0 z = z / 16384.0 return [x, y, z] # # Main function # i=0 gyro_z=[] rotation_degree=[] sum_degree=0 while i<100: # 温度. #temp = get_temp() # 小数点以下第1位まで表示. #print ('temperature[degrees C]:') #print ('%04.1f' % temp) #print ('||') # 角速度. #gyro_x,gyro_y,gyro_z = get_gyro_data_deg() t1 = time.time() gyro_z.append(get_gyro_data_deg()) t2 = time.time() elapsed_time=t2-t1 # 小数点以下第3位まで表示. #print ('gyro[deg/s]') #print ('x: %08.3f' % gyro_x) #print ('y: %08.3f' % gyro_y) #print ('z: %08.3f' % gyro_z) #print ('||') # 加速度 #accel_x,accel_y,accel_z = get_accel_data_g() # 小数点以下第3位まで表示. #print ('accel[g]') #print ('x: %06.3f' % accel_x) #print ('y: %06.3f' % accel_y) #print ('z: %06.3f' % accel_z) #print ('\n') # 改行. rotation_degree.append(gyro_z*elapsed_time) #sum_degree+=rotation_degree #print('degree: %08.3f' % sum_degree) #print(sum_degree) #sleep(time) i=i+1 for i in range(100): print ('gyro[deg/s]') print('z: %08.3f' % gyro_z(i)) sum_degree+=rotation_degree(i) print('degree: %08.3f' % sum_degree) print('\n')

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

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

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

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

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

guest

回答1

0

ベストアンサー

gyro_z*elapsed_time は

numpy の multiply(gyro_z, elapsed_time) にすればいいと思います。

たとえば リストの [1,2,3] に対して

整数を掛ける場合の意味はこうなります

>>> [1,2,3] * 10 [1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3]

それぞれに小数をかけたいのであれば

>>> [1,2,3] * 10.0 Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: can't multiply sequence by non-int of type 'float'

このようなエラーになってしまいます。

希望されているのは各要素について

>>> import numpy >>> numpy.multiply([1,2,3], 10.0) array([10., 20., 30.])

こういう結果を求めてますよね?たぶん。

投稿2019/06/05 04:33

tetsunosuke

総合スコア1292

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

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

aiai8976

2019/06/05 04:48

そういうことでした! エラー解決しました! ありがとうございます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問