3 deleted 16 characters in body
source | link
import RPi.GPIO as GPIO
import socket
import os
import re
#----------------------------------------------------------

## TODO: Port network information on background thread.


class CarController:

    """
    @param port: port for connectivity
    """
    def __init__(self,port):
        ## Intialize parameters of communication.
        self.command= os.popen('ifconfig wlp6s0 | grep "inet\ addr" | cut -d: -f2 | cut -d" " -f1') #Works for WiFi only.
        self.TCP_IP = self.command.read()[0:-1]
        self.TCP_PORT = port
        self.BUFFER_SIZE = 8
        self.pattern = re.compile('-*[0-9]+\|[0-9]+$')

        ##Intialize the connection
        self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.sock.bind((self.TCP_IP, self.TCP_PORT))
        self.sock.listen(True)
        print 'Connect to: %s:%d'%(self.TCP_IP,self.TCP_PORT)
        #Blocking call.
        self.conn, self.addr = self.sock.accept()
        print 'Connected with: ',self.addr

    """
    @param
    @return speed,angle
    """
    def getParameters(self):
        data = self.conn.recv(self.BUFFER_SIZE)
        if(data == 'stop\n'):
            self.conn.close()
            return -1,-1
        if(self.pattern.match(data) != None):
            angle,speed=data.split('|')
            return int(angle),int(speed);
        else:
            print 'Frame dropped'
            return self.getParameters()
controller = CarController(14002)
speed,angle,dc = 0,0,0
#print "The angle is: %d and speed is: %d"%(angle,speed)
 #----------------------------------------------------------

#----------------For driving the car-----------------------
GPIO.setmode(GPIO.BOARD)
GPIO.setup(13,GPIO.OUT)
servo= GPIO.PWM(13,50)
servo.start(65)

while 1:
    angle,speed = controller.getParameters()
    DutyCycle=dc=((angle/2)+65)
    servo.ChangeDutyCycle(DutyCycledc)
    print "The angle is: %d ,speed is: %d and DutyCycle: %d"%(angle,speed,DutyCycledc)
import RPi.GPIO as GPIO
import socket
import os
import re
#----------------------------------------------------------

## TODO: Port network information on background thread.


class CarController:

    """
    @param port: port for connectivity
    """
    def __init__(self,port):
        ## Intialize parameters of communication.
        self.command= os.popen('ifconfig wlp6s0 | grep "inet\ addr" | cut -d: -f2 | cut -d" " -f1') #Works for WiFi only.
        self.TCP_IP = self.command.read()[0:-1]
        self.TCP_PORT = port
        self.BUFFER_SIZE = 8
        self.pattern = re.compile('-*[0-9]+\|[0-9]+$')

        ##Intialize the connection
        self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.sock.bind((self.TCP_IP, self.TCP_PORT))
        self.sock.listen(True)
        print 'Connect to: %s:%d'%(self.TCP_IP,self.TCP_PORT)
        #Blocking call.
        self.conn, self.addr = self.sock.accept()
        print 'Connected with: ',self.addr

    """
    @param
    @return speed,angle
    """
    def getParameters(self):
        data = self.conn.recv(self.BUFFER_SIZE)
        if(data == 'stop\n'):
            self.conn.close()
            return -1,-1
        if(self.pattern.match(data) != None):
            angle,speed=data.split('|')
            return int(angle),int(speed);
        else:
            print 'Frame dropped'
            return self.getParameters()
controller = CarController(14002)
speed,angle = 0,0
#print "The angle is: %d and speed is: %d"%(angle,speed)
 #----------------------------------------------------------

#----------------For driving the car-----------------------
GPIO.setmode(GPIO.BOARD)
GPIO.setup(13,GPIO.OUT)
servo= GPIO.PWM(13,50)
servo.start(65)

while 1:
    angle,speed = controller.getParameters()
    DutyCycle=((angle/2)+65)
    servo.ChangeDutyCycle(DutyCycle)
    print "The angle is: %d ,speed is: %d and DutyCycle: %d"%(angle,speed,DutyCycle)
import RPi.GPIO as GPIO
import socket
import os
import re
#----------------------------------------------------------

## TODO: Port network information on background thread.


