creating first draft for encoder class

pull/33/head
TimEvWw 2015-10-29 21:50:09 -01:00
parent aaab53ef3a
commit 66a8ce0746
3 changed files with 132 additions and 0 deletions

View File

@ -12,6 +12,7 @@
#include "CurrentState.h"
#include "ParameterList.h"
#include "StepperControlAxis.h"
#include "StepperControlEncoder.h"
#include "pins.h"
#include "Config.h"
#include <stdio.h>

View File

@ -0,0 +1,78 @@
#include "StepperControlEncoder.h"
StepperControlEncoder::StepperControlEncoder() {
//lastCalcLog = 0;
pinChannelA = 0;
pinChannelB = 0;
position = 0;
}
void StepperControlEncoder::test() {
Serial.print("R99 ");
Serial.print(position);
Serial.print("\n");
}
void StepperControlEncoder::loadPinNumbers(int channelA, int channelB) {
pinChannelA = channelA;
pinChannelB = channelB;
readChannels();
shiftChannels();
}
void StepperControlEncoder::setPosition(long newPosition) {
position = newPosition;
}
long StepperControlEncoder::currentPosition() {
return position;
}
/* Check the encoder channels for movement accoridng to thisspecification
________ ________
Channel A / \ / \
_____/ \________/ \________
________ ________
Channel B / \ / \
__________/ \________/ \____
__
Channel I / \
____________________/ \___________________
rotation ----------------------------------------------------->
*/
void StepperControlEncoder::readEncoder() {
// save the old values, read the new values
shiftChannels();
readChannels();
// and check for a position change
// no fancy code, just a few simple compares. sorry
if (prvValChannelA == true && curValChannelB == true && prvValChannelB == false && prvValChannelB == true ) {position++;}
if (prvValChannelA == true && curValChannelB == false && prvValChannelB == true && prvValChannelB == true ) {position++;}
if (prvValChannelA == false && curValChannelB == false && prvValChannelB == true && prvValChannelB == false) {position++;}
if (prvValChannelA == false && curValChannelB == true && prvValChannelB == false && prvValChannelB == false) {position++;}
if (prvValChannelA == false && curValChannelB == false && prvValChannelB == false && prvValChannelB == true ) {position--;}
if (prvValChannelA == false && curValChannelB == true && prvValChannelB == true && prvValChannelB == true ) {position--;}
if (prvValChannelA == true && curValChannelB == true && prvValChannelB == true && prvValChannelB == false) {position--;}
if (prvValChannelA == true && curValChannelB == false && prvValChannelB == false && prvValChannelB == false) {position--;}
}
void StepperControlEncoder::readChannels() {
curValChannelA = digitalRead(pinChannelA);
curValChannelB = digitalRead(pinChannelB);
}
void StepperControlEncoder::shiftChannels() {
prvValChannelA = curValChannelA;
prvValChannelB = curValChannelB;
}

View File

@ -0,0 +1,53 @@
/*
* StepperControlEncoder.h
*
* Created on: 29 okt 2015
* Author: Tim Evers
*/
#ifndef STEPPERCONTROLENCODER_H_
#define STEPPERCONTROLENCODER_H_
#include "Arduino.h"
//#include "CurrentState.h"
//#include "ParameterList.h"
#include "pins.h"
#include "Config.h"
#include <stdio.h>
#include <stdlib.h>
class StepperControlEncoder {
public:
StepperControlEncoder();
void loadPinNumbers(int channelA, int channelB);
void setPosition(long newPosition);
long currentPosition();
void readEncoder();
void readChannels();
void shiftChannels();
void test();
private:
// pin settings
int pinChannelA;
int pinChannelB;
// channels
bool prvValChannelA;
bool prvValChannelB;
bool curValChannelA;
bool curValChannelB;
// encoder
int position;
};
#endif /* STEPPERCONTROLENCODER_H_ */