<commit_before>
// Copyright (c) 2013 Nautilabs
// https://github.com/baptistelabat/robokite

// Using example in http://code.google.com/p/arduino-pinchangeint/wiki/Usage

#include <PinChangeInt.h>

//Parts of code taken from http://playground.arduino.cc/Main/RotaryEncoders J.Carter(of Earth)

// 'threshold' is the De-bounce Adjustment factor for the Rotary Encoder. 
//
// The threshold value I'm using limits it to 100 half pulses a second
//
// My encoder has 12 pulses per 360deg rotation and the specs say
// it is rated at a maximum of 100rpm.
//
// This threshold will permit my encoder to reach 250rpm so if it was connected
// to a motor instead of a manually operated knob I
// might possibly need to adjust it to 25000. However, this threshold
// value is working perfectly for my situation
//
volatile unsigned long threshold = 1000;

// 'rotaryHalfSteps' is the counter of half-steps. The actual
// number of steps will be equal to rotaryHalfSteps / 2
//
volatile long rotaryHalfSteps = 0;

// Working variables for the interrupt routines
//
volatile unsigned long int0time = 0;
volatile unsigned long int1time = 0;
volatile uint8_t int0signal = 0;
volatile uint8_t int1signal = 0;
volatile uint8_t int0history = 0;
volatile uint8_t int1history = 0;

uint8_t latest_interrupted_pin;
uint8_t interrupt_count[20]={0}; // 20 possible arduino pins

int pinA = 2;
int pinB = 3;
int pinReset = 4;

// These constants won't change.  They're used to give names
// to the pins used:
const int analogInPin = 0;  // Analog input pin that the potentiometer is attached to
const int analogOutPin = 9; // Analog output pin that the LED is attached to

int sensorValue = 0;        // value read from the pot
int outputValue = 0;        // value output to the PWM (analog out)


void int0()
	{
	if ( micros() - int0time < threshold )
		return;
	int0history = int0signal;
	int0signal = bitRead(PIND,pinA);
	if ( int0history==int0signal )
		return;
	int0time = micros();
	if ( int0signal == int1signal )
		rotaryHalfSteps++;
	else
		rotaryHalfSteps--;
	}

void int1()
	{
	if ( micros() - int1time < threshold )
		return;
	int1history = int1signal;
	int1signal = bitRead(PIND,pinB);
	if ( int1history==int1signal )
		return;
	int1time = micros();
	}
void reset()
{
 rotaryHalfSteps = 0;
 }

void setup() {
  pinMode(pinA, INPUT);
  digitalWrite(pinA, HIGH);
  attachInterrupt(0, int0, CHANGE);
  //PCintPort::attachInterrupt(pinA, &int0, CHANGE);
  pinMode(pinB, INPUT);
  digitalWrite(pinB, HIGH);
  attachInterrupt(1, int1, CHANGE);
  //PCintPort::attachInterrupt(pinB, &int1, CHANGE);
  
  // Pin used to get absolute position
  pinMode(pinReset, INPUT);
  digitalWrite(pinReset, HIGH);
  PCintPort::attachInterrupt(pinReset, &reset, CHANGE);
  Serial.begin(115200);
  Serial.println("---------------------------------------");
}

uint8_t i;

void loop() {
  long actualRotaryTicks = (rotaryHalfSteps / 2);
    // read the analog in value:
  Serial.print(rotaryHalfSteps*8+512); 
  Serial.print(",");  
  //Serial.print("\t output = ");      
  //Serial.println(outputValue);
  delay(2);
    
  for (int thisPin = 1; thisPin < 6; thisPin++) {
  sensorValue = analogRead(thisPin);            
  
  // print the results to the serial monitor:
  //Serial.print("sensor = " );                       
  Serial.print(sensorValue); 
  Serial.print(",");  
  //Serial.print("\t output = ");      
  //Serial.println(outputValue);
  delay(2);   
  }
   Serial.println("");
  // wait 2 milliseconds before the next loop
  // for the analog-to-digital converter to settle
  // after the last reading:
  delay(2);  
}









<commit_msg>Use SI units, add correction of position based on potentiometer reading<commit_after>
// Copyright (c) 2013 Nautilabs
// https://github.com/baptistelabat/robokite

// Using example in http://code.google.com/p/arduino-pinchangeint/wiki/Usage

#include <PinChangeInt.h>

//Parts of code taken from http://playground.arduino.cc/Main/RotaryEncoders J.Carter(of Earth)

// 'threshold' is the De-bounce Adjustment factor for the Rotary Encoder. halfSteps
volatile unsigned long threshold = 1000;

