![Terasic Self-Balancing Robot Скачать руководство пользователя страница 28](http://html.mh-extra.com/html/terasic/self-balancing-robot/self-balancing-robot_user-manual_1088554028.webp)
Self-Balancing Robot
User Guide
26
www.terasic.com
July 12, 2018
else counter<=1;
wire count_rst=(counter==22'h3fffff)?0:1;
Below is the description for each state.
State 0:
Set the output of TRIG pin to high level logic signal and the state machine goes to State 1.
State 1:
In State 1, the counter
trig_count
starts to accumulate until counter achieves 2000. Since,
the system clock is 50MHz. Thus, the accumulation time is 10us, so the request for pulling the
TRIG to logic high for 10us is complete. Then, pulls the TRIG signal to low and goes to State 2.
State 2:
Monitor whether the ECHO signal has rising edge state. If it has, the state machine goes to
State 3, or the state machine is stay in State 2 until
count_rst
achieves 0, then the State machine is
reset.
State 3:
Calculate the ultrasonic reflection time by using the
measure_count
, the state machine goes
to State 4 when ECHO pin signal has falling edge.
State 4:
idle state, waiting for
count_rst
achieve 0, reset the state machine and begin to next
detection.
⚫
⚫
S
S
o
o
f
f
t
t
w
w
a
a
r
r
e
e
C
C
o
o
d
d
e
e
After adding this IP to Qsys, Nios CPU can read the ultrasonic transmission and reflection time
from measure_value register of the IP. Then calculate the obstacle distance by the formula, the Nios
code in \Demonstrations\BAL_CAR_Nios_Code\software\DE10_Nano_bal\main.cpp is shown
below and for designers’ reference:
data = IORD(SONIC_DISTANCE_0_BASE,0x00);
distance = (float)data*34.0/100000.0;
Note:
As described in previous, the system clock in the IP is 50MHz and the distance unit is
centimeter, apply to the formula, it is data * 340 * 100 / (2 * 50 * 1000000) = data*34.0/100000.