Aruna
aruna::driver::PCA9685 Class Reference

#include <PCA9685.h>

Inheritance diagram for aruna::driver::PCA9685:
Collaboration diagram for aruna::driver::PCA9685:

Public Member Functions

 PCA9685 (uint8_t led, I2C_master *i2c_bus, uint8_t i2c_address=default_i2c_address)
 PCA9685 pwm object for every LED. More...
 
 ~PCA9685 ()
 
uint32_t get_frequency ()
 
err_t set_duty (uint16_t on, uint16_t off)
 Set the on and off timing of the PCA9685 led. More...
 
uint16_t get_duty ()
 
err_t get_duty (uint16_t &on, uint16_t &off)
 Get the on and off timing of the PCA9685 led. More...
 
err_t is_connected ()
 Is the PCA9685 connected to the I²C bus. More...
 
- Public Member Functions inherited from aruna::driver::Pwm
err_t set_frequency (uint32_t frequency_hz)
 Set frequency of PWM in hertz. More...
 
uint32_t get_frequency () const
 Get the frequency used in hertz. More...
 
err_t set_duty (float duty_percentage)
 Set the duty cycle on time in percentage. More...
 
err_t set_duty (uint16_t duty)
 Set the duty cycle on time (16bit). More...
 
float get_duty_per () const
 Get the duty cycle on time in percentage. More...
 
uint16_t get_duty () const
 Get the duty cycle on time (16bit) More...
 
- Public Member Functions inherited from aruna::movement::Actuator
 Actuator (axis_mask_t axis=axis_mask_t::NONE)
 Actuator object, used by the movement module for vehicle movement. More...
 
virtual ~Actuator ()
 
err_t set_axis (axis_mask_t new_axis)
 Set the axis that this Actuator is capable in moving in. More...
 
axis_mask_t get_axis ()
 get the movement modes that this driver supports. More...
 
err_t set (axis_mask_t axisMask, int16_t speed)
 Set the speed of the motors directly. More...
 
uint16_t get_speed ()
 Get the current speed of Actuator. More...
 

Private Types

enum  register_address_pointer {
  register_address_pointer::MODE1 = 0x0, register_address_pointer::MODE2 = 0x1, register_address_pointer::SUBADR1 = 0x2, register_address_pointer::SUBADR2 = 0x3,
  register_address_pointer::SUBADR3 = 0x4, register_address_pointer::ALLCALLADR = 0x5, register_address_pointer::LED0_ON_L = 0x6, register_address_pointer::ALL_LED_ON_L = 0xFA,
  register_address_pointer::PRE_SCALE = 0xFE, register_address_pointer::TESTMODE = 0xFF
}
 Pointer to the registers. More...
 
enum  RESTART { RESTART::DEFAULT = 0x0, RESTART::DISABLED = 0x0, RESTART::ENABLED = 0x80 }
 
enum  EXTCLK { EXTCLK::DEFAULT = 0x0, EXTCLK::USE_INTERNAL_CLOCK = 0x0, EXTCLK::USE_EXTCLK_PIN_CLOCK = 0x40 }
 
enum  AI { AI::DEFAULT = 0x0, AI::AUTO_INCREMENT_DISABLED = 0x0, AI::AUTO_INCREMENT_ENABLED = 0x20 }
 
enum  SLEEP { SLEEP::DEFAULT = 0x10, SLEEP::NORMAL_MODE = 0x0, SLEEP::LOW_POWER = 0x10 }
 
enum  SUB1 { SUB1::DEFAULT = 0x0, SUB1::NOT_RESPOND_TO_SUBADDRESS1 = 0x0, SUB1::RESPOND_TO_SUBADDRESS1 = 0x8 }
 
enum  SUB2 { SUB2::DEFAULT = 0x0, SUB2::NOT_RESPOND_TO_SUBADDRESS2 = 0x0, SUB2::RESPOND_TO_SUBADDRESS2 = 0x4 }
 
