Servo Rotation

The servo can be turned clockwise, counter clockwise, and to the 90 degree position, but it lacks a way to query the current rotation. The rotation needs to be calculated manually and this script is a first attempt.

#!/usr/bin/env python

import RPi.GPIO as GPIO
import datetime
import time

servo_pin = 22
servo_pin2 = 18

# 60 degrees / 0.1seconds
servo_speed = 0.1

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)

GPIO.setup(servo_pin, GPIO.OUT)
GPIO.setup(servo_pin2, GPIO.OUT)

last_time = datetime.datetime.now()
current_time = datetime.datetime.now()
sumTime = datetime.timedelta(0, 0)

accuracy = 0.01
targetRotation = 0
currentRotation = 90

pulse1 = GPIO.PWM(servo_pin, 50)
pulse2 = GPIO.PWM(servo_pin2, 50)

logTime = datetime.datetime.now()

def log(msg):
 global deltaTime
 global logTime
 if (logTime < datetime.datetime.now()):
  logTime = datetime.datetime.now() + datetime.timedelta(0, 0.5)
  print msg
 return

def reset(pulse):
 pulse.start(7.5);
 pulse.ChangeDutyCycle(7.5)
 return

def update(pulse, targetRotation):
 global deltaTime
 global sumTime
 global servo_speed
 global accuracy
 global currentRotation
 log ("TargetRotation: " + str(targetRotation) + " CurrentRotation: "+str(currentRotation))
 if (targetRotation == 90):
  pulse.ChangeDutyCycle(7.5)
  if ((currentRotation - targetRotation) < -accuracy):
   currentRotation += servo_speed
  elif ((currentRotation - targetRotation) > accuracy):
   currentRotation -= servo_speed
  else:
   pulse.ChangeDutyCycle(0)
 elif ((currentRotation - targetRotation) < -accuracy):
  pulse.ChangeDutyCycle(12.5)
  currentRotation += servo_speed
 elif ((currentRotation - targetRotation) > accuracy):
  pulse.ChangeDutyCycle(2.5)
  currentRotation -= servo_speed
 else:
  pulse.ChangeDutyCycle(0)
 return

try:
 reset(pulse1)
 reset(pulse2)
 time.sleep(1)
 print "setup complete"

 while True:
 
  last_time = current_time
  current_time = datetime.datetime.now()
  deltaTime = current_time - last_time;
  sumTime += deltaTime;

  if (sumTime.total_seconds() > 3.0):
   #print (sumTime)
   sumTime -= datetime.timedelta(0, 3)
   targetRotation = (targetRotation + 45) % 180

  update(pulse1, targetRotation);
  update(pulse2, targetRotation);

  time.sleep(0);

except KeyboardInterrupt:

 print '\r\nProgam complete.'
 GPIO.cleanup();

Python Time Handling

Time and logic is needed to do anything fancy in Python.

#!/usr/bin/env python

import datetime
import time

# global time of last frame
last_time = datetime.datetime.now()

# global time of current frame
current_time = datetime.datetime.now()

# a running some of the delta time of each frame
sumTime = datetime.timedelta(0, 0)

# define an update function
def update():
 # make global accessibles from function
 global deltaTime;
 global sumTime;

 #prints the current time hours, minutes, seconds, and milliseconds
 #print (datetime.datetime.now().strftime("%H:%M:%S.%f"))

 # if condition checks for 1 second to pass
 if (sumTime.total_seconds() > 1.0):
  # print the elapsed time over the last second
  print (sumTime)
  # reset the elapsed time
  sumTime -= datetime.timedelta(0, 1)
 return

try:
 while True:

  # record the time in the last frame
  last_time = current_time

  # get the current time hours, minutes, seconds, milliseconds
  current_time = datetime.datetime.now()

  # calculate the time difference between frames
  deltaTime = current_time - last_time;

  # keep track of the elapsed time
  sumTime += deltaTime;

  # invoke the update function
  update();

  # yield for the next frame
  time.sleep(0);

# wait for a key to exit
except KeyboardInterrupt:

 print '\r\nProgam complete.'

Raspberry PI 2 – Servo Control

Using pulse modulation, the Raspberry PI can adjust a servo.
https://www.youtube.com/watch?v=ddlDgUymbxc

2015-04-22+12.06.15[1]

Here I combined the LED blinking example with the servo example.

#!/usr/bin/env python

import RPi.GPIO as GPIO
import time
led_pin = 15
led_pin2 = 16
led_pin3 = 36
led_pin4 = 37

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)

