Skip to content
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

Support esp32 boards #2038

Merged
merged 3 commits into from
Sep 15, 2020
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
3 changes: 3 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
.github/ISSUE_TEMPLATE/
packages/xod-cli/README.md
workspace/__ardulib__/*/
packages/xod-cli/bundle/
packages/xod-client-electron/src-babel/
8 changes: 5 additions & 3 deletions packages/xod-deploy-bin/src/arduinoCli.js
Original file line number Diff line number Diff line change
Expand Up @@ -433,13 +433,15 @@ export const listBoards = async (wsPath, cli) => {
available: res[1],
}))
.catch(async err => {
if (R.propEq('code', 6, err)) {
// Catch error produced by arduino-cli, but actually it's not an error:
if (R.propEq('code', 'ENOENT', err)) {
// When User added a new URL into `extra.txt` file it causes that
// arduino-cli tries to read new JSON but it's not existing yet
// so it fails with error "no such file or directory"
// To avoid this and make a good UX, we'll force call `updateIndexes`
return updateIndexesInternal(wsPath, cli).then(() => listBoards(cli));
// and then run `listBoards` again.
return updateIndexesInternal(wsPath, cli).then(() =>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment above is stale now. Needs update.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤔
It still actual, but I'll edit it a bit.

listBoards(wsPath, cli)
);
}

throw createError('UPDATE_INDEXES_ERROR_BROKEN_FILE', {
Expand Down
1 change: 1 addition & 0 deletions workspace/__ardulib__/ESP32Servo/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/.project
90 changes: 90 additions & 0 deletions workspace/__ardulib__/ESP32Servo/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# Servo Library for ESP32

This library attempts to faithfully replicate the semantics of the
Arduino Servo library (see http://www.arduino.cc/en/Reference/Servo)
for the ESP32, with two (optional) additions. The two new functions
expose the ability of the ESP32 PWM timers to vary timer width.
# Documentation by Doxygen

[ESP32Servo Doxygen](https://madhephaestus.github.io/ESP32Servo/annotated.html)

## License

Copyright (c) 2017 John K. Bennett. All right reserved.

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA

## Library Description:
```
  Servo - Class for manipulating servo motors connected to ESP32 pins.

int attach(pin ) - Attaches the given GPIO pin to the next free channel
(channels that have previously been detached are used first),
returns channel number or 0 if failure. All pin numbers are allowed,
but only pins 2,4,12-19,21-23,25-27,32-33 are recommended.

int attach(pin, min, max ) - Attaches to a pin setting min and max
values in microseconds; enforced minimum min is 500, enforced max
is 2500. Other semantics are the same as attach().

void write () - Sets the servo angle in degrees; a value below 500 is
treated as a value in degrees (0 to 180). These limit are enforced,
i.e., values are constrained as follows:
Value Becomes
----- -------
< 0 0
0 - 180 value (treated as degrees)
181 - 499 180
500 - (min-1) min
min-max (from attach or default) value (treated as microseconds)
(max+1) - 2500 max

void writeMicroseconds() - Sets the servo pulse width in microseconds.
min and max are enforced (see above).

int read() - Gets the last written servo pulse width as an angle between 0 and 180.

int readMicroseconds() - Gets the last written servo pulse width in microseconds.

bool attached() - Returns true if this servo instance is attached to a pin.

void detach() - Stops an the attached servo, frees the attached pin, and frees
its channel for reuse.
```

### **New ESP32-specific functions**

```
  setTimerWidth(value) - Sets the PWM timer width (must be 16-20) (ESP32 ONLY);
as a side effect, the pulse width is recomputed.

  int readTimerWidth() - Gets the PWM timer width (ESP32 ONLY)
```

### Useful Defaults:

default min pulse width for attach(): 544us

default max pulse width for attach(): 2400us

default timer width 16 (if timer width is not set)

default pulse width 1500us (servos are initialized with this value)

MINIMUM pulse with: 500us

MAXIMUM pulse with: 2500us

MAXIMUM number of servos: 16 (this is the number of PWM channels in the ESP32)
81 changes: 81 additions & 0 deletions workspace/__ardulib__/ESP32Servo/examples/Knob/Knob.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
Controlling a servo position using a potentiometer (variable resistor)
by Michal Rinott <http://people.interaction-ivrea.it/m.rinott>

modified on 8 Nov 2013
by Scott Fitzgerald

modified for the ESP32 on March 2017
by John Bennett

see http://www.arduino.cc/en/Tutorial/Knob for a description of the original code

* Different servos require different pulse widths to vary servo angle, but the range is
* an approximately 500-2500 microsecond pulse every 20ms (50Hz). In general, hobbyist servos
* sweep 180 degrees, so the lowest number in the published range for a particular servo
* represents an angle of 0 degrees, the middle of the range represents 90 degrees, and the top
* of the range represents 180 degrees. So for example, if the range is 1000us to 2000us,
* 1000us would equal an angle of 0, 1500us would equal 90 degrees, and 2000us would equal 1800
* degrees.
*
* Circuit: (using an ESP32 Thing from Sparkfun)
* Servo motors have three wires: power, ground, and signal. The power wire is typically red,
* the ground wire is typically black or brown, and the signal wire is typically yellow,
* orange or white. Since the ESP32 can supply limited current at only 3.3V, and servos draw
* considerable power, we will connect servo power to the VBat pin of the ESP32 (located
* near the USB connector). THIS IS ONLY APPROPRIATE FOR SMALL SERVOS.
*
* We could also connect servo power to a separate external
* power source (as long as we connect all of the grounds (ESP32, servo, and external power).
* In this example, we just connect ESP32 ground to servo ground. The servo signal pins
* connect to any available GPIO pins on the ESP32 (in this example, we use pin 18.
*
* In this example, we assume a Tower Pro SG90 small servo connected to VBat.
* The published min and max for this servo are 500 and 2400, respectively.
* These values actually drive the servos a little past 0 and 180, so
* if you are particular, adjust the min and max values to match your needs.
*/

