Skip to content

Commit

Permalink
align examples
Browse files Browse the repository at this point in the history
  • Loading branch information
timhendriks93 committed Jul 27, 2024
1 parent e786e8b commit e750332
Show file tree
Hide file tree
Showing 19 changed files with 156 additions and 92 deletions.
22 changes: 8 additions & 14 deletions examples/AdafruitPCA9685/AdafruitPCA9685.ino
Original file line number Diff line number Diff line change
@@ -1,34 +1,28 @@
/*
Using the Adafruit PCA9685 PWM Servo Driver Library to send servo positions.
Using an Adafruit PCA9685 module to send servo positions.

This approach is especially useful when the animation is based on multiple
servos. We assume the servo ID and the used board channel are equal.
Therefore, the servo with the ID 0 has to be connected to channel 0 etc.
This approach is especially useful when the animation is based on multiple servos. We assume the
servo ID and the used board channel are equal. Therefore, the servo with the ID 0 has to be
connected to channel 0 etc.
*/

#include "ik.h"
#include <Adafruit_PWMServoDriver.h>
#include <BlenderServoAnimation.h>

// Animation object to control the animation
BlenderServoAnimation animation;

// PWM driver instance to set PWM output
Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();
Adafruit_PWMServoDriver pwm;

// Callback function which is called whenever a servo needs to be moved
void move(byte servoID, int position) {
// We assume the servoID is equal to the used channel on the PCA9685
pwm.setPWM(servoID, 0, position);
}

// Animation object to control the animation
BlenderServoAnimation animation;

#define POWER_PIN 19