class CarController:

    """
    @param port: port for connectivity
    """
    def __init__(self,port):
        ## Intialize parameters of communication.
        self.command= os.popen('ifconfig wlp6s0 | grep "inet\ addr" | cut -d: -f2 | cut -d" " -f1') #Works for WiFi only.
        self.TCP_IP = self.command.read()[0:-1]
        self.TCP_PORT = port
        self.BUFFER_SIZE = 8
        self.pattern = re.compile('-*[0-9]+\|[0-9]+$')

        ##Intialize the connection
        self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.sock.bind((self.TCP_IP, self.TCP_PORT))
        self.sock.listen(True)
        print 'Connect to: %s:%d'%(self.TCP_IP,self.TCP_PORT)
        #Blocking call.
        self.conn, self.addr = self.sock.accept()
        print 'Connected with: ',self.addr

    """
    @param
    @return speed,angle
    """
    def getParameters(self):
        data = self.conn.recv(self.BUFFER_SIZE)
        if(data == 'stop\n'):
            self.conn.close()
            return -1,-1
        if(self.pattern.match(data) != None):
            angle,speed=data.split('|')
            return int(angle),int(speed);
        else:
            print 'Frame dropped'
            return self.getParameters()
controller = CarController(14002)
speed,angle,dc = 0,0,0
#print "The angle is: %d and speed is: %d"%(angle,speed)
 #----------------------------------------------------------

#----------------For driving the car-----------------------
GPIO.setmode(GPIO.BOARD)
GPIO.setup(13,GPIO.OUT)
servo= GPIO.PWM(13,50)
servo.start(65)

while 1:
    angle,speed = controller.getParameters()
    dc=((angle/2)+65)
    servo.ChangeDutyCycle(dc)
    print "The angle is: %d ,speed is: %d and DutyCycle: %d"%(angle,speed,dc)
2 edited tags
| link
1
source | link

Servo angle control using Rpi and android App

I am trying to control the angle of a servo motor via an Android app that I built, installed on my phone. My phone sends the angle data (i.e. -90 to +90 degrees) to the raspberry pi via a TCP socket. I have identified my desired duty cycles i.e. 50 to 80. Using linear graph theory I derived an equation which takes angle as input and gives the desired duty cycle.

The problem I am facing is that the servo turns on one side and stays there. Although the print statement in th while loop shows that the DutyCycle variable is changing as I rotate my mobile. I have even tried to add a little time delay in the loop, still the problem persists

The code below demonstrates my methodology:

import RPi.GPIO as GPIO
import socket
import os
import re
#----------------------------------------------------------

## TODO: Port network information on background thread.


class CarController:

    """
    @param port: port for connectivity
    """
    def __init__(self,port):
        ## Intialize parameters of communication.
        self.command= os.popen('ifconfig wlp6s0 | grep "inet\ addr" | cut -d: -f2 | cut -d" " -f1') #Works for WiFi only.
        self.TCP_IP = self.command.read()[0:-1]
        self.TCP_PORT = port
        self.BUFFER_SIZE = 8
        self.pattern = re.compile('-*[0-9]+\|[0-9]+$')

        ##Intialize the connection
        self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.sock.bind((self.TCP_IP, self.TCP_PORT))
        self.sock.listen(True)
        print 'Connect to: %s:%d'%(self.TCP_IP,self.TCP_PORT)
        #Blocking call.
        self.conn, self.addr = self.sock.accept()
        print 'Connected with: ',self.addr

    """
    @param
    @return speed,angle
    """
    def getParameters(self):
        data = self.conn.recv(self.BUFFER_SIZE)
        if(data == 'stop\n'):
            self.conn.close()
            return -1,-1
        if(self.pattern.match(data) != None):
            angle,speed=data.split('|')
            return int(angle),int(speed);
        else:
            print 'Frame dropped'
            return self.getParameters()
controller = CarController(14002)
speed,angle = 0,0
#print "The angle is: %d and speed is: %d"%(angle,speed)
 #----------------------------------------------------------

#----------------For driving the car-----------------------
GPIO.setmode(GPIO.BOARD)
GPIO.setup(13,GPIO.OUT)
servo= GPIO.PWM(13,50)
servo.start(65)

while 1:
    angle,speed = controller.getParameters()
    DutyCycle=((angle/2)+65)
    servo.ChangeDutyCycle(DutyCycle)
    print "The angle is: %d ,speed is: %d and DutyCycle: %d"%(angle,speed,DutyCycle)