<!DOCTYPE aesl-source>
<network>


<!--list of global events-->
<event name="acc_control" size="3"/>
<event name="button_emit" size="1"/>
<constant name="COEFF" value="20"/>
<constant name="MAX_SPEED" value="800"/>
<constant name="START" value="2"/>
<constant name="STOP" value="1"/>
<constant name="WHEELSPEED" value="200"/>
<constant name="XMAXHT" value="800"/>
<constant name="XMINHT" value="-800"/>
<constant name="YMAXHT" value="500"/>
<constant name="YMINHT" value="-600"/>
<constant name="distFactor" value="100"/>


<!--node Robot 1 (transmitter)-->
<node name="Robot 1 (transmitter)" nodeId="{aec1be2d-68cb-4db2-908b-d2624249aef8}"><![CDATA[# Robot 1 (Transmitter)
# This robot transmits accelerometer values wirelessly.
# Robot 2 receives these values and uses them to move the maze.
#
# Start with Robot 1 on a flat table and press the center button on Robot 1
# to enter or exit calibration mode.
# When Robot 2 is flashing red, use the left, right, forward, and backward
# buttons on Robot 1 to move the wheels to the 9:00 and 3:00 positions and
# level the maze.
# After exiting calibration mode, Robot 2 will flash green and can be
# controlled by tilting Robot 1.
# Note: The wheels do not return to exactly the same position every time, so
# calibration may drift after several back-and-forth movements. If this
# happens, stop and recalibrate.
#
# ロボット1（送信機）
# このロボットは加速度センサーの値を無線で送信します。
# ロボット2は受信した値を使って迷路を動かします。
#
# ロボット1を平らな机の上に置き、中央ボタンを押して
# キャリブレーションモードの開始・終了を切り替えます。
# ロボット2が赤色で点滅している間は、ロボット1の
# 左・右・前・後ボタンを使ってホイールを9時と3時の位置に合わせ、
# 迷路を水平にしてください。
# キャリブレーションモードを終了すると、ロボット2は緑色で点滅し、
# ロボット1の傾きで操作できるようになります。
# 注意：ホイールは毎回まったく同じ位置には戻らないため、
# 前後に何度も動かすとキャリブレーションがずれることがあります。
# その場合は停止して再度キャリブレーションを行ってください。

onevent acc
emit acc_control acc

if abs(acc[0]) > 5 or abs(acc[1]) > 5 then
    call leds.top(abs(acc[0]*4),abs(acc[1]*4),0)
else
    call leds.top(abs(acc[1]*4),abs(acc[0]*4),0)
end

onevent buttons
when button.forward == 1 do
    emit button_emit 1
end

when button.center == 1 do
    emit button_emit 0
end

when button.left == 1 do
    emit button_emit 2
end

when button.right == 1 do
    emit button_emit 3
end

when button.backward == 1 do
    emit button_emit -1
end]]></node>


<!--node Robot 2 -- (receiver)-->
<node name="Robot 2 -- (receiver)" nodeId="{1f47361f-987a-4c54-b1f8-fe361db16507}"><![CDATA[# Robot 2 (Receiver)
# This robot receives accelerometer values from Robot 1 and uses them to move the maze.
# The robot is mounted upside down.
# The left wheel controls the east-west axis and its pin should be set to 9:00.
# The right wheel controls the north-south axis and its pin should be set to 3:00.
# The north side of the maze corresponds to the front-left corner of Thymio.
#
# ロボット2（受信機）
# このロボットはロボット1から加速度センサーの値を受信し、その値を使って迷路を動かします。
# ロボットは上下逆さまに取り付けられています。
# 左側のホイールは東西方向を制御し、ピンを9時の位置に設定します。
# 右側のホイールは南北方向を制御し、ピンを3時の位置に設定します。
# 迷路の北側はThymioの左前方に対応します。

var state = STOP
var lSpeed = 0
var rSpeed = 0
var calibMode = 0
var xPos = 0
var yPos = 0
var moveToX = 0
var moveToY = 0
var calibrating = 0
var temp = 0
var led_pulse = 0

timer.period[0] = 60
timer.period[1] = 80

onevent acc_control
    if state == START then
        moveToX = -event.args[0] * 100
        if moveToX < XMINHT then
            moveToX = XMINHT
        end
        if moveToX > XMAXHT then
            moveToX = XMAXHT
        end

        moveToY = -event.args[1] * 100
        if moveToY < YMINHT then
            moveToY = YMINHT
        end
        if moveToY > YMAXHT then
            moveToY = YMAXHT
        end

        if xPos - moveToX < -distFactor then
            rSpeed = -WHEELSPEED
            motor.right.target = -rSpeed
        elseif xPos - moveToX > distFactor then
            rSpeed = WHEELSPEED
            motor.right.target = -rSpeed
        else
            rSpeed = 0
            motor.right.target = 0
        end

        if yPos - moveToY < -distFactor then
            lSpeed = -WHEELSPEED
            motor.left.target = -lSpeed
        elseif yPos - moveToY > distFactor then
            lSpeed = WHEELSPEED
            motor.left.target = -lSpeed
        else
            lSpeed = 0
            motor.left.target = 0
        end
    end

onevent button_emit
    if state == STOP then
        if event.args[0] == 2 then
            if calibrating == 0 then
                calibrating = 1
                motor.right.target = -WHEELSPEED / 2
                motor.left.target = 0
            else
                calibrating = 0
                motor.left.target = 0
                motor.right.target = 0
            end
        end

        if event.args[0] == 3 then
            if calibrating == 0 then
                calibrating = 1
                motor.right.target = WHEELSPEED / 2
                motor.left.target = 0
            else
                calibrating = 0
                motor.left.target = 0
                motor.right.target = 0
            end
        end

        if event.args[0] == 1 then
            if calibrating == 0 then
                calibrating = 1
                motor.left.target = WHEELSPEED / 2
                motor.right.target = 0
            else
                calibrating = 0
                motor.left.target = 0
                motor.right.target = 0
            end
        end

        if event.args[0] == -1 then
            if calibrating == 0 then
                calibrating = 1
                motor.left.target = -WHEELSPEED / 2
                motor.right.target = 0
            else
                calibrating = 0
                motor.left.target = 0
                motor.right.target = 0
            end
        end
    end

    if event.args[0] == 0 then
        xPos = 0
        yPos = 0
        moveToX = 0
        moveToY = 0

        if state == STOP then
            state = START
        elseif state == START then
            state = STOP
        end

        lSpeed = 0
        rSpeed = 0
        motor.left.target = 0
        motor.right.target = 0
    end

onevent timer0
    xPos -= rSpeed / 2

    if xPos < XMINHT - distFactor then
        xPos = XMINHT
    elseif xPos > XMAXHT + distFactor then
        xPos = XMAXHT
    end

    if state == STOP then
        led_pulse = led_pulse + 40
    end

    if led_pulse > 40 then
        led_pulse = -50
    end

    temp = -led_pulse / 2

    if state == START then
        call leds.top(0, temp, 0)
        call leds.bottom.left(0, temp, 0)
        call leds.bottom.right(0, temp, 0)
    elseif state == STOP then
        call leds.top(temp, 0, 0)
        call leds.bottom.left(temp, 0, 0)
        call leds.bottom.right(temp, 0, 0)
    end

onevent timer1
    yPos -= lSpeed / 2

    if yPos < YMINHT - distFactor then
        yPos = YMINHT
    elseif yPos > YMAXHT + distFactor then
        yPos = YMAXHT
    end]]></node>


</network>