GPIO.setup(led_pin, GPIO.OUT)
GPIO.setup(led_pin2, GPIO.OUT)
GPIO.setup(led_pin3, GPIO.OUT)
GPIO.setup(led_pin4, GPIO.OUT)

GPIO.setup(22, GPIO.OUT)

p = GPIO.PWM(22, 50)
p.start(7.5);

try:
 while True:

  GPIO.output(led_pin, GPIO.HIGH)
  GPIO.output(led_pin2, GPIO.HIGH)
  GPIO.output(led_pin3, GPIO.HIGH)
  GPIO.output(led_pin4, GPIO.HIGH)
  p.ChangeDutyCycle(7.5)
  time.sleep(1)

  GPIO.output(led_pin, GPIO.LOW)
  GPIO.output(led_pin2, GPIO.LOW)
  GPIO.output(led_pin3, GPIO.HIGH)
  GPIO.output(led_pin4, GPIO.HIGH)
  p.ChangeDutyCycle(12.5)
  time.sleep(1)

  GPIO.output(led_pin, GPIO.HIGH)
  GPIO.output(led_pin2, GPIO.HIGH)
  GPIO.output(led_pin3, GPIO.HIGH)
  GPIO.output(led_pin4, GPIO.HIGH)
  p.ChangeDutyCycle(7.5)
  time.sleep(1)

  GPIO.output(led_pin, GPIO.HIGH)
  GPIO.output(led_pin2, GPIO.HIGH)
  GPIO.output(led_pin3, GPIO.LOW)
  GPIO.output(led_pin4, GPIO.LOW)
  p.ChangeDutyCycle(2.5)
  time.sleep(1)

except KeyboardInterrupt:

 print '\r\nBack to neutral...'
 p.ChangeDutyCycle(7.5)
 time.sleep(1)

 print '\r\nProgam complete.'
 GPIO.cleanup();

Raspberry PI 2 – Alternating LEDs

The following Python alternates between two LEDs and then goes dark before repeating.

20150418_211943[1]

#!/usr/bin/env python

import RPi.GPIO as GPIO
import time
led_pin = 15
led_pin2 = 37
blinkSpeed = 5/2.0 #blink x times per second

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)

GPIO.setup(led_pin, GPIO.OUT)
GPIO.setup(led_pin2, GPIO.OUT)

try:
 while True:

  GPIO.output(led_pin, GPIO.HIGH)
  GPIO.output(led_pin2, GPIO.LOW)
  time.sleep(blinkSpeed / 3.0)

  GPIO.output(led_pin, GPIO.LOW)
  GPIO.output(led_pin2, GPIO.HIGH)
  time.sleep(blinkSpeed / 3.0)

  GPIO.output(led_pin, GPIO.LOW)
  GPIO.output(led_pin2, GPIO.LOW)
  time.sleep(blinkSpeed / 3.0)

finally:
 print 'finally'

Raspberry PI 2 – Blinking LED

Here’s a short Python script to toggle an LED using GPIO.

20150418_173116[1]

#!/usr/bin/env python

import RPi.GPIO as GPIO
import time
pin = 15
blinkSpeed = 1/5.0 #blink x times per second
GPIO.setmode(GPIO.BOARD)
GPIO.setup(pin, GPIO.OUT)
try:
 while True:
  print('PIN {} is going HIGH'.format(pin))
  GPIO.output(pin, GPIO.HIGH)
  time.sleep(blinkSpeed / 2.0)
  print('PIN {} is going LOW'.format(pin))
  GPIO.output(pin, GPIO.LOW)
  time.sleep(blinkSpeed / 2.0)
finally:
 print 'finally'

Raspberry PI 2 – Sails.js

Installing Sails.js required building and installing Node.js from source.

I let the source build overnight and then moments later ‘sails lift’ was functional.
https://github.com/tgraupmann/TAGENIGMA-Docs/blob/master/Sails.md

Also the reference guide is super helpful.
http://sailsjs.org/#!/documentation/reference/

Here is also a useful C# client:
https://github.com/Quobject/SocketIoClientDotNet

Raspberry PI 2 – VNC

I picked up a Raspberry PI 2 and it’s working super speedy. Quad-core 900 MHz, ARM, 4-usb, HDMI/audio out.
http://www.amazon.com/CanaKit-Raspberry-Ultimate-Starter-Components/dp/B00G1PNG54/

After settting up WIFI, installing VNC made connecting the display, mouse, and keyboard no longer necessary. The boot process can be altered to start VNC server automatically.
https://www.raspberrypi.org/documentation/remote-access/vnc/