Home > other >  Openmv ultrasonic ranging
Openmv ultrasonic ranging

Time:12-18

To see a post before, the program is running inside there is a bug, not solved, there are a great god can solve??
The import time, utime, pyb
The from pyb import Pin

Wave_echo_pin=Pin (' P7, Pin IN the Pin. PULL_NONE)
Wave_trig_pin=Pin (' P8, Pin. OUT_PP, Pin PULL_DOWN)

Wave_distance=0
Tim_counter=0
Flag_wave=0

# ultrasonic start
Def wave_start () :
Wave_trig_pin. Value (1)
Utime. Sleep_us (15)
Value (0)
wave_trig_pin.
# ultrasonic distance calculation
Def wave_distance_calculation () :
# global variable declaration
Global tim_counter
F # frequency of 0.2 MHZ high level time t=count * 1/f
Wave_distance tim_counter * 5 * 0.017=
# output eventually distance measuring unit (cm)
Print (' wave_distance wave_distance)

# ultrasonic data processing
Def wave_distance_process () :
Global flag_wave
If (flag_wave==0) :
Wave_start ()
If (flag_wave==2) :
Wave_distance_calculation ()
Flag_wave=0

# configuration timer
Tim=pyb. The Timer (1, prescaler=720, period=65535) # is equal to the freq=0.2 M

# external interrupt configuration
Def callback (line) :
Global flag_wave, tim_counter
# rising along the trigger processing
If (wave_echo_pin. Value ()) :
Tim. Init (prescaler=720, period=65535)
Flag_wave=1
Falling edge #
The else:
Tim. Deinit ()
Tim_counter=Tim. Counter ()
Tim. Counter (0)
Extint. Disable ()
Flag_wave=2
# interrupt configuration
Extin=pyb. ExtInt (wave_echo_pin, pyb. ExtInt. IRQ_RISING_FALLING, pyb. Pin. PULL_DOWN, callback)
While (True) :
Wave_distance_process ()
Time. Sleep (100).

CodePudding user response:

Change after measure available, because the source text does not start again interrupted so can output a
But there is a problem, openmv cannot output 5 v voltage, so ultrasonic module power supply still need flight control board,
The import time, utime, pyb
The from pyb import Pin

Wave_echo_pin=Pin (' P7, Pin IN the Pin. PULL_NONE)
Wave_trig_pin=Pin (' P8, Pin. OUT_PP, Pin PULL_DOWN)

Wave_distance=0
Tim_counter=0
Flag_wave=0



# ultrasonic start
Def wave_start () :
Wave_trig_pin. Value (True)
Utime. Sleep_us (20)
Value (0)
wave_trig_pin.
# ultrasonic distance calculation
Def wave_distance_calculation () :
# global variable declaration
Global tim_counter
F # frequency of 0.2 MHZ high level time t=count * 1/f
Wave_distance tim_counter * 5 * 0.017=
# output eventually distance measuring unit (cm)
Print (' wave_distance wave_distance)


# ultrasonic data processing
Def wave_distance_process () :
Global flag_wave
If (flag_wave==0) :
Wave_start ()
If (flag_wave==2) :
Wave_distance_calculation ()
# print (" 2 ")
Flag_wave=0
Extint. The enable ()

# configuration timer
Tim=pyb. The Timer (4, prescaler=720, period=65535) # is equal to the freq=0.2 M

# external interrupt configuration
Def callback (line) :
Global flag_wave, tim_counter
# rising along the trigger processing
If (wave_echo_pin. Value ()) :
Tim. Init (prescaler=720, period=65535)
Flag_wave=1
Falling edge #
The else:
Tim. Deinit ()
Tim_counter=Tim. Counter ()
Extint. Disable ()
Flag_wave=2
# interrupt configuration
Extint=pyb. Extint (wave_echo_pin, pyb. Extint. IRQ_RISING_FALLING, pyb. Pin. PULL_DOWN, callback)


While (True) :
Wave_distance_process ()
Time. Sleep (100)

CodePudding user response:

Remember remember! Power supply should be 5 v, otherwise the above procedures, or no!!!!!!!!!!!!!!!!!!!!! Grounding also need total of!
  • Related