#vl53l1x time of flight and motor

1 messages ยท Page 1 of 1 (latest)

marble jewel
#

That's what the while True: loop is for

You want your motor to run when the sensor value is in a range between yourstart_valueand stop_value , right?

tawdry bison
#

yes

#

this i colud be going about the hole proces in corectoley as i am quite new

#

the end gool is to make a ajustabull higth desk

marble jewel
#

I'll just call your start and stop values v1 and v2 , then you can do something like this every iteration


while True: 
   check sensor value = v

   if (v >= v1  and v <= v2) and motor_started == False:
      start_motor()
      motor_started = True 

   if  (v<v1  or v > v2) and motor_started == True: 
      stop_motor()
      motor_started = False
tawdry bison
#

i have tried that and the i am geting that sensor data corectley

marble jewel
#

Other things to consider:

  • the lower value cannot be 0, since this particular sensor doesn't detect stuff that close, it will not give accurate readings

  • your soft_stop() function doesn't actually stop the motor, it reverses it ... at the end the throttle value is almost -1, which is almost full speed in reverse direction

tawdry bison
#

so i guss im understanding the 'for i in range (100)'

#

it is not 100 loops of that function?

#

so long as the throttle is 1 at the start of the function it reduce by .01 fore each loop

marble jewel
#

you're doing 1-i so basically you end up with -0.99 at the end, which is just full speed in reverse

#

you probably want range(100, -1, -1) to stop, and i, not 1-i

tawdry bison
#

ok i think i understand that in the loung run i plan to have it do a soft stop but only down to like .05 untill it reatches the target hight then full stop

#

so i colud do range (95 ,-1 , -1)

marble jewel
#

range(100, 0, -1) will go from full, to just before 0 ... or even range(100, 5,-1) to keep the motor running really really slowly

#

anyway, this is all adaptable to your needs when you get it going ๐Ÿ™‚

tawdry bison
#

sweet

marble jewel
#

I'm pretty sure the problem in your current code is the infinite loop when you check the sensor value, that's why I tried to simplify the logic of your code

tawdry bison
#

so the def tof_Range(): while call_range == True: global call_range, value if TOF.data_ready: print("Distance: {} cm".format(TOF.distance)) value = TOF.distance TOF.clear_interrupt() call_range = False else: pass

#

so it sholud onley enact the loop when the call_range varible is set to True

marble jewel
#

sure

tawdry bison
#

and sincei have in the function to set call_range = False it sholud end the loop?

marble jewel
#

yep

tawdry bison
#

ok so that sholdu solve the infinit loop maby not in teh best way thow

marble jewel
#

as long as it works, all good

you don't need global call_range, value there tho, they were already declared above the function, right?

tawdry bison
#

yes

tawdry bison
#

so i tried removing he global value, call_range and it sead i was refrencing them before assignment and made some updates teh code get to the print('ranging") and dos not proceed

marble jewel
#

the first motor_start = False needs to be outside the while True, you can put it at the beginning with the other variables you're initializing

#

V1 and V2 can also be there, since their value doesn't change

#

and kit.motor1.throttle = i / 100 in soft_stop()