ServoKit

Hex.pm API docs CI

Use PCA9685 PWM/Servo Controller in Elixir

Installation

You can install this library by adding servo_kit to your list of dependencies in mix.exs:

def deps do
  [
    {:servo_kit, "~> 0.1.0"}
  ]
end

Examples

Basic usage

{:ok, _pid} = ServoKit.start_link()

# Set the duty cycle to 7.5% for Channel 0
ServoKit.set_pwm_duty_cycle(7.5, ch: 0)

Controling a standard servo

ServoKit.start_link()

# Define the mapping between the angle in degrees and duty cycle in percentage
angle_range = {0, 180}
duty_cycle_range = {2.5, 12.5}

# Set the angle to 0 degree for channel 0
0 |> ServoKit.map(angle_range, duty_cycle_range) |> ServoKit.set_pwm_duty_cycle(ch: 0)

# Set the angle to 90 degree for channel 0
90 |> ServoKit.map(angle_range, duty_cycle_range) |> ServoKit.set_pwm_duty_cycle(ch: 0)

# Set the angle to 180 degree for channel 0
180 |> ServoKit.map(angle_range, duty_cycle_range) |> ServoKit.set_pwm_duty_cycle(ch: 0)

Controling a continuous servo

ServoKit.start_link()

# Define the mapping between the throttle -1..1 and duty cycle in percentage
throttle_range = {-1.0, 1.0}
duty_cycle_range = {2.5, 12.5}

# Stop the actuator for channel 0
0 |> ServoKit.map(throttle_range, duty_cycle_range) |> ServoKit.set_pwm_duty_cycle(ch: 0)

# Throttle full speed forward for channel 0
1.0 |> ServoKit.map(throttle_range, duty_cycle_range) |> ServoKit.set_pwm_duty_cycle(ch: 0)

# Throttle full speed reverse for channel 0
-1.0 |> ServoKit.map(throttle_range, duty_cycle_range) |> ServoKit.set_pwm_duty_cycle(ch: 0)