Jump to content

[Python] Wie Servo auf Loslassen eines Tasters 1x hin- und her bewegen lassen?


Recommended Posts

Posted

Hallo,

 

ich habe mich durch die Python-Beispiele gearbeitet und bin meinem Ziel schon recht nahe.

Nun benötige ich aber eure Hilfe…

 

Ziel:

Servo 6: Position wird durch RotaryPoti eingestellt (OK)

Servo 5: wechselt Position von -90 auf +90 bei Tasterdruck an IO4-Bricklet (OK)

Servo 4: wechselt beim loslassen des Taster für Servo 5 von -90 zu +90 und zurück zu -90 (mein Problem)

 

Hintergrund:

Fernsteuerung eines mechanischen Verschlusses (Copal 0) einer Fotokamera.

Servo 6 verstellt die Blende

Servo 5 löst den Verschluss aus

Servo 4 spannt den Verschluss erneut

 

(Anleitung des Verschlusses: Copal No. 0 nur für den Hintergrund: http://www.schneiderkreuznach.com/pdf/zubehoer/copal_0.pdf)

 

Mein bisheriges Skript:

#!/usr/bin/env python
# -*- coding: utf-8 -*-  

HOST = "localhost"
PORT = 4223
UID_SERVO = "***" # Changed to the UID of my Servo Brick
UID_POTI = "***" # Changed to the UID of my Rotary Poti Bricklet
UID_IO4 = "***" # Changed to the UID of my IO4 Bricklet

from tinkerforge.ip_connection import IPConnection
from tinkerforge.brick_servo import Servo
from tinkerforge.bricklet_rotary_poti import RotaryPoti
from tinkerforge.bricklet_io4 import IO4

# Callback function for position callback (parameter has range -150 to 150) 
def cb_position(poti_position):
    servo_position = 9000*poti_position/150 # Poti-Position: -9000/9000
    print('Set Position/Velocity: ' + str(poti_position) + '/' + str(servo_position))
    servo.set_position(6, servo_position)    

# Callback function for interrupts
def cb_interrupt(interrupt_mask, value_mask):
    print('Interrupt by: ' + str(bin(interrupt_mask)))
    print('Value: ' + str(bin(value_mask)))
    if value_mask & (1 << 0):
        servo.set_position(5, 9000) 
    else:
        servo.set_position(5, -9000) 

if __name__ == "__main__":
    ipcon = IPConnection(HOST, PORT) # Create IP connection to brickd

    servo = Servo(UID_SERVO) # Create device object
    poti = RotaryPoti(UID_POTI) # Create rotary poti device object
    io = IO4(UID_IO4) # Create input output 4 object
    ipcon.add_device(servo) # Add device to IP connection
    ipcon.add_device(poti) # Add device to IP connection
    ipcon.add_device(io) # Add device to IP connection
    
    # Enable interrupt on pin 0  
    io.set_interrupt(1 << 0)

    # Register callback for interrupts
    io.register_callback(io.CALLBACK_INTERRUPT, cb_interrupt)


    poti.set_position_callback_period(50) # set callback period to 50ms
    poti.register_callback(poti.CALLBACK_POSITION, cb_position)

    servo.set_pulse_width(6, 1000, 2000)
    servo.set_period(6, 19500)
    servo.set_acceleration(6, 0xFFFF) # Full acceleration
    servo.set_velocity(6, 0xFFFF) # Full speed
    servo.enable(6) # enable Servo No. 6

    servo.set_pulse_width(5, 1000, 2000)
    servo.set_period(5, 19500)
    servo.set_acceleration(6, 0xFFFF) # Full acceleration
    servo.set_velocity(5, 0xFFFF) # Full speed
    servo.enable(5) # enable Servo No. 5

    raw_input('Press Enter to exit\n') # Use input() in Python 3
    ipcon.destroy()

 

Wie bekomme ich es hin, dass der Servo 4 diese "hin- und her"-Bewegung beim loslassen des Tasters macht?

 

Vielen Dank im Voraus,

 

Barkeeper

 

Posted

Wenn ich mir deinen Code ansehe, dann stimme ich zu, dass du Servo 6 mit dem Poti stellst. Servo 5 fährt auf +90 wenn der Taster gedrückt wird und fährt auf -90 zurück wenn du den Taster wieder loslässt.

 

Soweit so gut, nun zu Servo 4:

 

[...]
# Callback function for interrupts
def cb_interrupt(interrupt_mask, value_mask):
    print('Interrupt by: ' + str(bin(interrupt_mask)))
    print('Value: ' + str(bin(value_mask)))
    if value_mask & (1 << 0):
        servo.set_position(5, 9000)
    else:
        servo.set_position(5, -9000) 
        servo.set_position(4, 9000)

def cb_reached(servo_num, position):
    if servo_num == 4 and position == 9000:
        servo.set_position(4, -9000)

if __name__ == "__main__":
    [...]
    servo.register_callback(servo.CALLBACK_POSITION_REACHED, cb_reached)

    servo.set_pulse_width(4, 1000, 2000)
    servo.set_period(4, 19500)
    servo.set_acceleration(4, 0xFFFF) # Full acceleration
    servo.set_velocity(4, 0xFFFF) # Full speed
    servo.enable(4) # enable Servo No. 4
    [...]

 

Loslassen des Taster bekommst du in cb_interrupt mit. Dort also Servo 4 nach +90 schicken.

 

Dass Servo 4 +90 erreicht hat kannst du im Position Reached Callback mitbekommen: cb_reached. Wenn Servo 4 +90 erreicht hat ihn nach -90 schicken.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...