enum  SUB3 { SUB3::DEFAULT = 0x0, SUB3::NOT_RESPOND_TO_SUBADDRESS3 = 0x0, SUB3::RESPOND_TO_SUBADDRESS3 = 0x2 }
 
enum  ALLCALL { ALLCALL::DEFAULT = 0x1, ALLCALL::NOT_RESPOND_TO_LED_ALL = 0x0, ALLCALL::RESPOND_TO_LED_ALL = 0x1 }
 
enum  INVRT { INVRT::DEFAULT = 0x0, INVRT::OUTPUT_LOGIC_NOT_INVERTED = 0x0, INVRT::OUTPUT_LOGIC_INVERTED = 0x10 }
 
enum  OCH { OCH::DEFAULT = 0x0, OCH::OUTPUT_CHANGE_ON_STOP_COMMAND = 0x0, OCH::OUTPUT_CHANGE_ON_ACK = 0x8 }
 
enum  OUTDRV { OUTDRV::DEFAULT = 0x4, OUTDRV::OPEN_DRAIN = 0x0, OUTDRV::TOTEM_POLE = 0x4 }
 
enum  OUTNE { OUTNE::DEFAULT = 0x0, OUTNE::LEDn_0 = 0x0, OUTNE::LEDn_1_OUTDRV_1 = 0b01, OUTNE::LEDn_HIGH_IMPEDANCE = 0b10 }
 

Private Member Functions

err_t _set_frequency (uint32_t frequency_hz) override
 
err_t _set_duty (uint16_t duty) override
 

Private Attributes

const uint8_t led
 
const uint8_t i2c_address
 
I2C_masteri2c_bus
 

Static Private Attributes

static aruna::log::channel_t log
 
uint8_t static const default_i2c_address = 0b1000000
 

Additional Inherited Members

- Public Attributes inherited from aruna::movement::Actuator
err_t startup_error = err_t::NOT_STARTED
 error when constructing gets put here, read before usage. More...
 
- Static Protected Member Functions inherited from aruna::movement::Actuator
static double convert_range (uint16_t input, float range_max=100.f, float range_min=0.f)
 Convert uint16 to a new range. More...
 

Detailed Description

Definition at line 15 of file PCA9685.h.

Member Enumeration Documentation

◆ AI

enum aruna::driver::PCA9685::AI
strongprivate
Enumerator
DEFAULT 
AUTO_INCREMENT_DISABLED 
AUTO_INCREMENT_ENABLED 

Definition at line 50 of file PCA9685.h.

50  {
51  DEFAULT = 0x0,
52  AUTO_INCREMENT_DISABLED = 0x0,
53  AUTO_INCREMENT_ENABLED = 0x20,
54  };

◆ ALLCALL

enum aruna::driver::PCA9685::ALLCALL
strongprivate
Enumerator
DEFAULT 
NOT_RESPOND_TO_LED_ALL 
RESPOND_TO_LED_ALL 

Definition at line 80 of file PCA9685.h.

80  {
81  DEFAULT = 0x1,
82  NOT_RESPOND_TO_LED_ALL = 0x0,
83  RESPOND_TO_LED_ALL = 0x1,
84  };

◆ EXTCLK

enum aruna::driver::PCA9685::EXTCLK
strongprivate
Enumerator
DEFAULT 
USE_INTERNAL_CLOCK 
USE_EXTCLK_PIN_CLOCK 

Definition at line 44 of file PCA9685.h.

44  {
45  DEFAULT = 0x0,
46  USE_INTERNAL_CLOCK = 0x0,
47  USE_EXTCLK_PIN_CLOCK = 0x40,
48  };

◆ INVRT

enum aruna::driver::PCA9685::INVRT
strongprivate
Enumerator
DEFAULT 
OUTPUT_LOGIC_NOT_INVERTED 
OUTPUT_LOGIC_INVERTED 

Definition at line 87 of file PCA9685.h.

87  {
88  DEFAULT = 0x0,
89  OUTPUT_LOGIC_NOT_INVERTED = 0x0,
90  OUTPUT_LOGIC_INVERTED = 0x10,
91  };

