Ejemplo n.º 1
0
SERVO_MIN = 150  # Minimum rotation value for the servo, should be -90 degrees of rotation.
SERVO_MAX = 600  # Maximum rotation value for the servo, should be 90 degrees of rotation.
SERVO_CENTER = 200  # Center value for the servo, should be 0 degrees of rotation.
MQTT_SERVER = 'localhost'  # MQTT server to connect to for receiving commands.
MQTT_PORT = 1883  # Port for the MQTT server.
LASER_GPIO = 23  # GPIO pin connected to a transistor that controls the laser on/off.
# MQTT topics used for controlling the laser.
TOPIC_TARGET = 'catlaser/target'
TOPIC_RELATIVE = 'catlaser/relative'
TOPIC_PATH = 'catlaser/path'
TOPIC_LASER = 'catlaser/laser'

# Create servo and laser movement model.
servos = servos.Servos(SERVO_I2C_ADDRESS, SERVO_XAXIS_CHANNEL,
                       SERVO_YAXIS_CHANNEL, SERVO_PWM_FREQ)
model = model.LaserModel(servos, SERVO_MIN, SERVO_MAX, SERVO_CENTER,
                         LASER_GPIO)


# MQTT callbacks:
def on_connect(client, userdata, flags, rc):
    # Called when connected to the MQTT server.
    print('Connected to MQTT server!')
    # Subscribe to the laser targeting topic.
    client.subscribe(TOPIC_TARGET)
    client.subscribe(TOPIC_PATH)
    client.subscribe(TOPIC_RELATIVE)
    client.subscribe(TOPIC_LASER)


def on_message(client, userdata, msg):
    # Called when a MQTT message is received.
Ejemplo n.º 2
0
SERVO_I2C_ADDRESS     = 0x40   # I2C address of the PCA9685-based servo controller
SERVO_XAXIS_CHANNEL   = 0      # Channel for the x axis rotation which controls laser up/down
SERVO_YAXIS_CHANNEL   = 1      # Channel for the y axis rotation which controls laser left/right
SERVO_PWM_FREQ        = 50     # PWM frequency for the servos in HZ (should be 50)
SERVO_MIN             = 150    # Minimum rotation value for the servo, should be -90 degrees of rotation.
SERVO_MAX             = 600    # Maximum rotation value for the servo, should be 90 degrees of rotation.
SERVO_CENTER          = 200    # Center value for the servo, should be 0 degrees of rotation.
MQTT_SERVER           = 'tony-imac'  # MQTT server to connect to for receiving commands.
MQTT_PORT             = 1883         # Port for the MQTT server.

# MQTT topics used for controlling the laser.
TOPIC_TARGET          = 'catlaser/target'

# Create servo and laser movement model.
servos = servos.Servos(SERVO_I2C_ADDRESS, SERVO_XAXIS_CHANNEL, SERVO_YAXIS_CHANNEL, SERVO_PWM_FREQ)
model = model.LaserModel(servos, SERVO_MIN, SERVO_MAX, SERVO_CENTER)


# MQTT callbacks:
def on_connect(client, userdata, flags, rc):
    # Called when connected to the MQTT server.
    print('Connected to MQTT server!')
    # Subscribe to the laser targeting topic.
    client.subscribe(TOPIC_TARGET)

def on_message(client, userdata, msg):
    # Called when a MQTT message is received.
    print('{0}: {1}'.format(msg.topic, str(msg.payload)))
    # Handle a target request.
    if msg.topic == TOPIC_TARGET:
        # Try to parse out two numbers from the payload.  These are the