78 #include <util/delay.h>
85 void TIMER2_COMPA_vect(
void)
87 asm volatile(
"jmp _AccelerationAlgorithm \n\t");
90 void TIMER1_COMPA_vect(
void)
93 static float curAngle, oldAngle = 0.0, loops = 1.0;
103 deltaAngle = (oldAngle - curAngle);
105 if(deltaAngle < -50.0)
109 else if(deltaAngle > 50.0)
114 if((deltaAngle >= 10.0) || (deltaAngle <= -10.0))
132 if(deltaAngle < -50.0)
137 else if(deltaAngle > 50.0)
162 Vout = analogRead(TEMP)*0.0048828125;
163 NTC = ((
R*5.0)/Vout)-
R;
165 T =
A +
B*NTC +
C*NTC*NTC*NTC;
178 float deltaAngle, angle;
180 TIMSK1 &= ~(1 << OCIE1A);
183 deltaAngle = this->
oldAngle - angle;
185 if(deltaAngle < -50.0)
190 else if(deltaAngle > 50.0)
202 TIMSK1 |= (1 << OCIE1A);
218 TIMSK1 = (1 << OCIE1A);
219 TCCR1B = (1 << WGM12) | (1 << CS10);
246 angle = (float)((((uint16_t)data[0]) << 8 )| (uint16_t)data[1])*0.087890625;
257 return (((uint16_t)data[0]) << 8 )| (uint16_t)data[1];
282 else if(data == 0x10)
287 else if(data == 0x20)
304 pinMode(DIR, OUTPUT);
305 pinMode(STEP, OUTPUT);
306 pinMode(ENA, OUTPUT);
318 pinMode(DIR, OUTPUT);
319 pinMode(STEP, OUTPUT);
320 pinMode(ENA, OUTPUT);
354 else if(vel > 32000.0)
397 if(dir != digitalRead(DIR))
407 this->
delay = 0xFFFF;
426 this->
accelSteps = (uint32_t)(((this->velocity*this->velocity) - (curVel*curVel))/(2.0*this->
acceleration));
439 digitalWrite(DIR, dir);
446 this->
delay = 0xFFFF;
465 this->
hold = holdMode;
473 if(dir != digitalRead(DIR))
496 this->
delay = 0xFFFF;
529 this->
accelSteps = (uint32_t)(((this->velocity*this->velocity) - (curVel*curVel))/(2.0*this->
acceleration));
568 digitalWrite(DIR, dir);
591 this->
delay = 0xFFFF;
606 this->
hold = holdMode;
622 else if (holdMode ==
HARD)
634 this->
hold = holdMode;
648 this->
delay = 0xFFFF;
665 else if (holdMode ==
HARD)
675 TCCR2B &= ~((1 << CS20) | (1 << CS21) | (1 << CS22) | (1 << WGM22));
676 TCCR2A &= ~((1 << WGM20) | (1 << WGM21));
677 TCCR2B |= (1 << CS21);
678 TCCR2A |= (1 << WGM21);
686 TIFR2 |= (1 << OCF2A);
687 TIMSK2 |= (1 << OCIE2A);
694 TIMSK2 &= ~(1 << OCF2A);
699 digitalWrite(ENA, LOW);
704 digitalWrite(ENA, HIGH);
740 while (!(TWCR & (1 << TWINT)));
746 bool i2cMaster::read(uint8_t slaveAddr, uint8_t regAddr, uint8_t numOfBytes, uint8_t *data)
748 uint8_t i, buff[numOfBytes];
750 TIMSK1 &= ~(1 << OCIE1A);
758 for(i = 0; i < (numOfBytes - 1); i++)
767 TIMSK1 |= (1 << OCIE1A);
772 bool i2cMaster::write(uint8_t slaveAddr, uint8_t regAddr, uint8_t numOfBytes, uint8_t *data)
776 TIMSK1 &= ~(1 << OCIE1A);
781 for(i = 0; i < numOfBytes; i++)
787 TIMSK1 |= (1 << OCIE1A);
796 this->
cmd((1 << TWINT) | (1 << TWEN) | (1 << TWEA));
801 this->
cmd((1 << TWINT) | (1 << TWEN));
812 this->
cmd((1<<TWINT) | (1<<TWSTA) | (1<<TWEN));
820 TWDR = (addr << 1) | RW;
821 this->
cmd((1 << TWINT) | (1 << TWEN));
836 return this->
start(addr, RW);
843 this->
cmd((1 << TWINT) | (1 << TWEN));
852 TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWSTO);
855 while (TWCR & (1 << TWSTO));
uStepper(void)
Constructor of uStepper class.
uStepperEncoder(void)
Constructor.
uint16_t getStrength(void)
Measure the strength of the magnet.
float getSpeed(void)
Measure the current speed of the motor.
bool write(uint8_t slaveAddr, uint8_t regAddr, uint8_t numOfBytes, uint8_t *data)
sets up I2C connection to device, writes a number of data bytes and closes the connection ...
bool getMotorState(void)
Get the current state of the motor.
Prototype of class for accessing all features of the uStepper in a single object. ...
void enableMotor(void)
Enables the stepper driver output stage.
float getMaxAcceleration(void)
Get the value of the maximum motor acceleration.
float getTemp(void)
Request a reading of current temperature.
bool getCurrentDirection(void)
Returns the direction the motor is currently configured to rotate.
float getAngleMoved(void)
Measure the angle moved from reference position.
void hardStop(bool holdMode)
Stop the motor without deceleration.
void setMaxAcceleration(float accel)
Set the maximum acceleration of the stepper motor.
void startTimer(void)
Starts timer for stepper algorithm.
void setMaxVelocity(float vel)
Sets the maximum rotational velocity of the motor.
void moveSteps(uint32_t steps, bool dir, bool holdMode)
Make the motor perform a predefined number of steps.
float getMaxVelocity(void)
Returns the maximum rotational velocity of the motor.
bool writeByte(uint8_t data)
Writes a byte to a device on the I2C bus.
i2cMaster(void)
Constructor.
void softStop(bool holdMode)
Stop the motor with deceleration.
uint8_t detectMagnet(void)
Detect if magnet is present and within range.
uint8_t getStatus(void)
Get current I2C status.
void cmd(uint8_t cmd)
Sends commands over the I2C bus.
bool restart(uint8_t addr, bool RW)
Restarts connection between arduino and I2C device.
bool readByte(bool ack, uint8_t *data)
Reads a byte from the I2C bus.
void stopTimer(void)
Stops the timer for the stepper algorithm.
uint8_t getAgc(void)
Read the current AGC value of the encoder chip.
bool start(uint8_t addr, bool RW)
sets up connection between arduino and I2C device.
void runContinous(bool dir)
Make the motor rotate continuously.
#define ENCODERSPEEDCONSTANT
Prototype of class for accessing the TWI (I2C) interface of the AVR (master mode only).
void setHome(void)
Define new reference(home) position.
float getAngle(void)
Measure the current shaft angle.
bool stop(void)
Closes the I2C connection.
void begin(void)
Setup TWI (I2C) interface.
Function prototypes and definitions for the uStepper library.
volatile float angleMoved
void setup(void)
Initializes the different parts of the uStepper object.
uStepperTemp(void)
Constructor.
bool read(uint8_t slaveAddr, uint8_t regAddr, uint8_t numOfBytes, uint8_t *data)
sets up I2C connection to device, reads a number of data bytes and closes the connection ...
void disableMotor(void)
Disables the stepper driver output stage.
int64_t getStepsSinceReset(void)
Get the number of steps applied since reset.
void setup(void)
Setup the encoder.
uint32_t initialDecelSteps