PDA

Archiv verlassen und diese Seite im Standarddesign anzeigen : Raspi GPP C++ WiringPi: Rotationsencoder auslesen



HaWe
17.10.2015, 21:42
[erledigt]

Hallo,
ich bin jetzt erst 1 knappe Woche auf dem Raspi unterwegs (vorher Arduino) und bin daher mit der Verkabelung und der Programmierung noch unsicher.
Ich habe aktuell Arduino-Code für Rotationsencoder auf Raspi portiert, nun bin ich unsicher wegen Pullups oder Vorwiderständen, und ich möchte ungern einen Kurzschluss verursachen.


Meine Fragen:
Meine encoderpins habe ich im Prinzip wie INPUT_PULLUP bei Arduino initialisiert - sind jetzt die Pullups korrekt dazugeschaltet?


pinMode(encoderPin1, INPUT); // attach pullup resistor !
pinMode(encoderPin2, INPUT); // attach pullup resistor !

pullUpDnControl(encoderPin1, PUD_UP); // Enable pull-up resistor on encoder pin
pullUpDnControl(encoderPin2, PUD_UP); // Enable pull-up resistor on encoder pin

Muss ich beim Raspi zusätzlich Vorwiderstände dazuschalten? (Beim Arduino ist das nicht nötig)

Hier ist die vorangegangene Arduino-Schaltung:
http://www.multiwingspan.co.uk/arduino.php?page=rotary2

http://www.multiwingspan.co.uk/images/arduino/rot_bb.png


letzte Frage (vorläufig):
Die Pinchange-Interrupts sind irgendwie nirgends vernünftig dokumentiert und erklärt - habe ich das so richtig gemacht ?



// ISR init
if (wiringPiISR (encoderPin1, INT_EDGE_FALLING, &updateEncoder) < 0) {
fprintf (stderr, "Unable to setup ISR: %s\n", strerror (errno)) ;
return 1 ;
}
if (wiringPiISR (encoderPin2, INT_EDGE_FALLING, &updateEncoder) < 0) {
fprintf (stderr, "Unable to setup ISR: %s\n", strerror (errno)) ;
return 1 ;
}



Perfekt wäre es natürlich, wenn jemand bereits getesteten Code kennt bzw. benutzt, um hier nicht Anfängerfehler zu machen die schwer zu entdecken und auszumerzen sind.





/*

************************************************** *********************
* This file is part of wiringPi:
* https://projects.drogon.net/raspberry-pi/wiringpi/
*
*
* You should have received a copy of the GNU Lesser General Public License
* along with wiringPi. If not, see <http://www.gnu.org/licenses/>.
************************************************** *********************
*/

#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <wiringPi.h> // -I/usr/local/include -L/usr/local/lib -lwiringPi


int encoderPin1 = 2;
int encoderPin2 = 3;

volatile int lastEncoded = 0;
volatile long encoderValue = 0;

volatile long lastencoderValue = 0;

int lastMSB = 0;
int lastLSB = 0;


/*
* ISR updateEncoder:
************************************************** *******************************
*/

void updateEncoder (void)
{
int MSB = digitalRead(encoderPin1); //MSB = most significant bit
int LSB = digitalRead(encoderPin2); //LSB = least significant bit

int encoded = (MSB << 1) |LSB; //converting the 2 pin value to single number
int sum = (lastEncoded << 2) | encoded; //adding it to the previous encoded value

if(sum == 0b1101 || sum == 0b0100 || sum == 0b0010 || sum == 0b1011) encoderValue ++;
if(sum == 0b1110 || sum == 0b0111 || sum == 0b0001 || sum == 0b1000) encoderValue --;

lastEncoded = encoded; //store this value for next time
}


/*
************************************************** *******************************
* main
************************************************** *******************************
*/

int main (void)
{

// setup
if (wiringPiSetup () < 0) {
fprintf (stderr, "Unable to setup wiringPi: %s\n", strerror (errno)) ;
return 1 ;
}

pinMode(encoderPin1, INPUT); // attach pullup resistor !
pinMode(encoderPin2, INPUT); // attach pullup resistor !

pullUpDnControl(encoderPin1, PUD_UP); // Enable pull-up resistor on encoder pin
pullUpDnControl(encoderPin2, PUD_UP); // Enable pull-up resistor on encoder pin


// ISR init
if (wiringPiISR (encoderPin1, INT_EDGE_FALLING, &updateEncoder) < 0) {
fprintf (stderr, "Unable to setup ISR: %s\n", strerror (errno)) ;
return 1 ;
}
if (wiringPiISR (encoderPin2, INT_EDGE_FALLING, &updateEncoder) < 0) {
fprintf (stderr, "Unable to setup ISR: %s\n", strerror (errno)) ;
return 1 ;
}



// loop

for (;;) {
printf ("%3d: %6ld\n", 1, encoderValue);
delay(100);
}

return 0 ;
}

HaWe
18.10.2015, 09:29
hat sich erledigt, es war im Prinzip alles OK!