#include <SPI.h>
#include <Wire.h>
#include <LIDARLite.h>
#include <SparkFunAutoDriver.h>
#include <SparkFundSPINConstants.h>
// Set up board
AutoDriver boardA(10, 6);
bool dir = FWD;
// Initialize angles
double Angle1;
double Angle2;
double dAngle;
double Angle = 0.0;
// Create a new LIDARLite instance
LIDARLite myLidarLite;
void setup() {
Serial.begin(9600);
boardA.configSyncPin(BUSY_PIN, 0);// BUSY pin low during operations;
// second paramter ignored.
boardA.configStepMode(STEP_FS); // 128 microsteps per step
boardA.setMaxSpeed(10000); // 10000 steps/s max
boardA.setFullSpeed(10000); // microstep below 10000 steps/s
boardA.setAcc(10000); // accelerate at 10000 steps/s/s
boardA.setDec(10000);
boardA.setSlewRate(SR_180V_us); // Upping the edge speed increases torque.
boardA.setOCThreshold(OC_750mA); // OC threshold 750mA
boardA.setPWMFreq(PWM_DIV_2, PWM_MUL_2); // 31.25kHz PWM freq
boardA.setOCShutdown(OC_SD_DISABLE); // don't shutdown on OC
boardA.setVoltageComp(VS_COMP_DISABLE); // don't compensate for motor V
boardA.setSwitchMode(SW_USER); // Switch is not hard stop
boardA.setOscMode(INT_16MHZ_OSCOUT_16MHZ); // for boardA, we want 16MHz
// internal osc, 16MHz out.
boardA.setAccKVAL(25); // We'll tinker with these later, if needed.
boardA.setDecKVAL(25);
boardA.setRunKVAL(25);
boardA.setHoldKVAL(32); // This controls the holding current; keep it low.
boardA.setLoSpdOpt(1);
myLidarLite.begin(1,true);
// Read first angle
Angle1 = analogRead(0);
}
void loop() {
// Spin motor
boardA.run(dir,100);
// Get the change in angle
Angle2 = analogRead(0);
dAngle = Angle2-Angle1;
if (dAngle<-20.0)
dAngle = dAngle + 1024.0;
if (dAngle>20.0)
dAngle = 6.0;
// Modify the change in angle for the gear ratio between lidar and potentiometer
Angle = Angle + dAngle*1.447*6.28318530718/1024.0;
// Next we need to take 1 reading with preamp stabilization and reference pulse (these default to true)
// Print to serial with a space seperation and newline at end
Serial.print(myLidarLite.distance()/100.0);
Serial.print(' ');
Serial.println(Angle);
Angle1 = Angle2;
// Next lets take 99 reading without preamp stabilization and reference pulse (these read about 0.5-0.75ms faster than with)
for(int i = 1; i < 99; i++){
boardA.run(dir,100);
Angle2 = analogRead(0);
dAngle = Angle2-Angle1;
if (dAngle<-20.0)
dAngle = dAngle + 1024.0;
if (dAngle>20.0)
dAngle = 6.0;
Angle = Angle + dAngle*1.447*6.28318530718/1024.0;
Angle1 = Angle2;
Serial.print(myLidarLite.distance(false,false)/100.0);
Serial.print(' ');
Serial.println(Angle);
}
}