Skip to content

Instantly share code, notes, and snippets.

@wsmoak
Last active June 12, 2022 00:57
Show Gist options
  • Save wsmoak/6da34768fdfd6d4c2d1d to your computer and use it in GitHub Desktop.
Save wsmoak/6da34768fdfd6d4c2d1d to your computer and use it in GitHub Desktop.
Spinning a servo with elixir_ale

Spinny is an Elixir port of the Adafruit_PWM_Servo_Driver code in Adafruit's Raspberry-Pi Python Code Library

https://github.com/adafruit/Adafruit-Raspberry-Pi-Python-Code/tree/master/Adafruit_PWM_Servo_Driver

It uses Elixir ALE for low-level i2c communication:

https://github.com/fhunleth/elixir_ale

Page numbers refer to the PCA9685 Datasheet: https://www.adafruit.com/datasheets/PCA9685.pdf

See LICENSE.md for more info

Usage

At the command line:

$ mix run -e Spinny.start # repeatedly spin left and right

(to quit, time your ctrl-c's for when it's idle)

$ mix run -e Spinny.swrst # software reset

In iex:

$ iex -S mix
> {:ok,pid}= I2c.start_link("i2c-1", 0x40)
> Spinny.init(pid)
> Spinny.prescale(pid)
> Spinny.bump(pid)

Copyright 2015 Wendy Smoak

Spinny is an Elixir port of the Adafruit_PWM_Servo_Driver code in Adafruit's Raspberry-Pi Python Code Library

https://github.com/adafruit/Adafruit-Raspberry-Pi-Python-Code/tree/master/Adafruit_PWM_Servo_Driver

As such, it inherits the BSD license and the additional information shown below:

Adafruit's Raspberry-Pi Python Code Library

Here is a growing collection of libraries and example python scripts for controlling a variety of Adafruit electronics with a Raspberry Pi

In progress!

Adafruit invests time and resources providing this open source code, please support Adafruit and open-source hardware by purchasing products from Adafruit!

Written by Limor Fried, Kevin Townsend and Mikey Sklar for Adafruit Industries. BSD license, all text above and below must be included in any redistribution

To download, we suggest logging into your Pi with Internet accessibility and typing:

git clone https://github.com/adafruit/Adafruit-Raspberry-Pi-Python-Code.git

============ Copyright (c) 2012-2013 Limor Fried, Kevin Townsend and Mikey Sklar for Adafruit Industries. All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of the nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

defmodule Spinny.Mixfile do
use Mix.Project
def project do
[app: :spinny,
version: "0.0.1",
elixir: "~> 1.2-rc",
build_embedded: Mix.env == :prod,
start_permanent: Mix.env == :prod,
deps: deps]
end
# Configuration for the OTP application
#
# Type "mix help compile.app" for more information
def application do
[applications: [:logger]]
end
# Dependencies can be Hex packages:
#
# {:mydep, "~> 0.3.0"}
#
# Or git/path repositories:
#
# {:mydep, git: "https://github.com/elixir-lang/mydep.git", tag: "0.1.0"}
#
# Type "mix help deps" for more examples and options
defp deps do
[
{:elixir_ale, "~> 0.4.0"},
]
end
end
defmodule Spinny do
require Logger
use Bitwise
@mode1 0x00 # bit 4 is SLEEP 000X0000
@mode2 0x01
@prescale 0xFE
@all_on_l 0xFA
@all_on_h 0xFB
@all_off_l 0xFC
@all_off_h 0xFD
@led0_on_l 0x06
@led0_on_h 0x07
@led0_off_l 0x08
@led0_off_h 0x09
@allcall 0x01
@outdrv 0x04
@swrst 0x06
def start do
Logger.debug "Starting..."
{:ok,pid}= I2c.start_link("i2c-1", 0x40) # PCA9685 default address
init(pid)
prescale(pid, 60) # set pwm to 60 Hz
spin(pid)
end
# PCA9685 Software Reset (Section 7.6 on pg 28)
def swrst do
Logger.debug "PCA9685 Software Reset..."
{:ok,pid}=I2c.start_link("i2c-1", 0x00)
:timer.sleep 10
I2c.write(pid,<<@swrst>>)
end
def init(pid) do
Logger.debug "Initializing..."
set_all_pwm(pid,0,0)
I2c.write(pid, <<@mode2, @outdrv>>) # external driver, see docs
I2c.write(pid, <<@mode1, @allcall>>) # program all PCA9685's at once
:timer.sleep 5
end
def prescale(pid,freq) do
Logger.debug "Setting prescale to #{freq} Hz"
# pg 14 and solve for prescale or example on pg 25
prescaleval = trunc(Float.round( 25000000.0 / 4096.0 / freq ) - 1 )
Logger.debug "prescale value is #{prescaleval}"
oldmode = I2c.write_read(pid, <<@mode1>>, 1)
:timer.sleep 5
I2c.write(pid, <<@mode1, 0x11>>) # set bit 4 (sleep) to allow setting prescale
I2c.write(pid, <<@prescale, prescaleval>> )
I2c.write(pid, <<@mode1, 0x01>> ) #un-set sleep bit
:timer.sleep 5 # pg 14 it takes 500 us for the oscillator to be ready
I2c.write(pid, <<@mode1>> <> oldmode ) # put back old mode
end
# spin one way
def min(pid) do
Logger.debug "min"
set_pwm(pid, 0, 0, 0x0096)
end
# spin the other way
def max(pid) do
Logger.debug "max"
set_pwm(pid, 0, 0, 0x0258)
end
# stop
def mid(pid) do
Logger.debug "mid"
set_pwm(pid, 0, 0, 0)
end
# spins just a bit in one direction and stops
def bump(pid) do
Logger.debug "bump"
max(pid)
:timer.sleep 200
mid(pid)
end
# repeatedly spins one way, then the other
# to quit, time your ctrl-c's for when it is idle
# or it will be left spinning forever
def spin(pid) do
min(pid)
:timer.sleep 1000
mid(pid)
:timer.sleep 1000
max(pid)
:timer.sleep 1000
mid(pid)
:timer.sleep 1000
spin(pid)
end
# The registers for each of the 16 channels are sequential
# so the address can be calculated as an offset from the first one
def set_pwm(pid, channel, on, off) do
I2c.write(pid, <<@led0_on_l+4*channel, on &&& 0xFF>>)
I2c.write(pid, <<@led0_on_h+4*channel, on >>> 8>>)
I2c.write(pid, <<@led0_off_l+4*channel, off &&& 0xFF>>)
I2c.write(pid, <<@led0_off_h+4*channel, off >>> 8>>)
end
# The PCA9685 has special registers for setting ALL channels
# (or 1/3 of them) to the same value.
def set_all_pwm(pid, on, off) do
I2c.write(pid, <<@all_on_l, on &&& 0xFF>>)
I2c.write(pid, <<@all_on_h, on >>> 8>>)
I2c.write(pid, <<@all_off_l, off &&& 0xFF>>)
I2c.write(pid, <<@all_off_h, off >>> 8>>)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment