Skip to content

Commit 8fc416b

Browse files
committed
Add velocity sample
1 parent 53066c2 commit 8fc416b

File tree

2 files changed

+147
-0
lines changed

2 files changed

+147
-0
lines changed

SampleProgram/README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ This directory is licensed under the Apache 2.0 License, see [SampleProgram / LI
1515

1616
Each peripheral example has sample code files written Shell Script, C and Python.
1717

18+
RT Software Tutorialsでは動画つきで説明しています。
19+
20+
https://rt-net.github.io/tutorials/raspimouse/driver/samples.html
21+
1822
## Shell Script
1923

2024
```sh
@@ -96,3 +100,37 @@ Read the lightsensors values.
96100
モータを回して、パルスカウンタの値を読み込みます。
97101

98102
Drive the motors and read the pulse counters values.
103+
104+
# Step7
105+
106+
車体速度![v_{fw}](https://render.githubusercontent.com/render/math?math=%5Cdisplaystyle+v_%7Bfw%7D)
107+
車体角速度![v_{rot}](https://render.githubusercontent.com/render/math?math=%5Cdisplaystyle+v_%7Brot%7D)で指令するサンプルプログラムです。
108+
109+
サンプルでは車体速度と車体角速度を指定しての移動が実装されています。
110+
111+
## 解説
112+
113+
左右のモータへの制御信号を![\omega_{rot}](https://render.githubusercontent.com/render/math?math=%5Cdisplaystyle+%5Comega_%7Brot%7D)(Hz)で入力したときの、
114+
車体の並進方向のスピードを![v_{fw}](https://render.githubusercontent.com/render/math?math=%5Cdisplaystyle+v_%7Bfw%7D)(m/s)とします。
115+
右のモータへの制御信号を![\omega_{fw}](https://render.githubusercontent.com/render/math?math=%5Cdisplaystyle+%5Comega_%7Bfw%7D)(Hz)、
116+
左のモータへ![\omega_{fw}](https://render.githubusercontent.com/render/math?math=%5Cdisplaystyle+%5Comega_%7Bfw%7D)(Hz)としたとき、
117+
ロボットが旋回するときの角速度を![v_{rot}](https://render.githubusercontent.com/render/math?math=%5Cdisplaystyle+v_%7Brot%7D)(rad/s)とします。
118+
119+
このとき、ホイールの直径を![\phi](https://render.githubusercontent.com/render/math?math=%5Cdisplaystyle+%5Cphi)、車体のトレッドを![t](https://render.githubusercontent.com/render/math?math=%5Cdisplaystyle+t)、モータ1回転のための制御信号を![p](https://render.githubusercontent.com/render/math?math=%5Cdisplaystyle+p)とします。
120+
121+
モータ1回転につき車体が並進方向に進む距離は以下のように表現できます。
122+
123+
![v_{fw} : \omega_{fw} = \pi \phi : p](https://render.githubusercontent.com/render/math?math=%5Clarge+%5Cdisplaystyle+v_%7Bfw%7D+%3A+%5Comega_%7Bfw%7D+%3D+%5Cpi+%5Cphi++%3A+p)
124+
125+
並進方向についても同様に考えます。
126+
車体が1回転するときのホイールの移動距離は![\pi t](https://render.githubusercontent.com/render/math?math=%5Cdisplaystyle+%5Cpi+t)(m)です。
127+
モータ1回転でホイールは![\pi \phi](https://render.githubusercontent.com/render/math?math=%5Cdisplaystyle+%5Cpi+%5Cphi)(m)移動するので、
128+
モータ1回転につき車体が旋回する角度は以下のように表現できます。
129+
130+
![v_{rot} : \omega_{rot} = \frac{2\pi} {\pi t / \pi \phi} : p](https://render.githubusercontent.com/render/math?math=%5Clarge+%5Cdisplaystyle+v_%7Brot%7D+%3A+%5Comega_%7Brot%7D+%3D+%5Cfrac%7B2%5Cpi%7D+%7B%5Cpi+t+%2F+%5Cpi+%5Cphi%7D++%3A+p)
131+
132+
これらを整理するとモータ制御信号は以下の2つの式にまとめられます。
133+
134+
![\omega_{fw} = \frac{p} {\pi \phi} v_{fw} ](https://render.githubusercontent.com/render/math?math=%5Cdisplaystyle+%5Comega_%7Bfw%7D+%3D+%5Cfrac%7Bp%7D+%7B%5Cpi+%5Cphi%7D+v_%7Bfw%7D+)
135+
136+
![\omega_{rot} = \frac {tp}{2\pi \phi} v_{rot}](https://render.githubusercontent.com/render/math?math=%5Cdisplaystyle+%5Comega_%7Brot%7D+%3D+%5Cfrac+%7Btp%7D%7B2%5Cpi+%5Cphi%7D+v_%7Brot%7D)

SampleProgram/step7.py

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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

Comments
 (0)