benbusby / piro

A Raspberry Pi security camera rover
MIT License
100 stars 17 forks source link

3rd Servo? #10

Closed pikkaroni closed 5 years ago

pikkaroni commented 5 years ago

Will it be possible to have a third servo controlled through the app? I want to have a servo activating a laserpointer while you press the up key on the keyboard in the webapp, so it will activate while the RazTot is moving forward. It is working now on GPIO pin 17, and running on the 3v output, but I wanted to be able to run it simultaneously with the wheels.

So my question is: What do I have to change in the code to run GPIO pin 27 as if it was 22 and 17, but all at the same time? I will look through the code and see if I can figure it out, but if you have time to help a coding noob it would be awesome :)

benbusby commented 5 years ago

That should actually be a pretty simple change to get that working. The file you'll need to change is app/routes.py - in that file there are two variables defined sort of near the top of the file:

# GPIO pins for servos
SERVO_L = 17
SERVO_R = 22

which you can change to something like

# GPIO pins for servos
SERVO_L = 17
SERVO_R = 22
SERVO_U = 27

and then towards the bottom of the file (around line 250) is where the websocket function is for sending the pwm signals to the servos:

@socketio.on('move', namespace='/raztot')
@authenticated_only
def move(data):
    '''
    Assuming mirrored motor setup for either side of the raztot, one side should turn
    clockwise and the other should turn counterclockwise to move forward, and the 
    opposite for reversing. Turning is accomplished by moving the opposite wheel (turning left moves the right wheel and uses the left wheel as a pivot, and vice versa).
    '''
    if data is None:
        pi.set_servo_pulsewidth(SERVO_L, 0)
        pi.set_servo_pulsewidth(SERVO_R, 0)
    elif data.get('left') or data.get('right'):
        pi.set_servo_pulsewidth(SERVO_L, data.get('left') * 1000)
        pi.set_servo_pulsewidth(SERVO_R, data.get('right') * 2000)
    else:
        pi.set_servo_pulsewidth(SERVO_L, 2000 if data.get(
            'up') else 1000 * data.get('down'))
        pi.set_servo_pulsewidth(SERVO_R, 1000 if data.get(
'up') else 2000 * data.get('down'))

That final "else" block is where forward/backward controls are defined for the 17/22 pin servos, so you can modify that block to activate SERVO_U. You'll also have to deactivate SERVO_U for all other commands as well. In the end it should all look something like this (untested):

@socketio.on('move', namespace='/raztot')
@authenticated_only
def move(data):
    '''
    Assuming mirrored motor setup for either side of the raztot, one side should turn
    clockwise and the other should turn counterclockwise to move forward, and the 
    opposite for reversing. Turning is accomplished by moving the opposite wheel (turning left moves the right wheel and uses the left wheel as a pivot, and vice versa).
    '''
    if data is None:
        pi.set_servo_pulsewidth(SERVO_L, 0)
        pi.set_servo_pulsewidth(SERVO_R, 0)
        pi.set_servo_pulsewidth(SERVO_U, 0)
    elif data.get('left') or data.get('right'):
        pi.set_servo_pulsewidth(SERVO_L, data.get('left') * 1000)
        pi.set_servo_pulsewidth(SERVO_R, data.get('right') * 2000)
        pi.set_servo_pulsewidth(SERVO_U, 0)
    else:
        pi.set_servo_pulsewidth(SERVO_L, 2000 if data.get(
            'up') else 1000 * data.get('down'))
        pi.set_servo_pulsewidth(SERVO_R, 1000 if data.get(
            'up') else 2000 * data.get('down'))
        pi.set_servo_pulsewidth(SERVO_U, 2000 if data.get(
            'up') else 0)

With that block updated it will activate SERVO_U when the forward arrow key is pressed, but will zero it anytime any other key is pressed (or released). I haven't tested it but that should achieve what you're looking for. Let me know if you need any clarification on anything here, I'm happy to help.

pikkaroni commented 5 years ago

I managed to get it working so that it turns off when i press the down key, great success!

I changed

pi.set_servo_pulsewidth(SERVO_U, 2000 if data.get( 'up') else 0) to

pi.set_servo_pulsewidth(SERVO_U, 2000 if data.get( 'up') else 1000 * data.get('down'))

pikkaroni commented 5 years ago

Here is a video of the setup so far, I am missing one servo for the right wheel, since one of my drivermotors were fried out of the box, but when it arrives tomorrow I will put it together and upload some pictures! Here is the Darth Wader lazor working(dot appears on lp-player): ezgif com-video-to-gif

benbusby commented 5 years ago

Nice, that's awesome.

pikkaroni commented 5 years ago

I made an Imgur post: https://imgur.com/a/4FK60F2

Thank you for the help, awesome project!

benbusby commented 5 years ago

Woah! That is so cool! I really like the lego chassis you built, that's a great idea. Nice job!