Python Forum
How to use servo motor with TFMini Distance Sensor in python script?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to use servo motor with TFMini Distance Sensor in python script?
#1
I am using Adafruit TFmini distance sensor, with servo motor. I want the servo motor to move when distance sensor senses a certain distance. My code makes the servo motor move continuously, like crazy.

Credits: TfMini distance sensor code is taken from tfmini GitHub, and servo motor code is from raspberry pi tutorials.


# -*- coding: utf-8 -*
import serial
import math
import time
import RPi.GPIO as GPIO





servoPIN = 18
GPIO.setmode(GPIO.BCM)
GPIO.setup(servoPIN, GPIO.OUT)


fire_gun = GPIO.PWM(servoPIN, 50)   #GPIO 5 for PWM with 50Hz
fire_gun.start(2.5) #Initialization



"""
def gunRight():
    #fire_gun = GPIO.PWM(servoPIN, 50)  #GPIO 5 for PWM with 50Hz
    #fire_gun.start(2.5)    #Initialization

    print "Shot Fire The First Net Gun"
    fire_gun.ChangeDutyCycle(4.9)
    time.sleep(0.4)
    fire_gun.ChangeDutyCycle(7.2)
    time.sleep(0.4)
    print "Servo Motor Back To Rest Position"
    fire_gun.stop()
"""




ser = serial.Serial("/dev/ttyUSB0", 115200)

def getTFminiData():
    while True:
        count = ser.in_waiting
        if count > 8:
            recv = ser.read(9)
            ser.reset_input_buffer()
            if recv[0] == 'Y' and recv[1] == 'Y': # 0x59 is 'Y'
                low = int(recv[2].encode('hex'), 16)
                high = int(recv[3].encode('hex'), 16)
                distance = low + high * 256
                #My unit conversion starts here
                dist = float(distance) /100 
                print(distance)
                print "Distance to object is:",dist,"Meters"
                #GPIO.setup(TRIG, GPIO.OUT)
                #GPIO.setup(ECHO, GPIO.IN)

                #if dist <= 0.70:
                    #GPIO.output(5, True)   





try:
    if ser.is_open == False:
       ser.open()
       for i in range(500):
           getTFminiData()
           if dist <= 0.70:
          fire_gun.ChangeDutyCycle(4.9)
              time.sleep(0.4)
              GPIO.cleanup()


except KeyboardInterrupt:   # Ctrl+C
    if ser != None:
        #GPIO.cleanup()
    ser.close()
Reply


Messages In This Thread
How to use servo motor with TFMini Distance Sensor in python script? - by programerguy - Dec-04-2018, 02:41 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Is there a *.bat DOS batch script to *.py Python Script converter? pstein 3 3,165 Jun-29-2023, 11:57 AM
Last Post: gologica
  Trying to Get Arduino sensor data over to excel using Python. eh5713 1 1,686 Dec-01-2022, 01:52 PM
Last Post: deanhystad
  Stepper motor/Easy Driver/HELP Harney 1 1,880 Jul-31-2021, 08:05 AM
Last Post: Harney
  python 3 raspberry pi 4 dual control motor programming problem yome 0 1,970 Mar-21-2021, 05:17 PM
Last Post: yome
  stepper motor and servo key control cel 4 2,589 Jul-27-2020, 06:26 AM
Last Post: DPaul
  Stepper Motor key control cel 2 1,896 Jul-25-2020, 12:19 AM
Last Post: cel
  EOF error using stepper motor cel 1 1,704 Jul-24-2020, 01:27 PM
Last Post: Larz60+
  How to kill a bash script running as root from a python script? jc_lafleur 4 5,863 Jun-26-2020, 10:50 PM
Last Post: jc_lafleur
  crontab on RHEL7 not calling python script wrapped in shell script benthomson 1 2,283 May-28-2020, 05:27 PM
Last Post: micseydel
  Visualize Geo Map/Calculate distance zarize 1 1,883 Dec-05-2019, 08:36 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020