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
  Trying to Get Arduino sensor data over to excel using Python. eh5713 1 4,304 Dec-01-2022, 01:52 PM
Last Post: deanhystad
  Controller Buzzer as Distance Decreases barkster 6 4,580 Nov-01-2021, 03:26 PM
Last Post: barkster
  Stepper motor/Easy Driver/HELP Harney 1 3,413 Jul-31-2021, 08:05 AM
Last Post: Harney
  Averaging sensor results igorv1234 1 2,774 Jun-17-2021, 04:08 PM
Last Post: topfox
  python 3 raspberry pi 4 dual control motor programming problem yome 0 3,132 Mar-21-2021, 05:17 PM
Last Post: yome
  broadcasting euclidean distance error glennford49 1 5,925 Nov-19-2020, 07:01 PM
Last Post: Gribouillis
  How to kill a bash script running as root from a python script? jc_lafleur 4 9,946 Jun-26-2020, 10:50 PM
Last Post: jc_lafleur
  crontab on RHEL7 not calling python script wrapped in shell script benthomson 1 3,632 May-28-2020, 05:27 PM
Last Post: micseydel
  smallest Cosine distance in Graph vino689 3 3,826 Jan-12-2020, 08:06 AM
Last Post: rmspacedashrf
  Visualize Geo Map/Calculate distance zarize 1 2,924 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