codigo braço robotico2
codigo braço robotico2
// ---------------------------------------------------------------
// ---------------------------------------------------------------
// Libraries
#include <EEPROM.h>
#include <Servo.h>
// ---------------------------------------------------------------
// ---------------------------------------------------------------
// Class - Joystick
class Joystick {
// -----------------------
public:
// -----------------------
// Constructor
// @params {pinX} : the pin for the X axis [int]
// {pinY} : the pin for the Y axis [int]
// {pinBtn} : the pin for the button [int] (default -1)
Joystick(int pinX, int pinY, int pinBtn = -1){
_pinX = pinX;
_pinY = pinY;
_pinBtn = pinBtn;
pinMode(_pinX, INPUT);
pinMode(_pinY, INPUT);
if(_pinBtn > -1)
pinMode(_pinBtn, INPUT_PULLUP);
// -----------------------
// Calibrate the mean axis of both axis
// @params {stream} : the stream to output the debug messages [Stream*]
(default NULL)
void calibrate(Stream *stream = NULL){
if(stream != NULL)
stream->print(F("Calibrating axis..."));
// calibrate
_meanX = 0; // reset
_meanY = 0; // reset
const int iterations = 10;
for(int i=0 ; i < iterations ; i++){
_meanX += getX();
_meanY += getY();
}
_meanX /= iterations;
_meanY /= iterations;
if(stream != NULL)
stream->println(F(" done!"));
}
// -----------------------
// Get the value of the X axis
// @returns the X axis value [int]
int getX(void){
return get(&_pinX);
}
// -----------------------
// Get the value of the Y axis
// @returns the Y axis value [int]
int getY(void){
return get(&_pinY);
}
// -----------------------
// Get the value of the button
// @returns the button value [int]
int getButton(void){
return get(&_pinBtn);
}
// -----------------------
// Get the mean value of the X axis
// @returns the X axis mean value [int]
int meanX(void){
return _meanX;
}
// -----------------------
// Get the mean value of the Y axis
// @returns the Y axis mean value [int]
int meanY(void){
return _meanY;
}
// -----------------------
// Print method overriden
size_t printTo(Print& p) const {
size_t size = 0;
size += p.print(F("x{ "));
size += p.print(getX());
size += p.print(F(" ; "));
size += p.print(meanX());
size += p.print(F(" } y{ "));
size += p.print(getY());
size += p.print(F(" ; "));
size += p.print(meanY());
if(_pinBtn > -1){
size += p.print(F(" } b{ "));
size += p.print(getButton());
}
size += p.println(F(" }"));
return size;
}
// -----------------------
private:
int _pinX, _pinY, _pinBtn;
int _meanX, _meanY;
// -----------------------
// Get the value of a pin
// @params {pin*} : the pointer to the pin [int*]
// @returns [int]
// > [0 - 1023] for an analog pin
// > [HIGH / LOW] for a digital pin
int get(int *pin){
if(pin == &_pinBtn){
return digitalRead(*pin);
} else {
analogRead(*pin); // firt call to change the channel
delay(5); // pause to charge the sample & hold circuit
return analogRead(*pin);
}
}
};
// ---------------------------------------------------------------
// Class - RoboServo
#define ROBOSERVO_MIN 0
#define ROBOSERVO_MAX 1
// -----------------------
// Attach the servo to the predefined pin
// @returns [uint8_t]
// > 0 if failure
// > the channel number
uint8_t attach(void){
Servo::attach(_pin); // attach the pin
int mean = _limits[ROBOSERVO_MIN] + _limits[ROBOSERVO_MAX];
mean /= 2;
Servo::write(mean); // default angle
}
// -----------------------
// Get the MAX limit
// @returns [int]
int getMAX(void){
return getLimit(ROBOSERVO_MAX);
}
// -----------------------
// Get the MIN limit
// @returns [int]
int getMIN(void){
return getLimit(ROBOSERVO_MIN);
}
// -----------------------
// Print method overriden
size_t printTo(Print& p) const {
size_t size = 0;
size += p.print(Servo::attached());
size += p.print(F(" { "));
size += p.print(_limits[ROBOSERVO_MIN]);
size += p.print(F(" ; "));
size += p.print(Servo::read());
size += p.print(F(" ; "));
size += p.print(_limits[ROBOSERVO_MAX]);
size += p.println(F(" }"));
return size;
}
// -----------------------
// Set the MAX limit
// @params {value} : the maximum angle [int]
void setMAX(int value){
setLimit(ROBOSERVO_MAX, value);
}
// -----------------------
// Set the MIN limit
// @params {value} : the minimum angle [int]
void setMIN(int value){
setLimit(ROBOSERVO_MIN, value);
}
// -----------------------
// Write the angle
// @params {value} : the desired angle [int]
void write(int value){
// constrain to limits
if(value < _limits[ROBOSERVO_MIN])
value = _limits[ROBOSERVO_MIN];
if(value > _limits[ROBOSERVO_MAX])
value = _limits[ROBOSERVO_MAX];
// -----------------------
private:
int _limits[2]; // { min , max }
int _pin;
// -----------------------
// Get an angle limit
// @params {index} : the desired limit [byte]
// @returns [int]
int getLimit(byte index){
if(index == ROBOSERVO_MAX){
return _limits[ROBOSERVO_MAX];
} else {
return _limits[ROBOSERVO_MIN]; // default
}
}
// -----------------------
// Set an angle limit
// @params {index} : the desired limit [byte]
// {value} : the desired angle [int]
void setLimit(byte index, int value){
// check the index (redundant)
if(index > ROBOSERVO_MAX)
return;
#undef ROBOSERVO_MIN
#undef ROBOSERVO_MAX
// ---------------------------------------------------------------
// ---------------------------------------------------------------
// Variables
#define SERVO_STEP 5
enum Motor { Base , Reach , Height , Claw };
RoboServo servos[] = { RoboServo(8) , RoboServo(9) , RoboServo(10) ,
RoboServo(11) };
// ---------------------------------------------------------------
// ---------------------------------------------------------------
// Prototypes
// ---------------------------------------------------------------
// ---------------------------------------------------------------
void setup(){
// configure the serial output
Serial.begin(115200);
Serial.println(F("RoboARM - Demo"));
Serial.println(F("\t\t10/12/18\n"));
#ifndef RESET_CONFIG
ReadConfig();
#endif
// ---------------------------------------------------------------
void loop(){
// -----------------------
// -----------------------
if(Serial.available()){
char c = Serial.read();
switch(c){
// INFO
case 'i': {
Serial.print(F("Left: "));
jLeft.printTo(Serial);
Serial.print(F("Right: "));
jRight.printTo(Serial);
Serial.println(F("-----"));
for(int i=0 ; i <= Motor::Claw ; i++){
Serial.print('[');
Serial.print(i);
Serial.print("] ");
servos[i].printTo(Serial);
}
break;
}
// SERVO SELECTION
case '1':
current_motor = Motor::Base;
Serial.println(F("Base selected"));
break;
case '2':
current_motor = Motor::Reach;
Serial.println(F("Reach selected"));
break;
case '3':
current_motor = Motor::Height;
Serial.println(F("Height selected"));
break;
case '4':
current_motor = Motor::Claw;
Serial.println(F("Claw selected"));
break;
// ANGLE
case 'a': {
int angle = servos[current_motor].read();
Serial.print(F("Current angle: "));
Serial.println(angle);
break;
}
// ANGLE +
case '+': {
int angle = servos[current_motor].read();
angle += SERVO_STEP;
angle = AngleCorrection(current_motor, angle); // validate the angle
servos[current_motor].write(angle);
Serial.print(F("Current angle: "));
Serial.println(angle);
break;
}
// ANGLE -
case '-': {
int angle = servos[current_motor].read();
angle -= SERVO_STEP;
angle = AngleCorrection(current_motor, angle); // validate the angle
servos[current_motor].write(angle);
Serial.print(F("Current angle: "));
Serial.println(angle);
break;
}
// SAVE - MIN
case '<': {
int angle = servos[current_motor].read();
servos[current_motor].setMIN(angle);
servos[current_motor].printTo(Serial);
break;
}
// SAVE - MAX
case '>': {
int angle = servos[current_motor].read();
servos[current_motor].setMAX(angle);
servos[current_motor].printTo(Serial);
break;
}
}
}
// -----------------------
// -----------------------
// -----------------------
// -----------------------
// -----------------------
delay(delay_loop);
// ---------------------------------------------------------------
// ---------------------------------------------------------------
if(motor == Height){
int max_angle = servos[Reach].read();
max_angle = (float)max_angle * ANGLE_CORRECTION_A;
max_angle += ANGLE_CORRECTION_B;
return angle;
}
// ---------------------------------------------------------------
#define EEPROM_ADDRESS_START 10
#define EEPROM_DEFAULT_MIN_BASE 10
#define EEPROM_DEFAULT_MIN_REACH 60
#define EEPROM_DEFAULT_MIN_HEIGHT 60
#define EEPROM_DEFAULT_MIN_CLAW 100
#define EEPROM_DEFAULT_MAX_BASE 170
#define EEPROM_DEFAULT_MAX_REACH 170
#define EEPROM_DEFAULT_MAX_HEIGHT 170
#define EEPROM_DEFAULT_MAX_CLAW 170
if(blank){
// write data
EEPROM.write(address++, EEPROM_DEFAULT_MIN_BASE);
EEPROM.write(address++, EEPROM_DEFAULT_MIN_REACH);
EEPROM.write(address++, EEPROM_DEFAULT_MIN_HEIGHT);
EEPROM.write(address++, EEPROM_DEFAULT_MIN_CLAW);
EEPROM.write(address++, EEPROM_DEFAULT_MAX_BASE);
EEPROM.write(address++, EEPROM_DEFAULT_MAX_REACH);
EEPROM.write(address++, EEPROM_DEFAULT_MAX_HEIGHT);
EEPROM.write(address++, EEPROM_DEFAULT_MAX_CLAW);
// read data
servos[Base].setMIN(EEPROM.read(address++));
servos[Reach].setMIN(EEPROM.read(address++));
servos[Height].setMIN(EEPROM.read(address++));
servos[Claw].setMIN(EEPROM.read(address++));
servos[Base].setMAX(EEPROM.read(address++));
servos[Reach].setMAX(EEPROM.read(address++));
servos[Height].setMAX(EEPROM.read(address++));
servos[Claw].setMAX(EEPROM.read(address++));
}
#undef EEPROM_ADDRESS_START
// ---------------------------------------------------------------