|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | + |
| 4 | +# Copyright 2022 RT Corporation |
| 5 | +# |
| 6 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +# you may not use this file except in compliance with the License. |
| 8 | +# You may obtain a copy of the License at |
| 9 | +# |
| 10 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +# |
| 12 | +# Unless required by applicable law or agreed to in writing, software |
| 13 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +# See the License for the specific language governing permissions and |
| 16 | +# limitations under the License. |
| 17 | + |
| 18 | +import math |
| 19 | +import time |
| 20 | + |
| 21 | +class Motor: |
| 22 | + def __init__(self, wheel_diamiter=0.048, wheel_tread=0.0925, pulse_per_rotate=400): |
| 23 | + # ハードウェアパラメータ |
| 24 | + self.wheel_diamiter = wheel_diamiter |
| 25 | + self.wheel_tread = wheel_tread |
| 26 | + self.pulse_per_rotate = pulse_per_rotate |
| 27 | + |
| 28 | + # デバイスファイルを通してモータの回転角度を指定 |
| 29 | + def _set_motor_speed(self, left, right): |
| 30 | + try: |
| 31 | + with open('/dev/rtmotor_raw_l0', 'w') as lf, \ |
| 32 | + open('/dev/rtmotor_raw_r0', 'w') as rf: |
| 33 | + lf.write(str(int(left))) |
| 34 | + rf.write(str(int(right))) |
| 35 | + except Exception as e: |
| 36 | + print(e) |
| 37 | + |
| 38 | + # デバイスファイルからモータの電源をON/OFF |
| 39 | + def _set_motor_power(self, mode): |
| 40 | + try: |
| 41 | + with open('/dev/rtmotoren0', 'w') as f: |
| 42 | + f.write('1' if mode else '0') |
| 43 | + except Exception as e: |
| 44 | + print(e) |
| 45 | + |
| 46 | + # 車体速度(並進方向の移動速度および旋回の角速度)からモータ指令値を計算 |
| 47 | + def _calc_speed(self, linear, angular): |
| 48 | + """ |
| 49 | + linear: robot linear speed (m/s) |
| 50 | + angular: robot angular speed (rad/s) |
| 51 | + """ |
| 52 | + left_speed = self.meter_to_pulse(linear) - self.angle_to_pulse(angular) |
| 53 | + right_speed = self.meter_to_pulse(linear) + self.angle_to_pulse(angular) |
| 54 | + print("target speed (left: {}, right: {})".format(left_speed, right_speed)) |
| 55 | + return left_speed, right_speed |
| 56 | + |
| 57 | + # ラズパイマウスのモータパルスでの距離をメートル系に変換 |
| 58 | + def pulse_to_meter(self, pulse): |
| 59 | + return math.pi * self.wheel_diamiter * pulse / self.pulse_per_rotate |
| 60 | + |
| 61 | + # メートル系での距離をラズパイマウスのモータパルスに変換 |
| 62 | + def meter_to_pulse(self, meter): |
| 63 | + return int(self.pulse_per_rotate * meter / (math.pi * self.wheel_diamiter)) |
| 64 | + |
| 65 | + # ラズパイマウスの車体の旋回角度をラズパイマウスのモータパルスに変換 |
| 66 | + def angle_to_pulse(self, angle): |
| 67 | + return int(angle * self.pulse_per_rotate * self.wheel_tread / self.wheel_diamiter / (2.0 * math.pi)) |
| 68 | + |
| 69 | + # 走行させるための関数 |
| 70 | + def run(self, linear_speed, angular_speed): |
| 71 | + """ |
| 72 | + linear_speed: m/s |
| 73 | + angular_speed: rad/s |
| 74 | + """ |
| 75 | + (speed_l, speed_r) = self._calc_speed(linear_speed, angular_speed) |
| 76 | + |
| 77 | + self._set_motor_speed(speed_l, speed_r) |
| 78 | + |
| 79 | + |
| 80 | +if __name__ == '__main__': |
| 81 | + motor = Motor() |
| 82 | + print("Motor On") |
| 83 | + motor._set_motor_power(1) |
| 84 | + |
| 85 | + print("Turn 3.14[rad/s]") |
| 86 | + motor.run(linear_speed=0, angular_speed=3.14) |
| 87 | + time.sleep(1) |
| 88 | + print("Turn -3.14[rad/s]") |
| 89 | + motor.run(linear_speed=0, angular_speed=-3.14) |
| 90 | + time.sleep(1) |
| 91 | + print("Move forward at 0.2[m/s]") |
| 92 | + motor.run(linear_speed=0.2, angular_speed=0) |
| 93 | + time.sleep(1) |
| 94 | + print("Move backward 0.2[m/s]") |
| 95 | + motor.run(linear_speed=-0.2, angular_speed=0) |
| 96 | + time.sleep(1) |
| 97 | + print("Turn 1.57[rad/s] + Move forward 0.3[m/s]") |
| 98 | + motor.run(linear_speed=0.3, angular_speed=1.57) |
| 99 | + time.sleep(1) |
| 100 | + print("Turn -1.57[rad/s] + Move backward 0.3[m/s]") |
| 101 | + motor.run(linear_speed=-0.3, angular_speed=-1.57) |
| 102 | + time.sleep(1) |
| 103 | + |
| 104 | + print("Stop") |
| 105 | + motor.run(linear_speed=0, angular_speed=0) |
| 106 | + time.sleep(0.1) |
| 107 | + |
| 108 | + print("Motor Off") |
| 109 | + motor._set_motor_power(0) |
0 commit comments