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
#2
This package does half of what you want: https://github.com/adafruit/Adafruit_Cir...hon_TFmini
perhaps you can easily figure out how to drive servo motor using this for sensor.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  stepper motor and servo key control cel 4 425 Jul-27-2020, 06:26 AM
Last Post: DPaul
  Stepper Motor key control cel 2 362 Jul-25-2020, 12:19 AM
Last Post: cel
  EOF error using stepper motor cel 1 386 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 675 Jun-26-2020, 10:50 PM
Last Post: jc_lafleur
  crontab on RHEL7 not calling python script wrapped in shell script benthomson 1 505 May-28-2020, 05:27 PM
Last Post: micseydel
  Audio and servo at the same time cgreenwell 4 844 Jul-04-2019, 01:41 AM
Last Post: Larz60+
  Package python script which has different libraries as a single executable or script tej7gandhi 1 862 May-11-2019, 08:12 PM
Last Post: keames
  Control 2 stepper motor simultaneously jihene 2 1,249 May-08-2019, 05:27 PM
Last Post: DeaD_EyE
  Code Wireless Temperature sensor and send sensor readings to google sheet jenkins43 0 934 Nov-29-2018, 12:44 PM
Last Post: jenkins43
  RPi with IR Sensor pimlicosnail 1 932 Jun-24-2018, 08:53 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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