◆ OCH

enum aruna::driver::PCA9685::OCH
strongprivate
Enumerator
DEFAULT 
OUTPUT_CHANGE_ON_STOP_COMMAND 
OUTPUT_CHANGE_ON_ACK 

Definition at line 93 of file PCA9685.h.

93  {
94  DEFAULT = 0x0,
95  OUTPUT_CHANGE_ON_STOP_COMMAND = 0x0,
96  OUTPUT_CHANGE_ON_ACK = 0x8,
97  };

◆ OUTDRV

enum aruna::driver::PCA9685::OUTDRV
strongprivate
Enumerator
DEFAULT 
OPEN_DRAIN 
TOTEM_POLE 

Definition at line 99 of file PCA9685.h.

99  {
100  DEFAULT = 0x4,
101  OPEN_DRAIN = 0x0,
102  TOTEM_POLE = 0x4,
103  };

◆ OUTNE

enum aruna::driver::PCA9685::OUTNE
strongprivate
Enumerator
DEFAULT 
LEDn_0 
LEDn_1_OUTDRV_1 
LEDn_HIGH_IMPEDANCE 

Definition at line 105 of file PCA9685.h.

105  {
106  DEFAULT = 0x0,
107  LEDn_0 = 0x0,
108  LEDn_1_OUTDRV_1 = 0b01,
109  LEDn_HIGH_IMPEDANCE = 0b10,
110  };

◆ register_address_pointer

Pointer to the registers.

Enumerator
MODE1 
MODE2 
SUBADR1 
SUBADR2 
SUBADR3 
ALLCALLADR 
LED0_ON_L 
ALL_LED_ON_L 
PRE_SCALE 
TESTMODE 

Definition at line 22 of file PCA9685.h.

22  {
23  MODE1 = 0x0,
24  MODE2 = 0x1,
25  SUBADR1 = 0x2,
26  SUBADR2 = 0x3,
27  SUBADR3 = 0x4,
28  ALLCALLADR = 0x5,
29  LED0_ON_L = 0x6,
30 
31  ALL_LED_ON_L = 0xFA,
32  PRE_SCALE = 0xFE,
33  TESTMODE = 0xFF,
34 
35  };

◆ RESTART

enum aruna::driver::PCA9685::RESTART
strongprivate
Enumerator
DEFAULT 
DISABLED 
ENABLED 

Definition at line 38 of file PCA9685.h.

38  {
39  DEFAULT = 0x0,
40  DISABLED = 0x0,
41  ENABLED = 0x80,
42  };

◆ SLEEP

enum aruna::driver::PCA9685::SLEEP
strongprivate
Enumerator
DEFAULT 
NORMAL_MODE 
LOW_POWER 

Definition at line 56 of file PCA9685.h.

56  {
57  DEFAULT = 0x10,
58  NORMAL_MODE = 0x0,
59  LOW_POWER = 0x10,
60  };

◆ SUB1

enum aruna::driver::PCA9685::SUB1
strongprivate
Enumerator
DEFAULT 
NOT_RESPOND_TO_SUBADDRESS1 
RESPOND_TO_SUBADDRESS1 

Definition at line 62 of file PCA9685.h.

62  {
63  DEFAULT = 0x0,
64  NOT_RESPOND_TO_SUBADDRESS1 = 0x0,
65  RESPOND_TO_SUBADDRESS1 = 0x8,
66  };

◆ SUB2

enum aruna::driver::PCA9685::SUB2
strongprivate
Enumerator
DEFAULT 
NOT_RESPOND_TO_SUBADDRESS2 
RESPOND_TO_SUBADDRESS2 

Definition at line 68 of file PCA9685.h.

68  {
69  DEFAULT = 0x0,
70  NOT_RESPOND_TO_SUBADDRESS2 = 0x0,
71  RESPOND_TO_SUBADDRESS2 = 0x4,
72  };

◆ SUB3

enum aruna::driver::PCA9685::SUB3
strongprivate
Enumerator
DEFAULT 
NOT_RESPOND_TO_SUBADDRESS3 
RESPOND_TO_SUBADDRESS3 

