前の記事はこちら New Rescue Simulation を動かしてみる・・・その4
New Rescue Simulation ですが・・・Rescue Maze を模しているのに、Tutorial 1にあった、一番単純なプログラムが「ランダム走行」だったのが気に入らなくて、せめて右手法で進むプログラムを作ってみよう・・・と頑張ってきました。
その成果がこちら
スタートしたら、外周に沿って進み・・・
沼地を超えると、そこには黒の落とし穴が・・・
でも、落とし穴を回避するプログラムは入っていないので・・・
コロンと落ちました。(笑)
とりあえず、右手法のプログラムはコレです。
from controller import Robot
timeStep = 32
max_velocity = 6.28
robot = Robot()
wheel_left = robot.getMotor("left wheel motor")
wheel_right = robot.getMotor("right wheel motor")
rightSensors = []
rightSensors.append(robot.getDistanceSensor("ps1"))
rightSensors[0].enable(timeStep)
rightSensors.append(robot.getDistanceSensor("ps2"))
rightSensors[1].enable(timeStep)
# [left wheel speed, right wheel speed]
speeds = [max_velocity,max_velocity]
wheel_left.setPosition(float("inf"))
wheel_right.setPosition(float("inf"))
while robot.step(timeStep) != -1:
if rightSensors[0].getValue() > 0.12:
if rightSensors[1].getValue() < 0.07:
#Go stright
speeds[0] = 0.6 * max_velocity
speeds[1] = 0.6 * max_velocity
else:
#turn right
speeds[0] = 0.6 * max_velocity
speeds[1] = -0.2 * max_velocity
elif rightSensors[0].getValue() > 0.07:
#Go stright
speeds[0] = 0.6 * max_velocity
speeds[1] = 0.6 * max_velocity
else:
#turn left
speeds[0] = -0.2 * max_velocity
speeds[1] = 0.6 * max_velocity
wheel_left.setVelocity(speeds[0])
wheel_right.setVelocity(speeds[1])
Pyhonを知らない人間がコーディングしているので、基本的な間違いがあるかもしれません・・・
続きの記事はこちら New Rescue Simulation を動かしてみる・・・その6