Skip to content

Commit 17a291c

Browse files
committed
Añade los ficheros base iniciales
1 parent abb35ab commit 17a291c

4 files changed

Lines changed: 323 additions & 0 deletions

File tree

PIDfromBT.cpp

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
2+
#if ARDUINO >= 100
3+
#include "Arduino.h"
4+
#else
5+
#include "WProgram.h"
6+
#endif
7+
8+
#include <PIDfromBT.h>
9+
10+
PIDfromBT::PIDfromBT(float* kp, float* ki, float* kd, int* vel, bool debug)
11+
{
12+
_kp = kp; // P
13+
_ki = ki; // I
14+
_kd = kd; // D
15+
_vel = vel; // V
16+
17+
_debug = debug;
18+
valor = "";
19+
letra = ' ';
20+
last_millis = millis();
21+
_type = TYPE_PIDV;
22+
}
23+
24+
PIDfromBT::PIDfromBT(float* kp, float* ki, float* kd, int* vel, int* ideal, bool debug)
25+
{
26+
_kp = kp; // P
27+
_ki = ki; // I
28+
_kd = kd; // D
29+
_vel = vel; // V
30+
_ideal = ideal; // I
31+
_maxIdeal = MAX_IDEAL;
32+
_minIdeal = MIN_IDEAL;
33+
34+
_debug = debug;
35+
valor = "";
36+
letra = ' ';
37+
last_millis = millis();
38+
_type = TYPE_PIDVI;
39+
}
40+
41+
PIDfromBT::PIDfromBT(float* kp, float* ki, float* kd, int* vel, int* ideal, int* suction, bool debug)
42+
{
43+
_kp = kp; // P
44+
_ki = ki; // I
45+
_kd = kd; // D
46+
_vel = vel; // V
47+
_ideal = ideal; // I
48+
_suction = suction; // S
49+
_maxIdeal = MAX_IDEAL;
50+
_minIdeal = MIN_IDEAL;
51+
52+
_debug = debug;
53+
valor = "";
54+
letra = ' ';
55+
last_millis = millis();
56+
_type = TYPE_PIDVIS;
57+
}
58+
59+
60+
void PIDfromBT::update() {
61+
if (Serial.available()) {
62+
// Si hay algo pendiente en el Buffer, lo lee y guarda el tiempo en el que lo leyo.
63+
_last_update = millis();
64+
byte readByte = Serial.read();
65+
66+
if ((isDigit((char)readByte) || (char)readByte == '.' || (char)readByte == '-')) {
67+
// Si es un dígito, signo decimal o signo negativo, lo guarda como un valor.
68+
_value += (char)readByte;
69+
}else{
70+
if(_letter != ' '){
71+
// Si es una letra y ya hay otra letra ya leída, ejecuta la acción correspondiente a esa letra.
72+
execute_task(_letter, (_value!="")?_value.toFloat():0);
73+
}
74+
// Llega una nueva letra; limpia el _value anterior y lee la nueva letra.
75+
_value = "";
76+
_letter = (char)readByte;
77+
}
78+
}else if((millis()-_last_update) > 5 && _letter != ' '){
79+
// Si no se ha recibido nada en los últimos 5ms y hay alguna letra pendiente, ejecuta su acción correspondiente.
80+
execute_task(_letter, (_value!="")?_value.toFloat():0);
81+
_letter = ' ';
82+
_value = "";
83+
}
84+
}
85+
86+
87+
void PIDfromBT::execute_task(char letter, float value)
88+
{
89+
switch (letter) {
90+
case 'P':
91+
if(_type < TYPE_PIDV)return;
92+
*_kp = valor;
93+
if (print) {
94+
Serial.println("Kp: " + String(value));
95+
}
96+
break;
97+
case 'I':
98+
if(_type < TYPE_PIDV)return;
99+
*_ki = value;
100+
if (print) {
101+
Serial.println("Ki: " + String(value));
102+
}
103+
break;
104+
case 'D':
105+
if(_type < TYPE_PIDV)return;
106+
*_kd = value;
107+
if (print) {
108+
Serial.println("Kd: " + String(value));
109+
}
110+
break;
111+
case 'V':
112+
if(_type < TYPE_PIDV)return;
113+
*_vel = value;
114+
if (print) {
115+
Serial.println("Vel: " + String(value));
116+
}
117+
break;
118+
case 'X':
119+
if(_type < TYPE_PIDVI)return;
120+
*_ideal = map(value, -1000, 1000, minIdeal, maxIdeal);;
121+
if (print) {
122+
Serial.println("Ideal: " + String(*ideal));
123+
}
124+
break;
125+
case 'S':
126+
if(_type < TYPE_PIDVS)return;
127+
*_suction = value.toInt();
128+
if (print) {
129+
Serial.println("Suc: " + String(value));
130+
}
131+
break;
132+
}
133+
}
134+
135+
void PIDfromBT::setMinIdeal(int minIdeal){
136+
_minIdeal = minIdeal;
137+
}
138+
void PIDfromBT::setMaxIdeal(int maxIdeal){
139+
_maxIdeal = maxIdeal;
140+
}