Definition at line 74 of file PCA9685.h.

74  {
75  DEFAULT = 0x0,
76  NOT_RESPOND_TO_SUBADDRESS3 = 0x0,
77  RESPOND_TO_SUBADDRESS3 = 0x2,
78  };

Constructor & Destructor Documentation

◆ PCA9685()

PCA9685::PCA9685 ( uint8_t  led,
I2C_master i2c_bus,
uint8_t  i2c_address = default_i2c_address 
)

PCA9685 pwm object for every LED.

Parameters
led0-15 led number
i2c_addressI²C address of the IC
i2c_busBus to use for I²C communication.

Definition at line 13 of file PCA9685.cpp.

14  this->i2c_bus = i2c_bus;
15 
16 // read device
17  err_t i2c_err;
18  uint8_t mode1;
19  i2c_bus->lock(i2c_address);
20  i2c_err = i2c_bus->read(i2c_address, static_cast<uint8_t>(register_address_pointer::MODE1), &mode1, 1);
21  if (i2c_err != err_t::OK)
22  log.error("Error while reading MODE1 0x%X: %s", i2c_address, err_to_char.at(i2c_err));
23 
24 // check to see if device is powered on
25  if (mode1 & (uint8_t) SLEEP::LOW_POWER) {
26  i2c_err = i2c_bus->write(i2c_address, (uint8_t) register_address_pointer::MODE1,
27  reinterpret_cast<uint8_t *>(mode1 | (uint8_t) SLEEP::NORMAL_MODE), 1);
28  if (i2c_err != err_t::OK)
29  log.error("Error while writing MODE1 0x%X: %s", i2c_address, err_to_char.at(i2c_err));
30  }
31  i2c_bus->unlock(i2c_address);
32 }
const std::map< err_t, char * > err_to_char
Definition: arunaTypes.h:54
err_t read(uint8_t address, uint8_t reg, uint8_t *buffer, size_t buffer_size)
Read data from I²C slave.
Definition: I2C_master.cpp:7
static aruna::log::channel_t log
Definition: PCA9685.h:18
const uint8_t led
Definition: PCA9685.h:113
I2C_master * i2c_bus
Definition: PCA9685.h:114
err_t write(uint8_t address, uint8_t reg, uint8_t *data, size_t data_size)
Write data to the I²C bus.
Definition: I2C_master.cpp:15
err_t lock(uint8_t i2c_address)
Definition: I2C_master.cpp:41
const uint8_t i2c_address
Definition: PCA9685.h:113
int error(const char *format,...)
log error message
Definition: log.cpp:90
err_t unlock(uint8_t i2c_address)
Definition: I2C_master.cpp:51
Here is the call graph for this function:

◆ ~PCA9685()

PCA9685::~PCA9685 ( )

Definition at line 109 of file PCA9685.cpp.

109  {
110  Pwm::set_duty((uint16_t)0);
111 // TODO put PCA9685 to sleep if I'm the last one connected to it.
112 }
err_t set_duty(float duty_percentage)
Set the duty cycle on time in percentage.
Definition: Pwm.cpp:23
Here is the call graph for this function:

Member Function Documentation

◆ _set_duty()

err_t PCA9685::_set_duty ( uint16_t  duty)
overrideprivatevirtual

Implements aruna::driver::Pwm.

Definition at line 71 of file PCA9685.cpp.

71  {
72 
73  duty = round(duty / 65535 * 4095);
74  uint16_t on = 0;
75  uint16_t off = duty;
76  return set_duty(on, off);
77 }
uint16_t duty
Definition: Pwm.h:16
err_t set_duty(uint16_t on, uint16_t off)
Set the on and off timing of the PCA9685 led.
Definition: PCA9685.cpp:79
Here is the call graph for this function:

◆ _set_frequency()

err_t PCA9685::_set_frequency ( uint32_t  frequency_hz)
overrideprivatevirtual

Implements aruna::driver::Pwm.

