adafruitss: renamed to adafruit pca9685 servo shield

Signed-off-by: Mihai Tudor Panu <mihai.tudor.panu@intel.com>
This commit is contained in:
Mihai Tudor Panu
2015-03-06 12:37:45 -08:00
parent 2725342c19
commit f53d8e79cc
9 changed files with 39 additions and 33 deletions

View File

@ -1,7 +0,0 @@
%module jsupm_pca9685
%include "../upm.i"
%include "pca9685ss.h"
%{
#include "pca9685ss.h"
%}

View File

@ -1,152 +0,0 @@
/*
* Author: Stan Gifford <stan@gifford.id.au>
* Copyright (c) 2015 Intel Corporation.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "pca9685ss.h"
#include <unistd.h>
#include <math.h>
using namespace upm;
pca9685ss::pca9685ss(int bus,int i2c_address)
{
int n;
int result;
mraa_init();
m_i2c = mraa_i2c_init(bus);
pca9685_addr = i2c_address;
result=mraa_i2c_address(m_i2c, pca9685_addr);
m_rx_tx_buf[0]=PCA9685_MODE1;
m_rx_tx_buf[1]=0;
result=mraa_i2c_write(m_i2c,m_rx_tx_buf,2);
pca9685ss::setPWMFreq(60);
pca9685ss::update();
}
void pca9685ss::setPWMFreq(float freq) {
int result;
freq *= 0.88; // Correct for overshoot in the frequency setting (see issue #11).
float prescaleval = 25000000;
prescaleval /= 4096;
prescaleval /= freq;
prescaleval -= 1;
_pwm_frequency = 60.18; // FInal achieved frequency measured with Logic 8!
_duration_1ms = ((4096*_pwm_frequency)/1000); // This is 1ms duration
uint8_t prescale = floor(prescaleval + 0.5);
result=mraa_i2c_address(m_i2c, pca9685_addr);
uint8_t oldmode=0;
oldmode = mraa_i2c_read_byte_data(m_i2c,PCA9685_MODE1);
m_rx_tx_buf[0]=PCA9685_MODE1;
m_rx_tx_buf[1]=0x10; // sleep
result=mraa_i2c_address(m_i2c, pca9685_addr);
result=mraa_i2c_write(m_i2c,m_rx_tx_buf,2);
m_rx_tx_buf[0]=PCA9685_PRESCALE;
m_rx_tx_buf[1]=prescale;
result=mraa_i2c_address(m_i2c, pca9685_addr);
result=mraa_i2c_write(m_i2c,m_rx_tx_buf,2);
m_rx_tx_buf[0]=PCA9685_MODE1;
m_rx_tx_buf[1]=0x00;
result=mraa_i2c_address(m_i2c, pca9685_addr);
result=mraa_i2c_write(m_i2c,m_rx_tx_buf,2);
// result=mraa_i2c_write_byte_data(m_i2c,0x00,PCA9685_MODE1);
usleep(5000);
m_rx_tx_buf[0]=PCA9685_MODE1;
m_rx_tx_buf[1]=0xa1;
result=mraa_i2c_address(m_i2c, pca9685_addr);
result=mraa_i2c_write(m_i2c,m_rx_tx_buf,2);
}
int pca9685ss::update(void)
{
return MRAA_SUCCESS;
}
void pca9685ss::servo(uint8_t port, uint8_t servo_type, uint16_t degrees) {
// Set Servo values
// Degrees is from 0 to 180
// servo_type: 0 = standard 1ms to 2ms
// 1 = extended 0.6ms to 2.4ms
// 2 = extended 0.8ms to 2.2ms
uint16_t duration;
int result;
int r2;
if(degrees>180) degrees=180; // Ensure within bounds
if (degrees<0) degrees=0;
switch (servo_type) {
case 0: // Standard Servo 1ms to 2ms
duration = _duration_1ms + ((_duration_1ms*degrees)/180);
break;
case 1: // Extended Servo 0.6ms to 2.4ms, i.e. 1.8ms from 0 to 180
//duration = (_duration_1ms*0.6) + ((_duration_1ms*1.8*degrees)/180); simplified to..
duration = (_duration_1ms*0.6) + ((_duration_1ms*degrees)/100);
break;
case 2: // Extended Servo 0.8ms to 2.2ms, i.e. 1.4ms from 0 to 180
//duration = (_duration_1ms*0.8) + ((_duration_1ms*1.4*degrees)/180); simplified to..
duration = (_duration_1ms*0.8) + ((_duration_1ms*degrees)/128);
break;
case 3: // Extended Servo 0.9ms to 2.1ms, - GWS Mini STD BB servo
//duration = (_duration_1ms*0.8) + ((_duration_1ms*1.4*degrees)/180); simplified to..
duration = (_duration_1ms*0.9) + ((_duration_1ms*degrees)/120);
break;
}
result=mraa_i2c_address(m_i2c, pca9685_addr);
m_rx_tx_buf[0]=LED0_REG+4*port;
m_rx_tx_buf[1]=0;
m_rx_tx_buf[2]=0;
m_rx_tx_buf[3]=duration;
m_rx_tx_buf[4]=duration>>8;
result=mraa_i2c_write(m_i2c,m_rx_tx_buf,5);
r2=result;
}

View File

@ -1,105 +0,0 @@
/*
* Author: Stan Gifford <stan@gifford.id.au>
* Copyright (c) 2015 Intel Corporation.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#pragma once
#include <mraa/i2c.h>
#define MAX_BUFFER_LENGTH 6
#define SERVO_MIN 0
#define SERVO_MAX 180
#define SERVO_FREQ 60
#define PCA9685_SUBADR1 0x2
#define PCA9685_SUBADR2 0x3
#define PCA9685_SUBADR3 0x4
#define PCA9685_MODE1 0x0
#define PCA9685_PRESCALE 0xFE
#define LED0_ON_L 0x6
#define LED0_ON_H 0x7
#define LED0_OFF_L 0x8
#define LED0_OFF_H 0x9
#define ALLLED_ON_L 0xFA
#define ALLLED_ON_H 0xFB
#define ALLLED_OFF_L 0xFC
#define ALLLED_OFF_H 0xFD
#define PCA9685_MODE1_REG 0x00
#define PCA9685_PRESCALE_REG 0xFE
#define LED0_REG 0x06
namespace upm {
/**
* @brief pca9685 based servo controller
* @defgroup pca9685 libupm-pca9685
*/
/**
* @brief
* UPM library for pca9685 Adafruit 16-channel servo shield/controller. When 3 or more GWS servos attached results unpredictable.
* Adafruit do recommend a capacitor be installed on the board which should alleviate the issue. Sizing depends on servos and count.
*
* @ingroup i2c
* @snippet pca9685ss.cxx Interesting
*/
class pca9685ss {
public:
/**
* Creates a pca9685ss object
*
* @param bus number of used i2c bus
* @param i2c_address address of servo controller on i2c bus
*/
pca9685ss(int bus, int i2c_address);
int update(void);
/**
* Sets the frequency for your servos
*
* @param freq the frequency at which the servos operate
*/
void setPWMFreq(float freq);
/**
* Moves the one of the servos to the specified angle
*
* @param port port of the servo on the controller (servo number)
* @param servo_type can be 0 = standard 1ms to 2ms, 1 = extended 0.6ms to 2.4ms, or 2 = extended 0.8ms to 2.2ms
* @param degrees angle to set the servo to
*/
void servo(uint8_t port, uint8_t servo_type, uint16_t degrees);
private:
int pca9685_addr;
mraa_i2c_context m_i2c;
uint8_t m_rx_tx_buf[MAX_BUFFER_LENGTH];
float _pwm_frequency;
float _duration_1ms;
};
}

View File

@ -1,9 +0,0 @@
%module pyupm_pca9685
%include "../upm.i"
%feature("autodoc", "3");
%include "pca9685ss.h"
%{
#include "pca9685ss.h"
%}