Skip to content
Snippets Groups Projects
Commit 93ddf71d authored by cpryan's avatar cpryan
Browse files

Replace test_stand_firmware.ino, switched to serial polling to lag issues,...

Replace test_stand_firmware.ino, switched to serial polling to lag issues, testing of math effectiveness could be conducted
parent d760a9a6
No related branches found
No related tags found
3 merge requests!104adding cflib to this branch,!101adding baremetal programs,!92Replace test_stand_firmware.ino, switched to serial polling to lag issues,...
#include <movingAvg.h> #include <movingAvg.h>
unsigned long test_time = 0;
const int DATA_PIN = A7; const int DATA_PIN = A7;
const int BUTTON_PIN = 2; const int BUTTON_PIN = 2;
int buttonState = 0; boolean isStreaming = false;
const boolean NO_HANDSHAKE = false; int f = 200;
const int SEND_CODE = 65; int w = 0;
int incomingByte = 0; unsigned long time_step = 50000;
int val = 0; const double TEN_BIT_SCALAR = 0.3516; // 360/1024
const double TEN_BIT_SCALAR = 0.3516; // 360/1024 int homePosition;
int homePosition; double displayPosition;
// Rate variables // Rate variables
long lastTime = 0; unsigned long lastTime = 0;
long currTime = 0; unsigned long currTime = 0;
long lastReading = 0; int lastReading = 0;
long currReading = 0; int currReading = 0;
int prevRate = 0; double deltaTime = 1;
long deltaTime = 0; double deltaReading = 0;
long deltaReading = 0; unsigned long buttonTimer = 0;
int i = 0;
double rate;
double rate;
// Long Press Variables
long buttonTimer = 0; // Long Press Variables
const long LONG_PRESS_MILLIS = 250; const long LONG_PRESS = 250;
boolean buttonActive = false; boolean isInPositionMode = true;
boolean longPressActive = false; movingAvg myRate(20);
boolean isInPositionMode = true;
movingAvg mySensor(10); void rateMode(){
deltaTime = currTime - lastTime; // Find change in time
void serialFlush() { deltaReading = currReading - lastReading; // Find change in position
while (Serial.available() > 0) { deltaReading += (deltaReading < -512) ? (1024) : (0);
char t = Serial.read(); deltaReading -= (deltaReading > 512) ? (1024) : (0);
}
} // Rate is change in position divided by change in time (multiply by 10^6 to convert microseconds to seconds)
rate = TEN_BIT_SCALAR * deltaReading * 1000000 / deltaTime;
void setup() {
// put your setup code here, to run once: myRate.reading(rate);
Serial.begin(9600); rate = myRate.getAvg();
pinMode(BUTTON_PIN, INPUT);
pinMode(LED_BUILTIN, OUTPUT); if(isStreaming && !(isInPositionMode)){
homePosition = analogRead(DATA_PIN); Serial.println(rate);
homePosition += 512; }
lastTime = millis();
lastReading = analogRead(DATA_PIN); }
mySensor.begin(); void positionMode(){
} displayPosition = TEN_BIT_SCALAR * (double)(currReading);
displayPosition += (displayPosition < 0) ? (360) : (0);
void loop() { displayPosition -= 180;
currTime = millis(); if(isStreaming && isInPositionMode){
currReading = analogRead(DATA_PIN); Serial.println(displayPosition);
}
if (digitalRead(BUTTON_PIN) == HIGH) { }
if (buttonActive == false) { // Button was not pressed last cycle
buttonActive = true; void handleButton(){
buttonTimer = millis(); // If button was pressed
} if(digitalRead(BUTTON_PIN) == HIGH){
if (((millis() - buttonTimer) > LONG_PRESS_MILLIS) && (longPressActive == false)) { // Detected a long press, swap modes buttonTimer = millis();
longPressActive = true; while(digitalRead(BUTTON_PIN) == HIGH){
isInPositionMode = !isInPositionMode; }
} if(millis() - buttonTimer > LONG_PRESS){ // Detected a long press, swap modes
} isInPositionMode = !isInPositionMode;
else { }
if (buttonActive == true) { // Button was being pressed last cycle else{ // Short press occured, grab current position to reset home position regardless of mode
if (longPressActive == true) { // Button was being long pressed last cycle, user has released the long press homePosition = analogRead(DATA_PIN);
longPressActive = false; homePosition += 512;
} }
else { // Short press occured, grab current position regardless of mode // LED on if we're in position mode, off if we're in rate mode
homePosition = analogRead(DATA_PIN); digitalWrite(LED_BUILTIN, isInPositionMode);
homePosition += 512; }
} }
buttonActive = false;
} void handleSerial(){
} if(Serial.available() > 0){
char readChar = Serial.read();
// LED on if we're in position mode, off if we're in rate mode switch(readChar){
digitalWrite(LED_BUILTIN, isInPositionMode); case 'h': // Set current position to be zero
homePosition = analogRead(DATA_PIN);
if (isInPositionMode == true) { homePosition += 512;
double absoluteDegrees = TEN_BIT_SCALAR * (double) currReading; break;
double relativeDegrees = TEN_BIT_SCALAR * (double) homePosition; case 'w': // Change wait time between cycles (in microseconds)
double displayPosition = absoluteDegrees - relativeDegrees; w = Serial.readStringUntil('!').toInt();
displayPosition += (displayPosition < 0) ? (360) : (0); if(w > 0 && w < 1000000){
Serial.println((displayPosition-180)); time_step = w;
delay(100); }
} break;
else { // We're in rate mode case 't': // Toggle mode
isInPositionMode = !isInPositionMode;
deltaTime = currTime - lastTime; // LED on if we're in position mode, off if we're in rate mode
deltaReading = currReading - lastReading; digitalWrite(LED_BUILTIN, isInPositionMode);
rate = 1000 * ((TEN_BIT_SCALAR * deltaReading) / (deltaTime)); break;
case 'p': // Set to position mode
// Thanks josh for the fix isInPositionMode = true;
if(abs(rate - prevRate) > 360) { // LED on if we're in position mode, off if we're in rate mode
digitalWrite(LED_BUILTIN, isInPositionMode);
}else{ break;
mySensor.reading(rate); case 'r': // Set to rate mode
Serial.println(mySensor.getAvg()*-1); isInPositionMode = false;
delay(50); // LED on if we're in position mode, off if we're in rate mode
} digitalWrite(LED_BUILTIN, isInPositionMode);
prevRate = rate; break;
} case 'P': // Print position
// Reset vars for next cycle Serial.println(displayPosition);
lastTime = currTime; break;
lastReading = currReading; case 'R': // Print rate
} Serial.println(rate);
break;
case 'A': // Print value based on mode
if(isInPositionMode){
Serial.println(displayPosition);
}
else{
Serial.println(rate);
}
break;
case 's': // Enable Streaming
isStreaming = true;
break;
case 'd': // Disable Streaming
isStreaming = false;
break;
case 'x': // Print raw value
Serial.println(analogRead(DATA_PIN));
break;
}
}
}
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(BUTTON_PIN, INPUT);
pinMode(DATA_PIN, INPUT);
pinMode(LED_BUILTIN, OUTPUT);
homePosition = analogRead(DATA_PIN); // Capture home position
homePosition += 512; // Offset home position to middle value
lastTime = micros();
lastReading = analogRead(DATA_PIN);
// LED on if we're in position mode, off if we're in rate mode
digitalWrite(LED_BUILTIN, isInPositionMode);
myRate.begin();
}
void loop() {
currTime = micros();
currReading = (analogRead(DATA_PIN) - homePosition + 1024) % 1024;
// Switch mode after long button press, zero position after short button press
handleButton();
// Find the position
positionMode();
// Find the rate
rateMode();
// Handle commands from the PC
handleSerial();
// Reset vars for next cycle
lastTime = currTime;
lastReading = currReading;
delayMicroseconds(time_step);
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment