Skywriter python help please?


#1

Hello!

I’m playing around with the big Skywriter and progress was good, but now I’m stuck.

What I’m trying to do is to control some rgb-lights via mqtt. The mqtt works super nice with the flick-actions, but I’m having trouble with the move-function.

My problem is that I can’t figure out how to use a change in the z-value to adjust brightness, perhaps my beginner-level python can explain my thinking better than words?

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

import paho.mqtt.client as mqtt
import skywriter
from os import system
import time
import signal

R       = 0
G       = 0
B       = 0
old_z   = float(0)
new_z   = float(0)

def on_connect(client, userdata, rc):
        client.subscribe('all/huzzah/rgb')
        client.subscribe('all/rpi_rgb')
        client.subscribe('all/rpi_rgb_R')
        client.subscribe('all/rpi_rgb_G')
        client.subscribe('all/rpi_rgb_B')

def on_message(client, userdata, msg):
  global R
  global G
  global B
  rgb_payload = msg.payload
  r_payload = int(rgb_payload.split(',')[0])
  g_payload = int(rgb_payload.split(',')[1])
  b_payload = int(rgb_payload.split(',')[2])
  R = r_payload
  G = g_payload
  B = b_payload

@skywriter.flick()
def flick(start,finish):
  if(start == "north" and finish == "south"):
    print "north - south"
    client.publish("all/huzzah/rgb", "1024, 1024, 1024")
  elif(start == "south" and finish == "north"):
    print "south - north"
    client.publish("all/huzzah/rgb", "1024, 0, 0")
  elif(start == "west" and finish == "east"):
    print "west - east"
    client.publish("all/huzzah/rgb", "0, 1024, 0")
  else:
    print "Invalid"

@skywriter.move()
def move(x, y, z):
  global old_z
  old_z = float(z)
  print old_z
  print type(old_z)
  time.sleep(0.2)

def move_new(x, y, z):
  global new_z
  new_z = float(z)
  print "blopp"
  print new_z

def diff():
  z_diff = old_z - new_z
  print z_diff
  signal.pause()

#def old_z(x, y, z):
#  z_old = float(z)
#  print z_old
#  round_z_old = round(z_old,3)
#  print round_z_old
#  sleep(0.5)
#def new_z(x, y, z):
#  z_new = float(z)
#  round_z_new = round(z_new,3)
#z_diff = round_z_old - round_z_new
#print z_diff

client = mqtt.Client()

client.on_connect = on_connect
client.username_pw_set('user', 'pass')
client.on_message = on_message
client.connect('192.x.x.x', port, 5)

client.loop_forever(timeout=1.0, max_packets=1)

#2

Nvm, solved it!

How I did it, I’m sure there are better ways, but this seems to work for now (please excuse sloppy code):

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

import paho.mqtt.client as mqtt
import skywriter
from os import system
import time
import signal

R               = int(0)
G               = int(0)
B               = int(0)
old_z           = float(0)
new_z           = float(0)

@skywriter.move()
def move(x, y, z):
  global old_z, new_z, R, G, B
  print "old"
  print R, G, B
  print old_z
  new_z = round(z, 5)
  print "new"
  print new_z
  diff = old_z - new_z
  print "diff"
  print diff
  old_z = new_z
  if diff > 0.01:
    diff = diff * 102.4
    print "BLOPP"
    print diff
    R = int(R + diff)
    G = int(G + diff)
    B = int(B + diff)
    msg = str("%d, %d, %d" % (R, G, B))
    print msg
    client.publish("all/huzzah/rgb", msg)
  elif diff < -0.01:
    diff = diff * -1
    print "neg_diff"
    print diff
    diff = diff * 102.4
    print diff
    R = int(R - diff)
    G = int(G - diff)
    B = int(B - diff)
    msg = str("%d, %d, %d" % (R, G, B))
    print msg
    client.publish("all/huzzah/rgb", msg)
  else:
    print "nope"
  time.sleep(0.2)

client = mqtt.Client()
client.max_inflight_messages_set(2000)
client.on_connect = on_connect
client.username_pw_set('user', 'pass')
client.on_message = on_message
client.connect('192.x.x.x', port, 5)

client.loop_forever(timeout=1.0, max_packets=1)