// 'halfStepshalfSteps' is the counter of half-steps. The actual
// number of steps will be equal to halfSteps / 2
//
volatile long halfSteps = 0;
long halfStepsCorrection = 0;

// Working variables for the interrupt routines
//
volatile unsigned long int0time = 0;
volatile unsigned long int1time = 0;
volatile uint8_t int0signal = 0;
volatile uint8_t int1signal = 0;
volatile uint8_t int0history = 0;
volatile uint8_t int1history = 0;

uint8_t latest_interrupted_pin;
uint8_t interrupt_count[20]={0}; // 20 possible arduino pins

int pinA = 2;
int pinB = 3;
int pinReset = 4;

boolean isAbsoluteReference = false;

// These constants won't change.  They're used to give names
// to the pins used:
const int analogInPin = 0;  // Analog input pin that the potentiometer is attached to
const int analogOutPin = 9; // Analog output pin that the LED is attached to

int sensorValue = 0;        // value read from the pot
int outputValue = 0;        // value output to the PWM (analog out)

double linearResolution = 0.005; //Resolution of the linear encoder (in meters)
double linearRange = 0.5; //Range of the sensor to normalize data (+/- linearRange/2)

double potentiometerRangeDeg= 300; 
double potentiometerMaxRange = potentiometerRangeDeg * 3.1414/180;
double potentiometerUsedRange = 3.1415;
double potentiometerDistance = 0.05;

void int0()
{
  if ( micros() - int0time < threshold )
    return;
  int0history = int0signal;
  int0signal = bitRead(PIND,pinA);
  if ( int0history==int0signal )
    return;
  int0time = micros();
  if ( int0signal == int1signal )
    halfSteps++;
  else
    halfSteps--;
}

void int1()
{
  if ( micros() - int1time < threshold )
    return;
  int1history = int1signal;
  int1signal = bitRead(PIND,pinB);
  if ( int1history==int1signal )
    return;
  int1time = micros();
}

void reset()
{
  halfStepsCorrection = halfSteps;
  isAbsoluteReference = true;
 }

void setup() {
  pinMode(pinA, INPUT);
  digitalWrite(pinA, HIGH);
  attachInterrupt(0, int0, CHANGE);
  //PCintPort::attachInterrupt(pinA, &int0, CHANGE);
  pinMode(pinB, INPUT);
  digitalWrite(pinB, HIGH);
  attachInterrupt(1, int1, CHANGE);
  //PCintPort::attachInterrupt(pinB, &int1, CHANGE);
  
  // Pin used to get absolute position
  pinMode(pinReset, INPUT);
  digitalWrite(pinReset, HIGH);
  PCintPort::attachInterrupt(pinReset, &reset, CHANGE);
  
  // Setup serial communication
  Serial.begin(115200);
  Serial.println("---------------------------------------");
}

void loop() {
  long steps = (halfSteps / 2);
  double absolute_position_m = (halfSteps-halfStepsCorrection)*linearResolution/2;
  
  // read the analog in value:
  double raw_angle = analogRead(1)/1024.0*potentiometerMaxRange;
  double neutralAngle = 0.8*potentiometerMaxRange;
  double angle = raw_angle - neutralAngle;
  double correction_m = angle*potentiometerDistance; 
    
  Serial.print((absolute_position_m/linearRange)*1024 + 512);
  Serial.print(",");
  Serial.print(correction_m/linearRange*1024 + 512);
  Serial.print(",");
  Serial.print((absolute_position_m + correction_m)/linearRange*1024 + 512);
  Serial.print(",");
  Serial.print(angle/potentiometerUsedRange*1024 + 512);
  Serial.print(",");

  //Serial.print("\t output = ");      
  //Serial.println(outputValue);
  delay(2);
    
  for (int thisPin = 0; thisPin < 2; thisPin++) {
  sensorValue = analogRead(thisPin);            
  
  // print the results to the serial monitor:
  //Serial.print("sensor = " );                       
  Serial.print(sensorValue); 
  Serial.print(",");  
  //Serial.print("\t output = ");      
  //Serial.println(outputValue);
  delay(2);   
  }
   Serial.println("");
  // wait 2 milliseconds before the next loop
  // for the analog-to-digital converter to settle
  // after the last reading:
  delay(2);  
}










{'fraction_non_alphanumeric': 0.12515786814852237, 'fraction_numerical': 0.030689568072745643, 'mean_word_length': 3.3945615982241955, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1990174', 'n_tokens_mistral': 2769, 'n_tokens_neox': 2555, 'n_words': 760}