Tags

, , , ,

I would like to control the turning of the servo motor from the web app. The servo motor has a camera mounted on it. In this way, I can monitor the surrounding environment from the camera. But it is only limited to 180 degree. It would be nice to have both tilt and pan control of the camera (may be see this: http://youtu.be/4AwF_YYbORM). That would require 2 servo motors and it’s beyond my scope of this current project. Perhaps I could explore it in times to come.

The idea was to set and keep the value for the servo PWM in a variable declared outside of the Python function. So, when the left and right button is clicked, the servo would turn by a certain amount. jQuery was used to trigger the function by posting a direction to it, as such:

$('#left_button').click(function(){
	$.post('/turnCamera', {direction:"left"}).done(function (reply) {
		$('#camerapos').empty().append(reply);
		alert("left button clicked");});					
});
$('#right_button').click(function(){
	$.post('/turnCamera', {direction:"right"}).done(function (reply) {
		$('#camerapos').empty().append(reply);
		alert("right button clicked");});
});

However the append and alert function was not seen at the main page but can be seen at :8080.
The code in the python is as follow:

    # define a variable m outside the function but inside the class
    def __init__(self):
        self.m = 1500

    @cherrypy.expose
    def turnCamera (self, **data):

        import pigpio
        import time
 
        step= 100   #set a step size of 100
        servos=[4] #GPIO number
        pigpio.start()
        key = data['direction']

        if (self.m >= 500 and self.m <= 2500): #check if it's within the limit range for the servo
            if key =="left":
                self.m +=step
                 
            elif key =="right":
                self.m -=step

            pigpio.set_servo_pulsewidth(servos[0], self.m) 
            servostat= "Position of servo: %s" % self.m
            print servostat
            time.sleep(1)

        pigpio.stop()

        return servostat

See the output here:

Showing servo's position

Showing servo’s position

Showing servo's position

Showing servo’s position


See the demo video here:

NOTE: Joo Kai (the knowledgeable tech guy in the lab) said, to reduce the buzzing noise from the servo, i should send command to it more frequently.

Advertisements