void setup() {
// Set power pin
pinMode(POWER_PIN, OUTPUT);
digitalWrite(POWER_PIN, HIGH);

// Set the position callback
animation.onPositionChange(move);

Expand Down
10 changes: 9 additions & 1 deletion examples/AdafruitPCA9685/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Adafruit PCA9685

Using a PCA9685 PWM Servo Driver to animate 2 servos. The animation is based on the IK example of the Blender Servo Animation add-on which resembles a simple neck mechanism.
Using an Adafruit PCA9685 module to send servo positions.

This approach is especially useful when the animation is based on multiple servos. We assume the servo ID and the used board channel are equal. Therefore, the servo with the ID 0 has to be connected to channel 0 etc.

## Library Dependencies

- [Adafruit-PWM-Servo-Driver-Library](https://github.com/adafruit/Adafruit-PWM-Servo-Driver-Library)

## Wiring Diagram

![Arduino Nano with PCA9685](../../images/arduino-nano-with-PCA9685.png)
26 changes: 14 additions & 12 deletions examples/MultiplePCA9685/MultiplePCA9685.ino
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
/*
Using a struct to map Servos to a PWM driver and channel.
Using two PCA9685 PWM servo drivers to animate 2 servos.

This approach can be useful if you don't want to equate the servo ID with the
PWM board channel or use multiple PCA9685 boards. The Adafruit PCA9685 PWM
Servo Driver Library is used to send the servo positions.
Note that the A0 address jumper has to be soldered on the second driver board. This setup can
easily be extended to animate up to 32 servos. If even more servos are needed, you can also add
more driver boards to the chain.

We assume the servo ID and the used board channel are equal. Therefore, the servo with the ID 0
has to be connected to channel 0 etc.
*/

#include "ik.h"
#include <Adafruit_PWMServoDriver.h>
#include <BlenderServoAnimation.h>

// PWM driver instances to set PWM output
Adafruit_PWMServoDriver pwmA(0x40);
Adafruit_PWMServoDriver pwmB(0x41);
#define SERVO_AMOUNT 2

// Animation object to control the animation
BlenderServoAnimation animation;

// PWM driver instances to set PWM output
Adafruit_PWMServoDriver pwmA(0x40);
Adafruit_PWMServoDriver pwmB(0x41);

// We use a struct to map a servo to a PCA9685 board and channel
struct servoMapping {
byte id;
Expand All @@ -25,21 +30,18 @@ struct servoMapping {
};

// Define an array of servo maps
servoMapping servoMappings[] = {
servoMapping servoMappings[SERVO_AMOUNT] = {
// Servo 0 attached to board A on channel 0
{0, pwmA, 0},

// Servo 1 attached to board B on channel 0
{1, pwmB, 0},
};

// Calculate the amount of servos so that we can easily extend the array
const byte servoAmount = sizeof(servoMappings) / sizeof(servoMappings[0]);

// Callback function which is called whenever a servo needs to be moved
void setPWM(byte servoID, int position) {
// Iterate through the available servos
for (int i = 0; i < servoAmount; i++) {
for (int i = 0; i < SERVO_AMOUNT; i++) {
// Continue if the current servo ID doesn't match the target servo ID
if (servoMappings[i].id != servoID) {
continue;
Expand Down
10 changes: 9 additions & 1 deletion examples/MultiplePCA9685/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
# Multiple PCA9685

Using two PCA9685 PWM Servo Drivers to animate 2 servos. The animation is based on the IK example of the Blender Servo Animation add-on which resembles a simple neck mechanism.
Using two PCA9685 PWM servo drivers to animate 2 servos.

Note that the A0 address jumper has to be soldered on the second driver board. This setup can easily be extended to animate up to 32 servos. If even more servos are needed, you can also add more driver boards to the chain.

We assume the servo ID and the used board channel are equal. Therefore, the servo with the ID 0 has to be connected to channel 0 etc.

## Library Dependencies

- [Adafruit-PWM-Servo-Driver-Library](https://github.com/adafruit/Adafruit-PWM-Servo-Driver-Library)

## Wiring Diagram

![Arduino Nano with 2 PCA9685](../../images/arduino-nano-with-2-PCA9685.png)
11 changes: 6 additions & 5 deletions examples/MultipleScenes/MultipleScenes.ino
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/*
Setting up an animation consisting of 2 scenes.
It's even possible to have different playback rates (fps)
and frames per animation.

The 2 scenes will be played synchronously in a loop. It's even possible to have different playback
rates (fps) and frames per animation.
*/

#include "scene-a.h"
Expand All @@ -16,6 +17,9 @@

#define SERVO_PIN 3

// Animation object to control the animation
BlenderServoAnimation animation;

// Servo object to send positions
Servo myServo;

Expand All @@ -25,9 +29,6 @@ void move(byte servoID, int position) {
myServo.writeMicroseconds(position);
}

// Animation object to control the animation
BlenderServoAnimation animation;

void setup() {
// Attach the servo to the defined servo pin
myServo.attach(SERVO_PIN);
Expand Down
11 changes: 9 additions & 2 deletions examples/MultipleScenes/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
# Multiple scenes
# Multiple Scenes

Setting up an animation consisting of 2 scenes.

By default, the 2 scenes will be played synchronously in a loop.
The 2 scenes will be played synchronously in a loop. It's even possible to have different playback rates (fps) and frames per animation.

## Library Dependencies

- [Servo](https://github.com/arduino-libraries/Servo)
- [ESP32Servo](https://github.com/madhephaestus/ESP32Servo) (alternatively, when using an ESP32)

## Wiring Diagram

![Arduino Nano with servo](../../images/arduino-nano-with-servo.png)
20 changes: 12 additions & 8 deletions examples/MultipleScenesSD/MultipleScenesSD.ino
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/*
Setting up a show consisting of 2 animations.
It's even possible to have different playback rates (fps)
and frames per animation.
Setting up an animation consisting of 2 scenes with their animation data stored on an SD card.

The 2 scenes will be played synchronously in a loop. It's even possible to have different playback
rates (fps) and frames per animation.
*/

#include <BlenderServoAnimation.h>
Expand All @@ -15,6 +16,7 @@

#define SERVO_PIN 3
#define CS_PIN 4
#define SCENE_AMOUNT = 2

// Servo object to send positions
Servo myServo;
Expand All @@ -30,27 +32,29 @@ struct sceneMapping {
};

// Define an array of scene maps
sceneMapping sceneMappings[] = {
sceneMapping sceneMappings[SCENE_AMOUNT] = {
// Scene 0 = Scene A
{30, 100, "scene-a.bin"},

// Scene 1 = Scene B
{60, 200, "scene-b.bin"},
};

// Calculate the amount of scenes so that we can easily extend the array
const byte sceneAmount = sizeof(sceneMappings) / sizeof(sceneMappings[0]);

// Callback function which is called whenever a servo needs to be moved
void move(byte servoID, int position) {
// Ignore the servoID (there is only one servo) and write the current position
myServo.writeMicroseconds(position);
}

// Callback function which is called whenever a scene is being changed
void changeSceneFile(byte prevSceneIndex, byte nextSceneIndex) {
// Get the filename of the next scene
String filename = sceneMappings[nextSceneIndex].filename;

// Close the current animation file which points to the SD
animationFile.close();

// Open the new file on the SD and set it as the new animation file
animationFile = SD.open(filename);

if (!animationFile) {
Expand Down Expand Up @@ -83,7 +87,7 @@ void setup() {
animation.onSceneChange(changeSceneFile);

// Add multiple scenes with the same File stream
for (byte i = 0; i < sceneAmount; i++) {
for (byte i = 0; i < SCENE_AMOUNT; i++) {
byte fps = sceneMappings[i].fps;
int frames = sceneMappings[i].frames;

Expand Down
14 changes: 11 additions & 3 deletions examples/MultipleScenesSD/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
# Show
# Multiple Scenes with SD Card Animation Data

Setting up a show consisting of 2 animations.
Setting up an animation consisting of 2 scenes with their animation data being read from an SD card.

By default, the 2 animations will be played synchronously in a loop.
The 2 scenes will be played synchronously in a loop. It's even possible to have different playback rates (fps) and frames per animation.

## Library Dependencies

- [SD](https://github.com/arduino-libraries/SD)
- [Servo](https://github.com/arduino-libraries/Servo)
- [ESP32Servo](https://github.com/madhephaestus/ESP32Servo) (alternatively, when using an ESP32)

## Wiring Diagram

![Arduino Nano with servo](../../images/arduino-nano-with-sd-module.png)
14 changes: 11 additions & 3 deletions examples/SDAnimation/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
# Standard Servo Library
# SD Card Animation Data

Using the standard Arduino servo library to send servo positions.
Reading animation data from an SD card.

The setup requires nothing but a micro controller and a single servo.
The setup requires nothing but a micro controller and a single servo. It uses the standard Arduino servo library to send servo positions.

## Library Dependencies

- [SD](https://github.com/arduino-libraries/SD)
- [Servo](https://github.com/arduino-libraries/Servo)
- [ESP32Servo](https://github.com/madhephaestus/ESP32Servo) (alternatively, when using an ESP32)

## Wiring Diagram

![Arduino Nano with servo](../../images/arduino-nano-with-sd-module.png)
9 changes: 5 additions & 4 deletions examples/SDAnimation/SDAnimation.ino
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
/*
Using the standard Arduino servo library to send servo positions.
Reading animation data from an SD card.

Note the namespace BlenderServoAnimation which helps to distinguish
between the standard library servo class (Servo) and the servo
class of this library (BlenderServoAnimation::Servo).
The setup requires nothing but a micro controller and a single servo. It uses the standard Arduino
servo library to send servo positions.
*/

#include <BlenderServoAnimation.h>
Expand Down Expand Up @@ -32,7 +31,9 @@ void move(byte servoID, int position) {
myServo.writeMicroseconds(position);
}

// Callback function which is called whenever a scene is being changed
void resetFile(byte prevSceneIndex, byte nextSceneIndex) {
// Seek back to the first position in the file
animationFile.seek(0);
}

Expand Down
7 changes: 7 additions & 0 deletions examples/SerialLiveMode/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,11 @@ Sending live servo positions via serial commands.

This example requires a USB connection to your PC and a running Blender instance with the Blender Servo Animation Add-on. After starting the live mode by connecting to the micro controller via serial, you can move the servo in real time via Blender.

## Library Dependencies

- [Servo](https://github.com/arduino-libraries/Servo)
- [ESP32Servo](https://github.com/madhephaestus/ESP32Servo) (alternatively, when using an ESP32)

## Wiring Diagram

![Arduino Nano with servo](../../images/arduino-nano-with-servo.png)
17 changes: 9 additions & 8 deletions examples/SerialLiveMode/SerialLiveMode.ino
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/*
Sending live servo positions via serial commands.

This example requires a USB connection to your PC and a running Blender
instance with the Blender Servo Animation Add-on. We use a single servo which
is controlled via the standard Arduino servo library.
This example requires a USB connection to your PC and a running Blender instance with the Blender
Servo Animation Add-on. After starting the live mode by connecting to the micro controller via
serial, you can move the servo in real time via Blender.
*/

#include <BlenderServoAnimation.h>
Expand All @@ -15,6 +15,10 @@
#endif

#define SERVO_PIN 12
#define BAUD_RATE 115200

// Animation object to control the animation
BlenderServoAnimation animation;

// Servo object to send positions
Servo myServo;
Expand All @@ -25,20 +29,17 @@ void move(byte servoID, int position) {
myServo.writeMicroseconds(position);
}

// Animation object to control the animation
BlenderServoAnimation animation;

void setup() {
// Initialize serial communication
Serial.begin(115200);
Serial.begin(BAUD_RATE);

// Attach the servo to the defined servo pin
myServo.attach(SERVO_PIN);

// Set the position callback
animation.onPositionChange(move);

// Trigger the animation live mode
// Trigger the animation live mode by passing the Serial instance
animation.live(Serial);
}

Expand Down
9 changes: 8 additions & 1 deletion examples/StandardServoLib/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

Using the standard Arduino servo library to send servo positions.

The setup requires nothing but a micro controller and a single servo.
The setup requires nothing but a micro controller and a single servo. The animation is played in a loop.

## Library Dependencies

- [Servo](https://github.com/arduino-libraries/Servo)
- [ESP32Servo](https://github.com/madhephaestus/ESP32Servo) (alternatively, when using an ESP32)

## Wiring Diagram

![Arduino Nano with servo](../../images/arduino-nano-with-servo.png)
4 changes: 3 additions & 1 deletion examples/StandardServoLib/StandardServoLib.ino
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/*
Using the standard Arduino servo library to send servo positions.
The animation is played in a loop.

The setup requires nothing but a micro controller and a single servo. The animation is played in a
loop.
*/

#include "simple.h"
Expand Down
Loading

0 comments on commit e750332

Please sign in to comment.