PIDfromBT.h

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#ifndef PIDfromBT_h
2+
#define PIDfromBT_h
3+
#define LIBRARY_VERSION 1.0
4+
#include <SoftwareSerial.h>
5+
class PIDfromBT
6+
{
7+
8+
#define DEBUG 1
9+
#define NO_DEBUG 0
10+
11+
#define MIN_IDEAL 0
12+
#define MAX_IDEAL 500
13+
14+
#define TYPE_PIDV 1
15+
#define TYPE_PIDVI 2
16+
#define TYPE_PIDVIS 3
17+
18+
public:
19+
20+
// PIDV
21+
PIDfromBT(float* kp, float* kd, float* ki, int* vel, bool debug);
22+
23+
// PIDVI
24+
PIDfromBT(float* kp, float* kd, float* ki, int* vel, int* ideal, bool debug);
25+
26+
// PIDVIS
27+
PIDfromBT(float* kp, float* kd, float* ki, int* vel, int* ideal, int* suction, bool debug);
28+
29+
void setMinIdeal(int minIdeal);
30+
31+
void setMaxIdeal(int maxIdeal);
32+
33+
bool update();
34+
35+
private:
36+
void execute_task();
37+
String _value;
38+
char _letter;
39+
long _last_update;
40+
bool _debug;
41+
int _type;
42+
float *_kp;
43+
float *_ki;
44+
float *_kd;
45+
int *_vel;
46+
int *_ideal;
47+
int *_suction;
48+
int _maxIdeal;
49+
int _minIdeal;
50+
};
51+
#endif

