void loop()
{
  input = encoderRead();

  if(new_kP != old_kP)
  {
      myPID.SetTunings(new_kP,0,0);
      old_kP = new_kP;
  }

  myPID.Compute();
  double motorInput;
  if (output < -1)
  {
    motorInput = (112.0/127.0)*output -15;
  }
  else if (output > 1)
  {
    motorInput = (112.0/127.0)*output +15;
  }
  else
  {
    motorInput = output;
  }

  motorInput = motorInput+127.0;

  mySerial.write((int)motorInput);
  delay(120);
}
void setup()
{
  Wire.begin(myAddress);
  Wire.onReceive(i2cReceive);
  Wire.onRequest(i2cRequest);

  pinMode(limit_switch_pin, INPUT); 

  mySerial.begin(9600); // Serial commuication to motor controller start.

  setpoint = 0.0;

  calibration(); // Running the calibration code on every start up

  input = encoderRead();

  myPID.SetMode(AUTOMATIC);
  myPID.SetOutputLimits(-127, 127);
  myPID.SetSampleTime(20);
}
Esempio n. 3
0
/*
 * get increment/decrement keys.
 * This may be extended for rotary encoders in future version.
 */
int8_t get_key_increment(void)
{
	#ifdef ENCODER
	#warning encoder gets key increment
	int8_t keys = encoderRead();
	// limit
	if (keys > 0)
		return 1;
	if (keys < 0)
		return -1;
	#else
	uint8_t keys = get_key_press((1 << KEY_MINUS) | (1 << KEY_PLUS));
	if(keys & (1 << KEY_PLUS))
		return 1;
	if(keys & (1 << KEY_MINUS))
		return -1;
	#endif
	
	return 0;
}
Esempio n. 4
0
void main() {
	timerInit();
	sei();
	pwmInit();
	int16_t i = 0;
	uint32_t lastDetentTimestamp;
	pwmSet(0);
	int8_t encoderPulses = 0;
	while(1) {
		encoderPulses += encoderRead();

		if( encoderPulses > 3 || encoderPulses < -3 ) {
			int8_t encoderStep = encoderPulses / 4;

			if(millis - lastDetentTimestamp < 20) {
				i+= encoderStep*10;
			} else if(millis - lastDetentTimestamp < 30) {
				i+= encoderStep*5;
			} else if(millis - lastDetentTimestamp < 40) {
				i+= encoderStep*2;
			} else {
				i+= encoderStep;
			}
			if(i > 187) {
				i = 187;
			} 
			if(i < 0) {
				i = 0;
			}
			targetPwm = (uint8_t) pgm_read_byte(&(pwm[i]));
			lastDetentTimestamp = millis;
			//_delay_ms(1);
			encoderPulses = 0;
		}
//		_delay_ms(5);
	}
}