-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
3 changed files
with
85 additions
and
120 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
#include "teclas.h" | ||
|
||
|
||
Teclas::Teclas() { | ||
|
||
} | ||
|
||
void Teclas::initialize(){ | ||
pca1 = Adafruit_PWMServoDriver(0x41); | ||
pca2 = Adafruit_PWMServoDriver(0x40); | ||
pca1.begin(); | ||
pca1.setPWMFreq(50); // Analog servos run at ~50 Hz updates | ||
pca2.begin(); | ||
pca2.setPWMFreq(50); // Analog servos run at ~50 Hz updates | ||
|
||
for(int i = 0; i<NTECLAS; i++){ | ||
play_key(i); | ||
} | ||
delay(200); | ||
for(int i = 0; i < NTECLAS; i++){ | ||
key_up(i); | ||
} | ||
} | ||
|
||
void Teclas::play_key(int key){ | ||
if (key < 9){ | ||
pca1.writeMicroseconds(key, angle2PWM(40, key)); | ||
} else { | ||
pca2.writeMicroseconds(key - 9, angle2PWM(40, key-9)); | ||
} | ||
delay_down[key] = millis() + delay1; | ||
} | ||
|
||
void Teclas::key_up(int key){ | ||
if (key < 9){ | ||
pca1.writeMicroseconds(key, angle2PWM(50, key)); | ||
} else { | ||
pca2.writeMicroseconds(key - 9, angle2PWM(50, key-9)); | ||
} | ||
} | ||
|
||
|
||
|
||
|
||
void Teclas::play_loop(){ | ||
for(int i = 0; i<NTECLAS; i++){ | ||
if ((delay_down[i] != 0) and (millis() > delay_down[i])){ | ||
key_up(i); | ||
delay_down[i] = 0; | ||
} | ||
} | ||
} | ||
|
||
int Teclas::angle2PWM(double x, int i) { /* function angle2PWM */ | ||
////Convert joint angle into pwm command value | ||
int imp = (x - MIN_ANG[i]) * (MAX_IMP[i] - MIN_IMP[i]) / (MAX_ANG[i] - MIN_ANG[i]) + MIN_IMP[i]; | ||
imp = max(imp, MIN_IMP[i]); | ||
imp = min(imp, MAX_IMP[i]); | ||
|
||
return imp; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,28 @@ | ||
#include <Adafruit_PWMServoDriver.h> | ||
|
||
#define NTECLAS | ||
#define NTECLAS 18 | ||
|
||
#ifndef teclas_h | ||
#define teclas_h | ||
#include "Arduino.h" | ||
class Teclas { | ||
public: | ||
Teclas(); | ||
void initialize(); | ||
void play_key(int key); | ||
void play_loop(); | ||
private: | ||
int delay1; | ||
int delay2; | ||
int delay_up[NTECLAS]; | ||
int delay_down[NTECLAS]; | ||
int angle2PWM(double x, int i); | ||
void key_up(int key); | ||
int delay1 = 1000; | ||
int delay2 = 500; | ||
Adafruit_PWMServoDriver pca1; | ||
Adafruit_PWMServoDriver pca2; | ||
int delay_up[NTECLAS] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; | ||
int delay_down[NTECLAS] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; | ||
int MIN_IMP[NTECLAS] = { 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500 };; | ||
int MAX_IMP[NTECLAS] = { 2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500, 2500 }; | ||
int MIN_ANG[NTECLAS] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; | ||
int MAX_ANG[NTECLAS] = { 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180, 180 }; | ||
}; | ||
#endif |