SoftwareSerial.h

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
/*
2+
SoftwareSerial.h (formerly NewSoftSerial.h) -
3+
Multi-instance software serial library for Arduino/Wiring
4+
-- Interrupt-driven receive and other improvements by ladyada
5+
(http://ladyada.net)
6+
-- Tuning, circular buffer, derivation from class Print/Stream,
7+
multi-instance support, porting to 8MHz processors,
8+
various optimizations, PROGMEM delay tables, inverse logic and
9+
direct port writing by Mikal Hart (http://www.arduiniana.org)
10+
-- Pin change interrupt macros by Paul Stoffregen (http://www.pjrc.com)
11+
-- 20MHz processor support by Garrett Mace (http://www.macetech.com)
12+
-- ATmega1280/2560 support by Brett Hagman (http://www.roguerobotics.com/)
13+
14+
This library is free software; you can redistribute it and/or
15+
modify it under the terms of the GNU Lesser General Public
16+
License as published by the Free Software Foundation; either
17+
version 2.1 of the License, or (at your option) any later version.
18+
19+
This library is distributed in the hope that it will be useful,
20+
but WITHOUT ANY WARRANTY; without even the implied warranty of
21+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22+
Lesser General Public License for more details.
23+
24+
You should have received a copy of the GNU Lesser General Public
25+
License along with this library; if not, write to the Free Software
26+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
27+
28+
The latest version of this library can always be found at
29+
http://arduiniana.org.
30+
*/
31+
32+
#ifndef SoftwareSerial_h
33+
#define SoftwareSerial_h
34+
35+
#include <inttypes.h>
36+
#include <Stream.h>
37+
38+
/******************************************************************************
39+
* Definitions
40+
******************************************************************************/
41+
42+
#ifndef _SS_MAX_RX_BUFF
43+
#define _SS_MAX_RX_BUFF 64 // RX buffer size
44+
#endif
45+
46+
#ifndef GCC_VERSION
47+
#define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
48+
#endif
49+
50+
class SoftwareSerial : public Stream
51+
{
52+
private:
53+
// per object data
54+
uint8_t _receivePin;
55+
uint8_t _receiveBitMask;
56+
volatile uint8_t *_receivePortRegister;
57+
uint8_t _transmitBitMask;
58+
volatile uint8_t *_transmitPortRegister;
59+
volatile uint8_t *_pcint_maskreg;
60+
uint8_t _pcint_maskvalue;
61+
62+
// Expressed as 4-cycle delays (must never be 0!)
63+
uint16_t _rx_delay_centering;
64+
uint16_t _rx_delay_intrabit;
65+
uint16_t _rx_delay_stopbit;
66+
uint16_t _tx_delay;
67+
68+
uint16_t _buffer_overflow:1;
69+
uint16_t _inverse_logic:1;
70+
71+
// static data
72+
static uint8_t _receive_buffer[_SS_MAX_RX_BUFF];
73+
static volatile uint8_t _receive_buffer_tail;
74+
static volatile uint8_t _receive_buffer_head;
75+
static SoftwareSerial *active_object;
76+
77+
// private methods
78+
inline void recv() __attribute__((__always_inline__));
79+
uint8_t rx_pin_read();
80+
void setTX(uint8_t transmitPin);
81+
void setRX(uint8_t receivePin);
82+
inline void setRxIntMsk(bool enable) __attribute__((__always_inline__));
83+
84+
// Return num - sub, or 1 if the result would be < 1
85+
static uint16_t subtract_cap(uint16_t num, uint16_t sub);
86+
87+
// private static method for timing
88+
static inline void tunedDelay(uint16_t delay);
89+
90+
public:
91+
// public methods
92+
SoftwareSerial(uint8_t receivePin, uint8_t transmitPin, bool inverse_logic = false);
93+
~SoftwareSerial();
94+
void begin(long speed);
95+
bool listen();
96+
void end();
97+
bool isListening() { return this == active_object; }
98+
bool stopListening();
99+
bool overflow() { bool ret = _buffer_overflow; if (ret) _buffer_overflow = false; return ret; }
100+
int peek();
101+
102+
virtual size_t write(uint8_t byte);
103+
virtual int read();
104+
virtual int available();
105+
virtual void flush();
106+
operator bool() { return true; }
107+
108+
using Print::write;
109+
110+
// public only for easy access by interrupt handlers
111+
static inline void handle_interrupt() __attribute__((__always_inline__));
112+
};
113+
114+
// Arduino 0012 workaround
115+
#undef int
116+
#undef char
117+
#undef long
118+
#undef byte
119+
#undef float
120+
#undef abs
121+
#undef round
122+
123+
#endif

library.properties

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name=PIDfromBTlib
2+
version=2.0.0
3+
author=Alex Santos
4+
maintainer=Alex Santos <contacto.oprobots@gmail.com>
5+
sentence=Permite el calibrado del algoritmo PID junto con otros parámetros.
6+
paragraph=Con esta librería podrás calibrar facilmente el PID y la velocidad de cualquier sistema, usando la app Android asociada a la misma.
7+
category=Communication
8+
url=https://github.com/robotaleh/PIDfromBTlib
9+
architectures=*

0 commit comments

Comments
 (0)