Mise au propre du code YATA
This commit is contained in:
@@ -1,30 +1,14 @@
|
|||||||
#include <digitalWriteFast.h>
|
#include <digitalWriteFast.h>
|
||||||
//this is to use DWF library, it will increase the speed of digitalRead/Write command
|
|
||||||
//used in the interrupt function doEncoderMotor0, but may be used everywhere.
|
|
||||||
/*
|
/*
|
||||||
|
Fortement inspiré du projet :
|
||||||
https://github.com/danithebest91/ServoStrap
|
https://github.com/danithebest91/ServoStrap
|
||||||
i have made this code for the LMD18245 motor controller,
|
|
||||||
i have merged the pid code of Josh Kopel
|
|
||||||
whith the code of makerbot servo-controller board,
|
|
||||||
you can use this code on the some board changing some values.
|
|
||||||
Daniele Poddighe
|
|
||||||
external ardware require a quadrature encoder, timing slit strip and a dc motor,
|
|
||||||
all you can find inside an old printer, i have took it from canon and hp printers(psc1510)
|
|
||||||
|
|
||||||
for motor controll you can choose different type of H-bridge, i have used LMD18245,
|
|
||||||
you can order 3 of it on ti.com sample request, the hardware needed is explained on the datasheet but i'm drowing
|
|
||||||
the schematic and PCB layout on eagle.
|
|
||||||
|
|
||||||
|
|
||||||
read a rotary encoder with interrupts
|
|
||||||
Encoder hooked up with common to GROUND,
|
|
||||||
encoder0PinA to pin 2, encoder0PinB to pin 4 (or pin 3 see below)
|
|
||||||
it doesn't matter which encoder pin you use for A or B
|
|
||||||
|
|
||||||
is possible to change PID costants by sending on serial interfaces the values separated by ',' in this order: KP,KD,KI
|
is possible to change PID costants by sending on serial interfaces the values separated by ',' in this order: KP,KD,KI
|
||||||
example: 5.2,3.1,0 so we have KP=5.2 KD=3.1 KI=0 is only for testing purposes, but i will leave this function with eeprom storage
|
example: 5.2,3.1,0 so we have KP=5.2 KD=3.1 KI=0 is only for testing purposes, but i will leave this function with eeprom storage
|
||||||
|
|
||||||
This code use Port manipulation :
|
Utilisation de la librairies digitalWriteFast qui permet d'utiliser la manipulation de port pour accélérer les lectures écritures de certaine entree/sortie :
|
||||||
|
|
||||||
Port registers allow for lower-level and faster manipulation of the i/o pins of the microcontroller on an Arduino board. The chips used on the Arduino board
|
Port registers allow for lower-level and faster manipulation of the i/o pins of the microcontroller on an Arduino board. The chips used on the Arduino board
|
||||||
(the ATmega8 and ATmega168) have three ports:
|
(the ATmega8 and ATmega168) have three ports:
|
||||||
B (digital pin 8 to 13)
|
B (digital pin 8 to 13)
|
||||||
@@ -32,34 +16,46 @@ https://github.com/danithebest91/ServoStrap
|
|||||||
D (digital pins 0 to 7)
|
D (digital pins 0 to 7)
|
||||||
https://www.arduino.cc/en/Reference/PortManipulation
|
https://www.arduino.cc/en/Reference/PortManipulation
|
||||||
|
|
||||||
|
Petit rappel sur la puce arduino :
|
||||||
https://www.arduino.cc/en/Hacking/PinMapping
|
https://www.arduino.cc/en/Hacking/PinMapping
|
||||||
https://www.arduino.cc/en/Hacking/PinMapping168
|
https://www.arduino.cc/en/Hacking/PinMapping168
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#define encoder0PinA 2 // PD2 (INT0) You can't modify port mapping here because this code use Port manipulation (I2)
|
// --------------- INPUT ---------------------------------------------------
|
||||||
|
|
||||||
|
// Les ports de l'encoder sont 2 pour bénéficier de l'interuption sur A et 8 pour utiliser le PortManipulation. Attention sur le port manipulation à ne pas utiliser les pin 9 à 13 !
|
||||||
|
#define encoder0PinA 2 // PD2 (INT0)
|
||||||
#define encoder0PinB 8 // PB0; (I8)
|
#define encoder0PinB 8 // PB0; (I8)
|
||||||
|
|
||||||
|
// Utilisation de 5 et 6 pour la commande moteur via le driver L293D. Le enable est connecté au vcc et le pwm est envoyé directement dans les input.
|
||||||
#define MotorIN1 5 //(I5) IN1
|
#define MotorIN1 5 //(I5) IN1
|
||||||
#define MotorIN2 6 //(I6) IN2
|
#define MotorIN2 6 //(I6) IN2
|
||||||
|
|
||||||
//from ramps 1.4 stepper driver
|
// Recuperation des step de la carte avec interuption sur le port 3 et PortManipulation sur le port A0. Attention sur le port manipulation à ne pas utiliser les pin A1 à A5 !
|
||||||
#define STEP_PIN 3 //PD3 (INT1) (I3)
|
#define STEP_PIN 3 //PD3 (INT1) (I3)
|
||||||
#define DIR_PIN 14 //PC0; (A0)
|
#define DIR_PIN 14 //PC0; (A0)
|
||||||
|
|
||||||
|
// Récupération du fin de course x_min pour l'initialisation du moteur
|
||||||
#define X_MIN 4 //(4)
|
#define X_MIN 4 //(4)
|
||||||
|
|
||||||
|
// ------------------------------------------------------------------------
|
||||||
|
|
||||||
|
// ------------- CONSTANTE DU PID -------------
|
||||||
|
|
||||||
|
float KP = 5 ; //Porportionnel
|
||||||
|
float KI = 0.1; // Intergrale
|
||||||
|
float KD = 2.0; // derive
|
||||||
|
|
||||||
|
// ------------- CONSTANTE DU PID -------------
|
||||||
|
|
||||||
|
// Initialisation de la position encoder. Attention celle-ci est incrémenté via un CHANGE sur l'interuption 0. Le nombre de pas de la roue codeuse est donc double !
|
||||||
volatile long encoder0Pos = 0;
|
volatile long encoder0Pos = 0;
|
||||||
|
|
||||||
long target = 0;
|
long target = 0;
|
||||||
long target1 = 0;
|
long target1 = 0;
|
||||||
int StartRoutine=0;
|
|
||||||
//correction = Kp * error + Kd * (error - prevError) + kI * (sum of errors)
|
|
||||||
//PID controller constants
|
|
||||||
float KP = 5 ; //position multiplier (gain) 2.25
|
|
||||||
float KI = 0.1; // Intergral multiplier (gain) .25
|
|
||||||
float KD = 2.0; // derivative multiplier (gain) 1.0
|
|
||||||
|
|
||||||
int lastError = 0;
|
int lastError = 0;
|
||||||
int sumError = 0;
|
int sumError = 0;
|
||||||
|
int StartRoutine=0;
|
||||||
|
|
||||||
//Integral term min/max (random value and not yet tested/verified)
|
//Integral term min/max (random value and not yet tested/verified)
|
||||||
int iMax = 100;
|
int iMax = 100;
|
||||||
@@ -84,13 +80,17 @@ void setup() {
|
|||||||
pinModeFast(MotorIN1, OUTPUT);
|
pinModeFast(MotorIN1, OUTPUT);
|
||||||
pinModeFast(MotorIN2, OUTPUT);
|
pinModeFast(MotorIN2, OUTPUT);
|
||||||
|
|
||||||
//ramps 1.4 motor control
|
// Initailisation recuperation commande carte
|
||||||
pinModeFast(STEP_PIN, INPUT);
|
pinModeFast(STEP_PIN, INPUT);
|
||||||
pinModeFast(DIR_PIN, INPUT);
|
pinModeFast(DIR_PIN, INPUT);
|
||||||
|
|
||||||
|
// INTERUPTION
|
||||||
|
|
||||||
attachInterrupt(0, doEncoderMotor0, CHANGE); // encoder pin on interrupt 0 - pin 2
|
attachInterrupt(0, doEncoderMotor0, CHANGE); // encoder pin on interrupt 0 - pin 2
|
||||||
attachInterrupt(1, countStep, RISING); //on pin 3
|
attachInterrupt(1, countStep, RISING); //on pin 3
|
||||||
|
|
||||||
|
// INTERUPTION
|
||||||
|
|
||||||
Serial.begin (115200);
|
Serial.begin (115200);
|
||||||
Serial.println("start"); // a personal quirk
|
Serial.println("start"); // a personal quirk
|
||||||
|
|
||||||
@@ -125,7 +125,8 @@ void loop(){
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Fstart(){
|
void Fstart(){
|
||||||
|
//Fonction start joue une seul fois
|
||||||
|
// Devellopement de l'autotune en cours a intergrer au demarrage du moteur ainsi que le homing
|
||||||
if (StartRoutine == 0)
|
if (StartRoutine == 0)
|
||||||
{
|
{
|
||||||
digitalWrite ( MotorIN1 , LOW );
|
digitalWrite ( MotorIN1 , LOW );
|
||||||
|
|||||||
Reference in New Issue
Block a user