Skip to content

Servo library #54

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Aug 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions cores/arduino/ard_sup/analog/ap3_analog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,9 +242,15 @@ bool power_adc_disable()

//Apollo3 is capapble of 14-bit ADC but Arduino defaults to 10-bit
//This modifies the global var that controls what is returned from analogRead()
void analogReadResolution(uint8_t bits)
ap3_err_t analogReadResolution(uint8_t bits)
{
if (bits > 16)
{
_analogBits = 16; // max out the resolution when this happens
return AP3_ERR;
}
_analogBits = bits;
return AP3_OK;
}

ap3_err_t ap3_adc_setup()
Expand Down Expand Up @@ -652,15 +658,28 @@ ap3_err_t servoWriteResolution(uint8_t res)
return AP3_OK;
}

uint8_t getServoResolution()
{
return(_servoWriteBits);
}

ap3_err_t servoWrite(uint8_t pin, uint32_t val)
{
return(servoWrite(pin, val, 544, 2400)); //Call servoWrite with Arduino default min/max microseconds. See: https://www.arduino.cc/en/Reference/ServoAttach
}

ap3_err_t servoWrite(uint8_t pin, uint32_t val, uint16_t minMicros, uint16_t maxMicros)
{
// Determine the high time based on input value and the current resolution setting
uint32_t fsv = (0x01 << _servoWriteBits); // full scale value for the current resolution setting
val = val % fsv; // prevent excess
uint32_t clk = AM_HAL_CTIMER_HFRC_3MHZ; // Using 3 MHz to get fine-grained control up to 20 ms wide
uint32_t fw = 60000; // 20 ms wide frame
uint32_t max = 6000; // max width of RC pwm pulse is 2 ms or 6000 counts
uint32_t min = 3000; // min width of RC pwm pulse is 1 ms or 3000 counts

//Convert microSeconds to PWM counts.
uint32_t min = minMicros * 3;
uint32_t max = maxMicros * 3;

uint32_t th = (uint32_t)(((max - min) * val) / fsv) + min;

return ap3_pwm_output(pin, th, fw, clk);
Expand Down
4 changes: 3 additions & 1 deletion cores/arduino/ard_sup/ap3_analog.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,15 @@ ap3_err_t ap3_change_channel(ap3_gpio_pad_t padNumber);

bool power_adc_disable();
uint16_t analogRead(uint8_t pinNumber);
void analogReadResolution(uint8_t bits);
ap3_err_t analogReadResolution(uint8_t bits);

ap3_err_t ap3_pwm_output(uint8_t pin, uint32_t th, uint32_t fw, uint32_t clk);
ap3_err_t analogWriteResolution(uint8_t res);
ap3_err_t analogWrite(uint8_t pin, uint32_t val);
ap3_err_t servoWriteResolution(uint8_t res);
uint8_t getServoResolution();
ap3_err_t servoWrite(uint8_t pin, uint32_t val);
ap3_err_t servoWrite(uint8_t pin, uint32_t val, uint16_t minMicros, uint16_t maxMicros);

ap3_err_t tone(uint8_t pin, uint32_t freq);
ap3_err_t tone(uint8_t pin, uint32_t freq, uint32_t duration);
Expand Down
2 changes: 1 addition & 1 deletion cores/arduino/ard_sup/ap3_gpio.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ extern const am_hal_gpio_pincfg_t g_AM_HAL_GPIO_INPUT_PULLDOWN;
#define AP3_GPIO_IS_VALID(pad) ((pad >= 0) && (pad < AP3_GPIO_MAX_PADS))

extern ap3_gpio_pad_t ap3_gpio_pin2pad(ap3_gpio_pin_t pin);
#define AP3_GPIO_PAD_UNUSED (-1)
#define AP3_GPIO_PAD_UNUSED (255)

#define AP3_GPIO_DEFAULT_PINCFG AP3_GPIO_PINCFG_NULL

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
Author: Nathan Seidle
SparkFun Electronics
Created: August 18th, 2019
License: MIT. See SparkFun Arduino Apollo3 Project for more information

Purchasing from SparkFun helps write code like this and helps us
release products open source so that we can help each other learn: https://www.sparkfun.com/artemis

This example demonstrates the control of a servo on pin 8 on the RedBoard Artemis. Any PWM
pin can control a servo.

Hardware Connections:
Load this code
Connect a Servo to pin 18:
Red Wire -> 3.3V or 5V
Black Wire -> GND
Signal (Yellow or White) -> 8

The servo will rotate back and forth.
*/

#include <Servo.h>

Servo myServo;

int pos = 0;

void setup()
{
Serial.begin(9600);
Serial.println("SparkFun Servo Example");

myServo.attach(8);
}

void loop()
{
//Sweep
for (pos = 0; pos <= 180; pos++) { // goes from 0 degrees to 180 degrees
myServo.write(pos); // tell servo to go to position in variable 'pos'
delay(5); // waits 15ms for the servo to reach the position
}
for (pos = 180; pos >= 0; pos--) { // goes from 180 degrees to 0 degrees
myServo.write(pos); // tell servo to go to position in variable 'pos'
delay(5); // waits 15ms for the servo to reach the position
}
}
51 changes: 51 additions & 0 deletions libraries/Servo/examples/Example2_TwoServos/Example2_TwoServos.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
Author: Nathan Seidle
SparkFun Electronics
Created: August 18th, 2019
License: MIT. See SparkFun Arduino Apollo3 Project for more information

Purchasing from SparkFun helps write code like this and helps us
release products open source so that we can help each other learn: https://www.sparkfun.com/artemis

This example demonstrates controlling two servos.

Hardware Connections:
Load this code
Connect a Servo to pin 18:
Red Wire -> 3.3V or 5V
Black Wire -> GND
Signal (Yellow or White) -> 8

The servo will rotate back and forth.
*/

#include <Servo.h>

Servo myServoA;
Servo myServoB;

int pos = 0;

void setup()
{
Serial.begin(9600);
Serial.println("SparkFun Servo Example");

myServoA.attach(8);
myServoB.attach(7);
}

void loop()
{
//Sweep
for (pos = 0; pos <= 180; pos++) {
myServoA.write(pos);
myServoB.write(180 - pos);
delay(5);
}
for (pos = 180; pos >= 0; pos--) {
myServoA.write(pos);
myServoB.write(180 - pos);
delay(5);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
Author: Nathan Seidle
SparkFun Electronics
Created: August 18th, 2019
License: MIT. See SparkFun Arduino Apollo3 Project for more information

Purchasing from SparkFun helps write code like this and helps us
release products open source so that we can help each other learn: https://www.sparkfun.com/artemis

Different servos have different end points. Arduino defaults to 544us (min) and 2400us (max).
These however can be changed at startup. For more info see: https://www.arduino.cc/en/Reference/ServoAttach

Hardware Connections:
Load this code
Connect a Servo to pin 18:
Red Wire -> 3.3V or 5V
Black Wire -> GND
Signal (Yellow or White) -> 8

The servo will rotate back and forth.
*/

#include <Servo.h>

Servo myServoA;

void setup()
{
Serial.begin(9600);
Serial.println("SparkFun Servo Example");

myServoA.attach(8, 500, 2600); //Increase min/max to drive servo fully
}

void loop()
{
myServoA.write(180); //Go to max position
delay(2000);
myServoA.write(0); //Go to min position
delay(2000);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
Author: Nathan Seidle
SparkFun Electronics
Created: August 18th, 2019
License: MIT. See SparkFun Arduino Apollo3 Project for more information

Purchasing from SparkFun helps write code like this and helps us
release products open source so that we can help each other learn: https://www.sparkfun.com/artemis

Arduino defaults to 8-pin PWM resolution. This works for the majority of servos but can
be increased for projects that need it.

servoWriteResolution()
getServoResolution()

Are provided for changing and reading the resolution setting.

Hardware Connections:
Load this code
Connect a Servo to pin 18:
Red Wire -> 3.3V or 5V
Black Wire -> GND
Signal (Yellow or White) -> 8

The servo will rotate back and forth.
*/

#include <Servo.h>

Servo myServoA;

void setup()
{
Serial.begin(9600);
Serial.println("SparkFun Servo Example");

myServoA.attach(8);

servoWriteResolution(10); //Increase to 10-bit resolution. 16-bit is max.

int currentRes = getServoResolution();
Serial.print("Current resolution: ");
Serial.println(currentRes);
}

void loop()
{
myServoA.write(180); //Go to max position
delay(2000);
myServoA.write(0); //Go to min position
delay(2000);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
Author: Nathan Seidle
SparkFun Electronics
Created: August 18th, 2019
License: MIT. See SparkFun Arduino Apollo3 Project for more information

Purchasing from SparkFun helps write code like this and helps us
release products open source so that we can help each other learn: https://www.sparkfun.com/artemis

This example shows how to write to the servo in microseconds instead of position. This
is helpful for find the min/max us settings for a given servo manufacturer.

Hardware Connections:
Load this code
Connect a Servo to RX1:
Red Wire -> 3.3V or 5V
Black Wire -> GND
Signal (Yellow or White) -> RX1

The servo will rotate back and forth.
*/

#include <Servo.h>

Servo myServo;

void setup()
{
Serial.begin(9600);
Serial.println("SparkFun Servo Example");

myServo.attach(8);

myServo.writeMicroseconds(600); //Servo will go to very near the 0 degree position
}

void loop()
{
}
Loading