Definition at line 34 of file PCA9685.cpp.

34  {
35  if (frequency_hz > 1526)
37  if (frequency_hz < 25)
39  err_t i2c_err;
40  uint8_t mode1;
41 // only works if using internal clock
42  uint8_t prescale = round(25000000 / (4096 * frequency_hz)) - 1;
44  i2c_err = i2c_bus->read(i2c_address, static_cast<uint8_t>(register_address_pointer::MODE1), &mode1, 1);
45  if (i2c_err != err_t::OK)
46  log.error("Error while reading MODE1 0x%X: %s", i2c_address, err_to_char.at(i2c_err));
48  reinterpret_cast<uint8_t *>(mode1 ^ (uint8_t) SLEEP::NORMAL_MODE), 1);
49  if (i2c_err != err_t::OK)
50  log.error("Error while writing MODE1 0x%X: %s", i2c_address, err_to_char.at(i2c_err));
51  i2c_err = i2c_bus->write(i2c_address, (uint8_t) register_address_pointer::PRE_SCALE, &prescale, 1);
52  if (i2c_err != err_t::OK)
53  log.error("Error while writing PRE_SCALE 0x%X: %s", i2c_address, err_to_char.at(i2c_err));
54  i2c_err = i2c_bus->write(i2c_address, (uint8_t) register_address_pointer::MODE1, &mode1, 1);
56 
57  return i2c_err;
58 }
const std::map< err_t, char * > err_to_char
Definition: arunaTypes.h:54
err_t read(uint8_t address, uint8_t reg, uint8_t *buffer, size_t buffer_size)
Read data from I²C slave.
Definition: I2C_master.cpp:7
static aruna::log::channel_t log
Definition: PCA9685.h:18
I2C_master * i2c_bus
Definition: PCA9685.h:114
err_t write(uint8_t address, uint8_t reg, uint8_t *data, size_t data_size)
Write data to the I²C bus.
Definition: I2C_master.cpp:15
err_t lock(uint8_t i2c_address)
Definition: I2C_master.cpp:41
const uint8_t i2c_address
Definition: PCA9685.h:113
int error(const char *format,...)
log error message
Definition: log.cpp:90
err_t unlock(uint8_t i2c_address)
Definition: I2C_master.cpp:51
uint32_t frequency_hz
Definition: Pwm.h:15
Here is the call graph for this function:

◆ get_duty() [1/2]

uint16_t PCA9685::get_duty ( )

Definition at line 89 of file PCA9685.cpp.

89  {
90  uint16_t on, off;
91  err_t err = get_duty(on, off);
92  if (err != err_t::OK)
93  log.error("Error while getting duty cycle 0x%X: %s", i2c_address, err_to_char.at(err));
94 // TODO test conversion
95  uint16_t duty = round(off / 4095 * 65535);
96  return duty;
97 }
uint16_t get_duty()
Definition: PCA9685.cpp:89
const std::map< err_t, char * > err_to_char
Definition: arunaTypes.h:54
uint16_t duty
Definition: Pwm.h:16
static aruna::log::channel_t log
Definition: PCA9685.h:18
const uint8_t i2c_address
Definition: PCA9685.h:113
int error(const char *format,...)
log error message
Definition: log.cpp:90
Here is the call graph for this function:

◆ get_duty() [2/2]

err_t PCA9685::get_duty ( uint16_t &  on,
uint16_t &  off 
)

Get the on and off timing of the PCA9685 led.

Parameters
on12 bit time to set signal to high
off12 bit time to set signal to low
Returns
I²C bus error

Definition at line 99 of file PCA9685.cpp.

99  {
100  uint8_t reg = (uint8_t) register_address_pointer::LED0_ON_L + 4 * led;
101  uint8_t buff[4];
102  err_t i2c_err = i2c_bus->read(i2c_address, reg, buff, 4);
103 // todo test this conversion
104  on = buff[0] | buff[1] >> 8;
105  off = buff[2] | buff[3] >> 8;
106  return i2c_err;
107 }
err_t read(uint8_t address, uint8_t reg, uint8_t *buffer, size_t buffer_size)
Read data from I²C slave.
Definition: I2C_master.cpp:7
const uint8_t led
Definition: PCA9685.h:113
I2C_master * i2c_bus
Definition: PCA9685.h:114
const uint8_t i2c_address
Definition: PCA9685.h:113
Here is the call graph for this function:

◆ get_frequency()

uint32_t PCA9685::get_frequency ( )

Definition at line 60 of file PCA9685.cpp.

60  {
61  uint8_t prescale = 0;
62  err_t i2c_err;
63  uint32_t freq = 0;
64  i2c_err = i2c_bus->read(i2c_address, (uint8_t) register_address_pointer::PRE_SCALE, &prescale, 1);
65  if (i2c_err != err_t::OK)
66  log.error("Error while reading PRE_SCALE 0x%X: %s", i2c_address, err_to_char.at(i2c_err));
67  freq = round(25000000 / (4096 * prescale)) - 1;
68  return freq;
69 }
const std::map< err_t, char * > err_to_char
Definition: arunaTypes.h:54
err_t read(uint8_t address, uint8_t reg, uint8_t *buffer, size_t buffer_size)
Read data from I²C slave.
Definition: I2C_master.cpp:7
static aruna::log::channel_t log
Definition: PCA9685.h:18
I2C_master * i2c_bus
Definition: PCA9685.h:114
const uint8_t i2c_address
Definition: PCA9685.h:113
int error(const char *format,...)
log error message
Definition: log.cpp:90
Here is the call graph for this function:

◆ is_connected()

err_t PCA9685::is_connected ( )

Is the PCA9685 connected to the I²C bus.

Note
only checks if a device with said I²C address returns an ACK.
Returns
I²C error code.

Definition at line 114 of file PCA9685.cpp.

114  {
115  uint8_t fake_buffer;
116  return i2c_bus->read(i2c_address, 0, fake_buffer);
117 }
err_t read(uint8_t address, uint8_t reg, uint8_t *buffer, size_t buffer_size)
Read data from I²C slave.
Definition: I2C_master.cpp:7
I2C_master * i2c_bus
Definition: PCA9685.h:114
const uint8_t i2c_address
Definition: PCA9685.h:113
Here is the call graph for this function:

◆ set_duty()

err_t PCA9685::set_duty ( uint16_t  on,
uint16_t  off 
)

Set the on and off timing of the PCA9685 led.

Parameters
on12 bit time to set signal to high
off12 bit time to set signal to low
Returns
I²C bus error

Definition at line 79 of file PCA9685.cpp.

79  {
80  uint8_t reg = (uint8_t) register_address_pointer::LED0_ON_L + 4 * led;
81  uint8_t data[4];
82  data[0] = on;
83  data[1] = on >> 8;
84  data[2] = off;
85  data[3] = off >> 8;
86  return i2c_bus->write(i2c_address, reg, data, 4);
87 }
const uint8_t led
Definition: PCA9685.h:113
I2C_master * i2c_bus
Definition: PCA9685.h:114
err_t write(uint8_t address, uint8_t reg, uint8_t *data, size_t data_size)
Write data to the I²C bus.
Definition: I2C_master.cpp:15
const uint8_t i2c_address
Definition: PCA9685.h:113
Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ default_i2c_address

uint8_t static const aruna::driver::PCA9685::default_i2c_address = 0b1000000
staticprivate

Definition at line 112 of file PCA9685.h.

◆ i2c_address

const uint8_t aruna::driver::PCA9685::i2c_address
private

Definition at line 113 of file PCA9685.h.

◆ i2c_bus

I2C_master* aruna::driver::PCA9685::i2c_bus
private

Definition at line 114 of file PCA9685.h.

◆ led

const uint8_t aruna::driver::PCA9685::led
private

Definition at line 113 of file PCA9685.h.

◆ log

log::channel_t PCA9685::log
staticprivate

Definition at line 18 of file PCA9685.h.


The documentation for this class was generated from the following files: