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

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

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

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

Q&A

解決済

1回答

537閲覧

ラズパイで方位センサから角度を知りたい

shuppi

総合スコア47

Python

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

0グッド

0クリップ

投稿2017/12/15 02:37

#!/usr/bin/env python # vim: set fileencoding=UTF-8 : # HMC5888L Magnetometer (Digital Compass) wrapper class # Based on https://bitbucket.org/thinkbowl/i2clibraries/src/14683feb0f96, # but uses smbus rather than quick2wire and sets some different init # params. import smbus import math import time import sys class hmc5883l: __scales = { 0.88: [0, 0.73], 1.30: [1, 0.92], 1.90: [2, 1.22], 2.50: [3, 1.52], 4.00: [4, 2.27], 4.70: [5, 2.56], 5.60: [6, 3.03], 8.10: [7, 4.35], } def __init__(self, port=1, address=0x1E, gauss=1.3, declination=(0,0)): self.bus = smbus.SMBus(port) self.address = address (degrees, minutes) = declination self.__declDegrees = degrees self.__declMinutes = minutes self.__declination = (degrees + minutes / 60) * math.pi / 180 (reg, self.__scale) = self.__scales[gauss] self.bus.write_byte_data(self.address, 0x00, 0x70) # 8 Average, 15 Hz, normal measurement self.bus.write_byte_data(self.address, 0x01, reg << 5) # Scale self.bus.write_byte_data(self.address, 0x02, 0x00) # Continuous measurement def declination(self): return (self.__declDegrees, self.__declMinutes) def twos_complement(self, val, len): # Convert twos compliment to integer if (val & (1 << len - 1)): val = val - (1<<len) return val def __convert(self, data, offset): val = self.twos_complement(data[offset] << 8 | data[offset+1], 16) if val == -4096: return None return round(val * self.__scale, 4) def axes(self): data = self.bus.read_i2c_block_data(self.address, 0x00) #print map(hex, data) x = self.__convert(data, 3) y = self.__convert(data, 7) z = self.__convert(data, 5) return (x,y,z) def heading(self): (x, y, z) = self.axes() headingRad = math.atan2(y, x) headingRad += self.__declination # Correct for reversed heading if headingRad < 0: headingRad += 2 * math.pi # Check for wrap and compensate elif headingRad > 2 * math.pi: headingRad -= 2 * math.pi # Convert to degrees from radians headingDeg = headingRad * 180 / math.pi return headingDeg def degrees(self, headingDeg): degrees = math.floor(headingDeg) minutes = round((headingDeg - degrees) * 60) return (degrees, minutes) def __str__(self): (x, y, z) = self.axes() return "Axis X: " + str(x) + "\n" \ "Axis Y: " + str(y) + "\n" \ "Axis Z: " + str(z) + "\n" \ "Declination: " + self.degrees(self.declination()) + "\n" \ "Heading: " + self.degrees(self.heading()) + "\n" if __name__ == "__main__": # http://magnetic-declination.com/Great%20Britain%20(UK)/Harrogate# compass = hmc5883l(gauss = 4.7, declination = (-2,5)) while True: sys.stdout.write("\rHeading: " + str(compass.degrees(compass.heading())) + " ") sys.stdout.flush() time.sleep(0.5)

このプログラムにatan2()を追加して角度を見たいのですが、どのように追加したらよいか分かりません。

教えてください。お願いします。

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

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

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

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

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

guest

回答1

0

ベストアンサー

質問に挙げていただいたコードのhmc5883l.heading() にて、atan2() を使ってradianを求め、更に degree に変換しているようですが、このmethod の戻り値では何か不具合があるのでしょうか?

Python

1def heading(self): 2 (x, y, z) = self.axes() 3 headingRad = math.atan2(y, x) # <--------------- 4 headingRad += self.__declination 5 6 # Correct for reversed heading 7 if headingRad < 0: 8 headingRad += 2 * math.pi 9 10 # Check for wrap and compensate 11 elif headingRad > 2 * math.pi: 12 headingRad -= 2 * math.pi 13 14 # Convert to degrees from radians 15 headingDeg = headingRad * 180 / math.pi 16 return headingDeg

投稿2017/12/15 05:00

magichan

総合スコア15898

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

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

shuppi

2017/12/15 06:05

すみません;よく見てませんでした。 実行結果に出た値がx,y座標っぽかったのでてっきり使っていないのかと思っていました。 ありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問