guild icon
Toit
#Connecting Ultrasonic sensor to trigger Servo motor
Thread channel in help
Deryc Dark
Deryc Dark 10/06/2022 09:16 AM
import gpio
import i2c
import gpio.pwm

TRIGGER ::= 21
ECHO ::= 22

measure_distance trigger echo:
trigger_start := Time.monotonic_us
trigger.set 1
while Time.monotonic_us < trigger_start + 10:
// Do nothing while waiting for the 10us.
trigger.set 0

while echo.get != 1: null
echo_start := Time.monotonic_us
while echo.get == 1: null

echo_end := Time.monotonic_us
diff := echo_end - echo_start
return diff / 58

main:
trigger := gpio.Pin TRIGGER --output
echo := gpio.Pin ECHO --input
while true:
print "measured $(measure_distance trigger echo)cm"
sleep --ms=50

if measure_distance trigger echo = 20:
print "DOOR OPEN"

else:
print "DOOR LOCKED"

servo := gpio.Pin 14

generator := pwm.Pwm --frequency=50

channel := generator.start servo --duty_factor=0.075

sleep --ms=10
// https://github.com/toitlang/toit/issues/518


// Max angle.

channel.set_duty_factor 0.125

sleep --ms=2000


// Min angle.

channel.set_duty_factor 0.025