// Include the ESP32 Arduino Servo Library instead of the original Arduino Servo Library
#include <ESP32Servo.h>

Servo myservo; // create servo object to control a servo

// Possible PWM GPIO pins on the ESP32: 0(used by on-board button),2,4,5(used by on-board LED),12-19,21-23,25-27,32-33
// Possible PWM GPIO pins on the ESP32-S2: 0(used by on-board button),1-17,18(used by on-board LED),19-21,26,33-42
int servoPin = 18; // GPIO pin used to connect the servo control (digital out)
// Possible ADC pins on the ESP32: 0,2,4,12-15,32-39; 34-39 are recommended for analog input
// Possible ADC pins on the ESP32-S2: 1-20 are recommended for analog input
#if defined(ARDUINO_ESP32S2_DEV)
int potPin = 10; // GPIO pin used to connect the potentiometer (analog in)
#else
int potPin = 34; // GPIO pin used to connect the potentiometer (analog in)
#endif
int ADC_Max = 4096; // This is the default ADC max value on the ESP32 (12 bit ADC width);
// this width can be set (in low-level oode) from 9-12 bits, for a
// a range of max values of 512-4096

int val; // variable to read the value from the analog pin

void setup()
{
// Allow allocation of all timers
ESP32PWM::allocateTimer(0);
ESP32PWM::allocateTimer(1);
ESP32PWM::allocateTimer(2);
ESP32PWM::allocateTimer(3);
myservo.setPeriodHertz(50);// Standard 50hz servo
myservo.attach(servoPin, 500, 2400); // attaches the servo on pin 18 to the servo object
// using SG90 servo min/max of 500us and 2400us
// for MG995 large servo, use 1000us and 2000us,
// which are the defaults, so this line could be
// "myservo.attach(servoPin);"
}

