Hello all!
Maybe someone could lead me in the right direction but below is my current program. It turns a stepper motor that is attached to a test device. It will float to a certain torque target and then read and write the pressure and torque. Then it will return back to its original position and do this all over again.
My question is, instead of floating to a certain torque target, could I just float to a certain position? Instead of "float torqueTarget=40" could I just use "float position=800" ? I know my position of 800 gets me to my 45 degree turn and that's where I wish to stop.
// LCD Setup
#include <Wire.h>
#include <Adafruit_MCP23017.h>
#include <Adafruit_RGBLCDShield.h>
Adafruit_RGBLCDShield lcd = Adafruit_RGBLCDShield();
//Motor Controller Setup
#include <AccelStepper.h>
AccelStepper stepper(AccelStepper::DRIVER, 5, 3);
//SD Card
#include <SD.h>
const int chipSelect = 10;
File dataFile;
String dataString;
//RTC Setup
#include "RTClib.h"
RTC_DS1307 RTC;
//Pressure Transducer Setup
int pressurePin = A0;
float pressure;
float diffPressure;
float pressureVoltage;
//Solenoid Setup
const int solenoid0 = 8;
const int solenoid1 = 9;
/*int solenoidPin = 7;*/
//Strain Gauge Setup
int strainPin = A1;
float intitalStrainVoltage;
float torque = 0.0;
float aftervolt;
// float torqueAvg = 40.0;
// float torqueTarget = 70.0;
float torqueTarget = 40;
int position = 800;
int new_position;
int bump_step;
//int cycles = 2000;
int count = 1;
unsigned int ADCValue;
float Vcc;
//Reads a reference for analog signals
long readVcc() {
long result;
// Read 1.1V reference against AVcc
ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
delay(2); // Wait for Vref to settle
ADCSRA |= _BV(ADSC); // Convert
while (bit_is_set(ADCSRA,ADSC));
result = ADCL;
result |= ADCH<<8;
result = 1154158L / result; // Back-calculate AVcc in mV
return result;
}
void timestamp() {
DateTime now = RTC.now();
dataString += String(now.hour());
dataString += String(':');
dataString += String(now.minute());
dataString += String(':');
dataString += String(now.second());
dataString += ",";
}
//Checks the current pressure
void readPressure() {
pressure = 0;
for (int i=0; i <= 99; i++) {
Vcc = readVcc()/1000.0;
ADCValue = analogRead(pressurePin);
pressureVoltage = (ADCValue / 1023.0) * Vcc;
pressure += abs(7500 / 4 * (pressureVoltage - 1));
delay(10);
}
pressure /= 100;
/* Serial.print("Pressure:");
Serial.println(pressure); */
return;
}
void initialStrain() {
intitalStrainVoltage = 0.0;
for (int i=0; i <= 5; i++) {
Vcc = readVcc()/1000.0;
ADCValue = analogRead(strainPin);
intitalStrainVoltage += ADCValue * Vcc / 1023.0;
delay(10);
}
intitalStrainVoltage /= 6;
}
float measureTorque() {
aftervolt = 0.0;
Vcc = readVcc()/1000.0;
ADCValue = analogRead(strainPin);
aftervolt = ADCValue * Vcc / 1023.0;
torque = (aftervolt - intitalStrainVoltage) *147.5;
return torque;
}
void setup() {
Serial.begin (9600);
lcd.begin(16, 2);
lcd.setBacklight(0x2);
RTC.begin();
if (! RTC.isrunning()) {
/* Serial.println("RTC is NOT running!"); */
// following line sets the RTC to the date & time this sketch was compiled
RTC.adjust(DateTime(__DATE__, __TIME__));
}
pinMode(solenoid0, OUTPUT);
pinMode(solenoid1, OUTPUT);
pinMode(strainPin, INPUT);
pinMode(7, OUTPUT);
pinMode(10, INPUT);
pinMode(12, INPUT);
/* Serial.print("Initializing SD card..."); */
pinMode(SS, OUTPUT);
// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
/* Serial.println("Card failed, or not present"); */
// don't do anything more:
while (1) ;
}
/* Serial.println("card initialized."); */
// Open up the file we're going to log to!
dataFile = SD.open("datalog.csv", FILE_WRITE);
if (! dataFile) {
/* Serial.println("error opening datalog.txt"); */
// Wait forever since we cant write data
while (1) ;
}
// stepper.setMaxSpeed(400);
stepper.setAcceleration(5000);
}
void loop() {
lcd.clear();
while(1) {
lcd.setCursor(0, 0);
lcd.print("Endurance Test");
/* lcd.setCursor(0, 1);
lcd.print(cycles);*/
uint8_t buttons = lcd.readButtons();
if (buttons & BUTTON_SELECT) {
break;
}
}
lcd.clear();
initialStrain();
while(1) {
lcd.setBacklight(0x2);
dataString = "";
lcd.setCursor(0, 0);
lcd.print(count);
dataString += String(count);
dataString += ",";
digitalWrite(7, LOW);
timestamp();
//Close Valve
lcd.setCursor(0, 1);
lcd.print("Close");
// initialStrain();
measureTorque();
lcd.setCursor(11, 0);
lcd.print(" ");
lcd.setCursor(11, 0);
lcd.print(torque,1);
stepper.setMaxSpeed(900);
stepper.runToNewPosition(-position);
delay(500);
measureTorque();
//lcd.setCursor(11, 0);
//lcd.print(stepper.currentPosition());
// we are shooting for between 114 - 126
while (torque < torqueTarget) {
if (torqueTarget - torque > 0) { bump_step = -0.25; }
if (torqueTarget - torque > 5) { bump_step = -1; }
if (torqueTarget - torque > 10) { bump_step = -10; }
if (torqueTarget - torque > 20) { bump_step = -25; }
if (torqueTarget - torque > 50) { bump_step = -50; }
stepper.move(bump_step);
int position2 = stepper.currentPosition() + bump_step;
while (stepper.currentPosition() != position2)
stepper.run();
delay(500);
measureTorque();
lcd.setCursor(11, 0);
lcd.print(torque,1);
}
timestamp();
lcd.setCursor(0, 1);
lcd.print("PAUSE");
position = 800;
measureTorque();
lcd.setCursor(11, 0);
lcd.print(torque,1);
if (torque > 60) {
lcd.setCursor(0, 1);
lcd.print("TORQUE ERROR");
stepper.setMaxSpeed(300);
stepper.runToNewPosition(0);
while(1) {}
}
/*
stepper.move(-4000);
stepper.setMaxSpeed(20);
while (digitalRead(12) == LOW)
stepper.run();
stepper.stop();
position = abs(stepper.currentPosition());
*/
/*
for (int i = 0; measureTorque() < torqueTarget; i++)
{
bump_step = torqueTarget - torque;
stepper.move(bump_step);
int position2 = stepper.currentPosition() + bump_step;
while (stepper.currentPosition() != position2)
stepper.run();
position += bump_step;
Serial.println("bumped");
Serial.print("Bump_steps: ");
Serial.println(bump_step);
if (bump_step = 0) { break; break;}
delay(500);
}
*/
// measureTorque();
// lcd.setCursor(11, 0);
// lcd.print(torque,1);
readPressure();
lcd.setCursor(5, 0);
lcd.print(pressure,0);
delay(3500);
//Vcc = readVcc()/1000.0;
//ADCValue = analogRead(rhePin);
//rheVoltage = ADCValue * Vcc / 1023.0;
//rheValue = 30 * rheVoltage / 5 - 15;
//lcd.setCursor(12, 1);
//lcd.print(rheValue,1);
/* Vcc = readVcc()/1000.0;
int ADCValue1 = analogRead(strainPin);
float aftervolt = ADCValue1 * Vcc / 1023.0;
torque = (aftervolt - intitalStrainVoltage) / 0.0088; */
// torqueAvg = (torqueAvg * count + torque) / (count + 1);
// torqueTarget = 200.0 + rheValue + (200.0 - torqueAvg);
// torqueTarget = 62.0 + rheValue + (62 - torque);
/*Serial.print("Torque Avg: ");+
Serial.println(torqueAvg);
Serial.print("Torque Target: ");
Serial.println(torqueTarget);*/
/* if (torque > 65.05 || torque < 58.86) {
digitalWrite(7, HIGH);
} */
/* Serial.print("Torque: ");
Serial.println(torque); */
dataString += String(torque,2);
dataString += ",";
dataString += String(pressure,2);
dataString += ",";
//Vent to Atmosphere
lcd.setCursor(0, 1);
lcd.print("Vent ");
diffPressure = pressure;
readPressure();
diffPressure -= pressure;
dataString += String(diffPressure,2);
dataString += ",";
timestamp();
digitalWrite(solenoid0, HIGH);
delay(5000);
digitalWrite(solenoid0, LOW);
digitalWrite(solenoid1, HIGH);
delay(300);
digitalWrite(solenoid1, LOW);
timestamp();
delay(5000);
//Read Pressure
readPressure();
lcd.setCursor(5, 0);
lcd.print(" ");
lcd.setCursor(5, 0);
lcd.print(pressure,0);
dataString += String(pressure/2);
dataString += ",";
//Open Valve
lcd.setCursor(0, 1);
lcd.print("Open ");
timestamp();
/*
for(int i = 0; i = 19; i++) {
stepper.move(-1);
int position2 = stepper.currentPosition() - 1;
while (stepper.currentPosition() != position2)
stepper.run();
delay (300);
}
*/
stepper.setMaxSpeed(300);
stepper.runToNewPosition(-position + 500);
stepper.setMaxSpeed(900);
stepper.runToNewPosition(0);
if (torque > torqueTarget + 5)
position= torque - torqueTarget;
lcd.setCursor(0, 1);
lcd.print("PAUSE");
//count increase here
/*if (digitalRead(10) == HIGH)
position -= 10;
if (digitalRead(12) == HIGH)
position += 10;
*/
timestamp();
if (dataString.substring(1) == "V" || dataString.substring(1) == "c") {
lcd.setCursor(0, 1);
lcd.print("Error");
}
delay(6000);
count++;
dataString += "0";
dataFile.println(dataString);
/* Serial.println(dataString); */
dataFile.flush();
}
}