sleep --ms=200
import gpio import gpio.pwm main: led := gpio.Pin 5 generator := pwm.Pwm --frequency=400 channel2 := generator.start (gpio.Pin 12) channel3 := generator.start (gpio.Pin 13) channel4 := generator.st...
floitsch
floitsch 10/06/2022 09:17 AM
Please use tripple backticks to make the code look nicer:
text in tripple ` looks like code.
floitsch
floitsch 10/06/2022 09:18 AM
floitsch
floitsch 10/06/2022 09:18 AM
You should be able to edit your post.
floitsch
floitsch 10/06/2022 09:19 AM
Now to making that program work:
- do you read the distance correctly? You have a print there. Does it show the distance correctly?
If the DOOR OPEN and DOOR LOCKED are shown correctly, then you are almost there.
floitsch
floitsch 10/06/2022 09:20 AM
You just need to use that information for an angle (== duty factor) of the servo.
floitsch
floitsch 10/06/2022 09:20 AM
Wait.
I can see that the code won't work.
floitsch
floitsch 10/06/2022 09:21 AM
measure_distance trigger echo = 20 is an assignment, not a comparison.
floitsch
floitsch 10/06/2022 09:21 AM
You need to write (measure_distance trigger echo) == 20
Deryc Dark
Deryc Dark 10/06/2022 09:31 AM
we are also getting an error when we jag monitor, because it says failed due to bad authentication, so it wont even print
Deryc Dark
Deryc Dark 10/06/2022 09:34 AM
this is the new error we are receiving
floitsch
floitsch 10/06/2022 09:35 AM
Jaguar seems to work just fine.
floitsch
floitsch 10/06/2022 09:35 AM
However, the program you are writing still has errors, and can't be compiled yet.
floitsch
floitsch 10/06/2022 09:36 AM
After the condition (== 20) you are now missing a :.
Deryc Dark
Deryc Dark 10/06/2022 09:36 AM
its supposed to an automatic door system
floitsch
floitsch 10/06/2022 09:36 AM
I understand.
floitsch
floitsch 10/06/2022 09:39 AM
Try with the following code.
I didn't actually test it, so there might still be bugs in there.
main: trigger := gpio.Pin TRIGGER --output echo := gpio.Pin ECHO --input servo := gpio.Pin 14 generator := pwm.Pwm --frequency=50 channel := generator.start servo --duty_factor=0.075 while true: distance := measure_distance trigger echo print "measured $(distance)cm" if distance <= 200: print "DOOR OPEN" channel.set_duty_factor 0.125 // Max angle. else: print "DOOR LOCKED" channel.set_duty_factor 0.025 // Min angle. // The sleep can be lower (or even be removed), but while // we are printing, we don't want the loop to run too // quickly. sleep --ms=500
(edited)
Deryc Dark
Deryc Dark 10/06/2022 09:39 AM
now it compiling without errors but it does not seem to trigger the hardware
floitsch
floitsch 10/06/2022 09:40 AM
Start your main with a print "starting".
floitsch
floitsch 10/06/2022 09:40 AM
This way you know if the program is arriving correctly.
floitsch
floitsch 10/06/2022 09:40 AM
Either way: what's the output of the console/monitor?(edited)
Deryc Dark
Deryc Dark 10/06/2022 09:44 AM
thats the result when i jag monitor
floitsch
floitsch 10/06/2022 09:45 AM
ok. But what happens now, if you take another terminal and do jag run (while jag monitor is still running).
Deryc Dark
Deryc Dark 10/06/2022 09:52 AM
when i do that, it prints starting
Deryc Dark
Deryc Dark 10/06/2022 09:52 AM
and thats all
floitsch
floitsch 10/06/2022 10:02 AM
So it only prints the first line of your main ?
floitsch
floitsch 10/06/2022 10:02 AM
where you have print "starting" ?
Deryc Dark
Deryc Dark 10/06/2022 10:06 AM
nope actually its reads the whole code but it runs it once and if the distance is not wat we specified it prints door locked yet we want it to loop until it reads the specified distance and then open the door and after it locks it back.
floitsch
floitsch 10/06/2022 10:06 AM
Can you show me the code you are using now?
Deryc Dark
Deryc Dark 10/06/2022 10:10 AM
import gpio
import i2c
import gpio.pwm

TRIGGER ::= 21
ECHO ::= 22

measure_distance trigger echo:
trigger_start := Time.monotonic_us
trigger.set 1
while Time.monotonic_us < trigger_start + 10:
// Do nothing while waiting for the 10us.
trigger.set 0

while echo.get != 1: null
echo_start := Time.monotonic_us
while echo.get == 1: null

echo_end := Time.monotonic_us
diff := echo_end - echo_start
return diff / 58

main:
print "starting"
trigger := gpio.Pin TRIGGER --output
echo := gpio.Pin ECHO --input
while true:
print "measured $(measure_distance trigger echo)cm"
sleep --ms=50

if (measure_distance trigger echo) == 200:
print "DOOR OPEN"

else:
print "DOOR LOCKED"

servo := gpio.Pin 14

generator := pwm.Pwm --frequency=50

channel := generator.start servo --duty_factor=0.075

sleep --ms=10

// Max angle.

channel.set_duty_factor 0.125

sleep --ms=2000


// Min angle.

channel.set_duty_factor 0.025

sleep --ms=200
Deryc Dark
Deryc Dark 10/06/2022 10:10 AM
and its also not triggering the servo motor
floitsch
floitsch 10/06/2022 10:10 AM
Please send code in triple quotes.
floitsch
floitsch 10/06/2022 10:11 AM
you can edit your post to put the code into quotes.
floitsch
floitsch 10/06/2022 10:11 AM
Otherwise it's hard to see indentation.
Deryc Dark
Deryc Dark 10/06/2022 10:12 AM
"""import gpio
import i2c
import gpio.pwm

TRIGGER ::= 21
ECHO ::= 22

measure_distance trigger echo:
trigger_start := Time.monotonic_us
trigger.set 1
while Time.monotonic_us < trigger_start + 10:
// Do nothing while waiting for the 10us.
trigger.set 0

while echo.get != 1: null
echo_start := Time.monotonic_us
while echo.get == 1: null

echo_end := Time.monotonic_us
diff := echo_end - echo_start
return diff / 58

main:
print "starting"
trigger := gpio.Pin TRIGGER --output
echo := gpio.Pin ECHO --input
while true:
print "measured $(measure_distance trigger echo)cm"
sleep --ms=50

if (measure_distance trigger echo) == 200:
print "DOOR OPEN"

else:
print "DOOR LOCKED"

servo := gpio.Pin 14

generator := pwm.Pwm --frequency=50

channel := generator.start servo --duty_factor=0.075

sleep --ms=10
// https://github.com/toitlang/toit/issues/518


// Max angle.

channel.set_duty_factor 0.125

sleep --ms=2000


// Min angle.

channel.set_duty_factor 0.025

sleep --ms=200"""
import gpio import gpio.pwm main: led := gpio.Pin 5 generator := pwm.Pwm --frequency=400 channel2 := generator.start (gpio.Pin 12) channel3 := generator.start (gpio.Pin 13) channel4 := generator.st...
floitsch
floitsch 10/06/2022 10:12 AM
You have to use backticks:
` (three times)
I sent a screenshot earlier.
(edited)
Deryc Dark
Deryc Dark 10/06/2022 10:19 AM
import gpio import i2c import gpio.pwm TRIGGER ::= 21 ECHO ::= 22 measure_distance trigger echo: trigger_start := Time.monotonic_us trigger.set 1 while Time.monotonic_us < trigger_start + 10: // Do nothing while waiting for the 10us. trigger.set 0 while echo.get != 1: null echo_start := Time.monotonic_us while echo.get == 1: null echo_end := Time.monotonic_us diff := echo_end - echo_start return diff / 58 main: print "starting" trigger := gpio.Pin TRIGGER --output echo := gpio.Pin ECHO --input while true: print "measured $(measure_distance trigger echo)cm" sleep --ms=50 if (measure_distance trigger echo) == 200: print "DOOR OPEN" else: print "DOOR LOCKED" servo := gpio.Pin 14 generator := pwm.Pwm --frequency=50 channel := generator.start servo --duty_factor=0.075 sleep --ms=10 // https://github.com/toitlang/toit/issues/518 // Max angle. channel.set_duty_factor 0.125 sleep --ms=2000 // Min angle. channel.set_duty_factor 0.025 sleep --ms=200
Deryc Dark
Deryc Dark 10/06/2022 10:19 AM
finally
floitsch
floitsch 10/06/2022 10:20 AM
:🙂:
floitsch
floitsch 10/06/2022 10:21 AM
That code should run the distance measurement every 50ms and print "DOOR OPEN" or "DOOR LOCKED" continuously.
floitsch
floitsch 10/06/2022 10:21 AM
depending on whether the distance is more or less than 200cm.
floitsch
floitsch 10/06/2022 10:21 AM
Is that correct?
floitsch
floitsch 10/06/2022 10:22 AM
Should also print the measured distance before the "DOOR ..." line.
Deryc Dark
Deryc Dark 10/06/2022 10:24 AM
yes but we want the servo motor to move when its open and close when its locked
floitsch
floitsch 10/06/2022 10:24 AM
I understand.
Deryc Dark
Deryc Dark 10/06/2022 10:24 AM
the code is just measuring and printing
floitsch
floitsch 10/06/2022 10:24 AM
That's because the rest (servo code) is outside the while true loop.
floitsch
floitsch 10/06/2022 10:24 AM
and is thus never reached.
floitsch
floitsch 10/06/2022 10:25 AM
You need to put the servo code into the while body.
floitsch
floitsch 10/06/2022 10:25 AM
Deryc Dark
Deryc Dark 10/06/2022 10:35 AM
now we want if the distance criteria is not met to open the door, we want the sensor to stay locked but the sensor to keep measuring until an object is with in range to trigger an opening
floitsch
floitsch 10/06/2022 10:36 AM
it should do that already.
floitsch
floitsch 10/06/2022 10:36 AM
It's continuously measuring the distance and updating accordingly.
floitsch
floitsch 10/06/2022 10:37 AM
You could introduce a boolean state, to only set the generator's duty_factor when the state changes, but it doesn't really cost to set the factor to the same value it already is.
floitsch
floitsch 10/06/2022 10:37 AM
That said: if you keep a boolean variable, you could only print "DOOR OPEN" or "DOOR LOCKED" when the state changes.
floitsch
floitsch 10/06/2022 10:38 AM
Also: you probably want to avoid to be in a state where the distance is just about 200cm and changes very frequently.
floitsch
floitsch 10/06/2022 10:39 AM
So you should maybe open the door when something is closer than 190cm, but only close it when you measure something more than 210cm away.
Deryc Dark
Deryc Dark 10/06/2022 10:42 AM
but the door doesnt close after it opens, we want it to close after 180ms
Deryc Dark
Deryc Dark 10/06/2022 10:43 AM
how can it get closed when something is 210cm away
floitsch
floitsch 10/06/2022 10:43 AM
Just to summarize:
floitsch
floitsch 10/06/2022 10:44 AM
the code is running and continuously printing "DOOR OPEN" or "DOOR LOCKED".
floitsch
floitsch 10/06/2022 10:44 AM
But the servo is not changing according to the state?
Deryc Dark
Deryc Dark 10/06/2022 10:46 AM
the code is running perfectly, well the servo opens but does not close back after the object is through
floitsch
floitsch 10/06/2022 10:46 AM
So when the code prints "DOOR LOCKED" the servo does not move?
Deryc Dark
Deryc Dark 10/06/2022 10:48 AM
yes
floitsch
floitsch 10/06/2022 10:48 AM
please show me again the code you are using.
Deryc Dark
Deryc Dark 10/06/2022 10:49 AM
sorry no, the code prints open but does not close back after that
floitsch
floitsch 10/06/2022 10:49 AM
What about the distance measurement?
floitsch
floitsch 10/06/2022 10:50 AM
Do you see the measured distance change?
Deryc Dark
Deryc Dark 10/06/2022 10:53 AM
yes
Deryc Dark
Deryc Dark 10/06/2022 10:53 AM
somewhere its now showing brown out detector triggered
floitsch
floitsch 10/06/2022 10:54 AM
That means that not enough power is provided, or that you have a short-circuit.
floitsch
floitsch 10/06/2022 10:54 AM
can be a bad USB cable too.
Deryc Dark
Deryc Dark 10/06/2022 10:54 AM
okay
floitsch
floitsch 10/06/2022 10:55 AM
So you see the distance that is measured going from above 200cm to below 200cm, but it's still printing "DOOR OPEN" ?
floitsch
floitsch 10/06/2022 10:55 AM
Another reason for brown-out is if you set the duty-factor to a value that blocks the servo motor.
floitsch
floitsch 10/06/2022 10:56 AM
The servo can only work in a certain range, and if you use values that are out of that range it will consume too much current.
Deryc Dark
Deryc Dark 10/06/2022 11:06 AM
its now working well sir
Deryc Dark
Deryc Dark 10/06/2022 11:06 AM
thanks alot
👍1
80 messages in total