void loop() {
val = analogRead(potPin); // read the value of the potentiometer (value between 0 and 1023)
val = map(val, 0, ADC_Max, 0, 180); // scale it to use it with the servo (value between 0 and 180)
myservo.write(val); // set the servo position according to the scaled value
delay(200); // wait for the servo to get there
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
/*
* ESP32 Servo Example Using Arduino ESP32 Servo Library
* John K. Bennett
* March, 2017
*
* This sketch uses the Arduino ESP32 Servo Library to sweep 4 servos in sequence.
*
* Different servos require different pulse widths to vary servo angle, but the range is
* an approximately 500-2500 microsecond pulse every 20ms (50Hz). In general, hobbyist servos
* sweep 180 degrees, so the lowest number in the published range for a particular servo
* represents an angle of 0 degrees, the middle of the range represents 90 degrees, and the top
* of the range represents 180 degrees. So for example, if the range is 1000us to 2000us,
* 1000us would equal an angle of 0, 1500us would equal 90 degrees, and 2000us would equal 1800
* degrees.
*
* Circuit:
* Servo motors have three wires: power, ground, and signal. The power wire is typically red,
* the ground wire is typically black or brown, and the signal wire is typically yellow,
* orange or white. Since the ESP32 can supply limited current at only 3.3V, and servos draw
* considerable power, we will connect servo power to the VBat pin of the ESP32 (located
* near the USB connector). THIS IS ONLY APPROPRIATE FOR SMALL SERVOS.
*
* We could also connect servo power to a separate external
* power source (as long as we connect all of the grounds (ESP32, servo, and external power).
* In this example, we just connect ESP32 ground to servo ground. The servo signal pins
* connect to any available GPIO pins on the ESP32 (in this example, we use pins
* 22, 19, 23, & 18).
*
* In this example, we assume four Tower Pro SG90 small servos.
* The published min and max for this servo are 500 and 2400, respectively.
* These values actually drive the servos a little past 0 and 180, so
* if you are particular, adjust the min and max values to match your needs.
* Experimentally, 550 and 2350 are pretty close to 0 and 180.
*/

#include <ESP32Servo.h>

// create four servo objects
Servo servo1;
Servo servo2;
Servo servo3;
Servo servo4;
Servo servo5;
// Published values for SG90 servos; adjust if needed
int minUs = 1000;
int maxUs = 2000;

// These are all GPIO pins on the ESP32
// Recommended pins include 2,4,12-19,21-23,25-27,32-33
// for the ESP32-S2 the GPIO pins are 1-21,26,33-42
int servo1Pin = 15;
int servo2Pin = 16;
int servo3Pin = 14;
#if defined(ARDUINO_ESP32S2_DEV)
int servo4Pin = 13;
#else
int servo4Pin = 32;
#endif
int servo5Pin = 4;

int pos = 0; // position in degrees
ESP32PWM pwm;
void setup() {
// Allow allocation of all timers
ESP32PWM::allocateTimer(0);
ESP32PWM::allocateTimer(1);
ESP32PWM::allocateTimer(2);
ESP32PWM::allocateTimer(3);
Serial.begin(115200);
servo1.setPeriodHertz(50); // Standard 50hz servo
servo2.setPeriodHertz(50); // Standard 50hz servo
servo3.setPeriodHertz(330); // Standard 50hz servo
servo4.setPeriodHertz(200); // Standard 50hz servo
//servo5.setPeriodHertz(50); // Standard 50hz servo


}

void loop() {
servo1.attach(servo1Pin, minUs, maxUs);
servo2.attach(servo2Pin, minUs, maxUs);
#if defined(ARDUINO_ESP32S2_DEV)
pwm.attachPin(37, 10000);//10khz
#else
pwm.attachPin(27, 10000);//10khz
#endif
servo3.attach(servo3Pin, minUs, maxUs);
servo4.attach(servo4Pin, minUs, maxUs);

//servo5.attach(servo5Pin, minUs, maxUs);


for (pos = 0; pos <= 180; pos += 1) { // sweep from 0 degrees to 180 degrees
// in steps of 1 degree
servo1.write(pos);
delay(1); // waits 20ms for the servo to reach the position
}
for (pos = 180; pos >= 0; pos -= 1) { // sweep from 180 degrees to 0 degrees
servo1.write(pos);
delay(1);
}

for (pos = 0; pos <= 180; pos += 1) { // sweep from 0 degrees to 180 degrees
// in steps of 1 degree
servo2.write(pos);
delay(1); // waits 20ms for the servo to reach the position
}
for (pos = 180; pos >= 0; pos -= 1) { // sweep from 180 degrees to 0 degrees
servo2.write(pos);
delay(1);
}

for (pos = 0; pos <= 180; pos += 1) { // sweep from 0 degrees to 180 degrees
// in steps of 1 degree
servo3.write(pos);
delay(1); // waits 20ms for the servo to reach the position
}
for (pos = 180; pos >= 0; pos -= 1) { // sweep from 180 degrees to 0 degrees
servo3.write(pos);
delay(1);
}

for (pos = 0; pos <= 180; pos += 1) { // sweep from 0 degrees to 180 degrees
// in steps of 1 degree
servo4.write(pos);
delay(1); // waits 20ms for the servo to reach the position
}
for (pos = 180; pos >= 0; pos -= 1) { // sweep from 180 degrees to 0 degrees
servo4.write(pos);
delay(1);
}
for (pos = 0; pos <= 180; pos += 1) { // sweep from 0 degrees to 180 degrees
// in steps of 1 degree
servo5.write(pos);
delay(1); // waits 20ms for the servo to reach the position
}
for (pos = 180; pos >= 0; pos -= 1) { // sweep from 180 degrees to 0 degrees
servo5.write(pos);
delay(1);
}
servo1.detach();
servo2.detach();;
servo3.detach();
servo4.detach();
pwm.detachPin(27);

delay(5000);

}

Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include <ESP32Servo.h>
int APin = 13;
ESP32PWM pwm;
int freq = 1000;
void setup() {
// Allow allocation of all timers
ESP32PWM::allocateTimer(0);
ESP32PWM::allocateTimer(1);
ESP32PWM::allocateTimer(2);
ESP32PWM::allocateTimer(3);
Serial.begin(115200);
pwm.attachPin(APin, freq, 10); // 1KHz 8 bit

}
void loop() {

// fade the LED on thisPin from off to brightest:
for (float brightness = 0; brightness <= 0.5; brightness += 0.001) {
// Write a unit vector value from 0.0 to 1.0
pwm.writeScaled(brightness);
delay(2);
}
//delay(1000);
// fade the LED on thisPin from brithstest to off:
for (float brightness = 0.5; brightness >= 0; brightness -= 0.001) {
freq += 10;
// Adjust the frequency on the fly with a specific brightness
// Frequency is in herts and duty cycle is a unit vector 0.0 to 1.0
pwm.adjustFrequency(freq, brightness); // update the time base of the PWM
delay(2);
}
// pause between LEDs:
delay(1000);
freq = 1000;
pwm.adjustFrequency(freq, 0.0); // reset the time base
}
Loading