Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of TestAnalogInPins by
main.cpp
- Committer:
- skanderian
- Date:
- 2015-07-23
- Revision:
- 1:610dc7763656
- Parent:
- 0:ce79b04fc2ca
- Child:
- 2:f2583b56777e
File content as of revision 1:610dc7763656:
#include "mbed.h"
//#include "rtos.h"
//Declare hardware inputs
Timer timer;
AnalogIn potSet(A0);
AnalogIn pressureDAQZO(A1);
PwmOut pumpStep(D8);
DigitalOut dir(D9);
DigitalOut visDir(LED2);
PwmOut r(LED_RED);
PwmOut g(LED_GREEN);
PwmOut b(LED_BLUE);
//Declare globals
char bufferIn[10];
char bufferOut[8];
char hexCCIn[2];
char hexDDDDIn[4];
int decCCIn;
int decDDDDIn;
unsigned long lastTime; //start it at 0, so at least one run of computePressureCommand is guaranteed
float outputCmd, iTerm;
float kp=2/9.9;
float ki=0;
float setPoint=0.000f;
int sampleTime = 250; //1/4 sec
float outMin=-1.0;
float outMax=1.0;
float smallChange=0.01*9.9/5;
float oldOutputCmd; //dont need
bool runPump=false;
//------------------------------------
// Hyperterminal configuration 9600 bauds, 8N
//------------------------------------
Serial pc(USBTX, USBRX);
void ledConfirmReceive()
{
r=1;
g=1;
b=0.9;
}
void ledConfirmSent()
{
r=1;
g=0.9;
b=1;
}
int hexToDec(char hex[])
{
int decValue = strtol(hex, NULL, 16);
return decValue;
}
int hexToDecSub(char hex[], int st, int ed)
{
int n=ed-st+1;
char hexSubset[n];
for(int i=st; i<ed+1; i++) {
hexSubset[i-st]=hex[i];
}
int decValue = strtol(hexSubset, NULL, 16);
return decValue;
}
void computePressureCommand()
{
unsigned int now = timer.read_ms();
int nSamplesToAverage=10;
int timeChange = (now - lastTime);
if(timeChange>=sampleTime) {
float avePressureDAQZO=0;
for (int i=0; i<nSamplesToAverage; i++) {
avePressureDAQZO = avePressureDAQZO+pressureDAQZO;
}
avePressureDAQZO=avePressureDAQZO/(double)(nSamplesToAverage);
//setPoint = potSet;
float inputPressure=avePressureDAQZO*9.9;
float error = setPoint - inputPressure;
iTerm+= (ki * error);
if(iTerm > outMax) iTerm= outMax;
else if(iTerm < outMin) iTerm= outMin;
outputCmd = kp * error + iTerm;
if(outputCmd > outMax) outputCmd = outMax;
else if(outputCmd < outMin) outputCmd = outMin;
visDir=0;
if (outputCmd < 0) {
dir=1;
visDir=1;
outputCmd = outputCmd * -1.0;
} else dir=0;
if (abs(error) > smallChange) {
pumpStep.write(0.50f); // turn back on
pumpStep.period_us((1.0/outputCmd)*200);
} else pumpStep.write(0.0f); //shut off steps for too small change
char signSetPoint='+';
if(setPoint<0) {
signSetPoint='-';
}
char signinputVoltage='+';
if(setPoint<0) {
signinputVoltage='-';
}
pc.printf("%s%c%2.3f%s%c%2.3f%s","#SP",signSetPoint,abs(setPoint),"MP",signinputVoltage,abs(inputPressure),"SS^");
//printf("%f, %f, %f, %f, %f, %f \n\r ",setPoint, inputVoltage, outputCmd, kp*error, iTerm, (-0.5-dir.read()));
ledConfirmSent();
oldOutputCmd=outputCmd; //don't need
lastTime = now; /*Remember time for next time*/
}//end if timeChange
}//end computePressureVoltage
void runWhenNewSerialIn()
{
bufferOut[0]=bufferIn[0];
for(int i=0; i<7; i++) { // duplicate bytes 0 to 9 for now;
bufferOut[i]=bufferIn[i];
}
bufferOut[7]='^';//IMPORTANT!
//get hexCCIn
for(int i=0; i<2; i++) {
hexCCIn[i]=bufferIn[i+1];
}
//pc.printf("hexCCIn= is %s\r\n", hexCCIn);
for(int i=0; i<4; i++) {
hexDDDDIn[i]=bufferIn[i+3];
}
//pc.printf("hello test= is %s\r\n", test);
//int value = strtol(hexDDDDIn, NULL, 16);
decCCIn = hexToDec(hexCCIn);
decDDDDIn = hexToDec(hexDDDDIn);
//int decDDDDIn = hexToDec("0000");
//Make sure incoming checksum pans out:
//Do action based on CC
//pc.printf("hexCCIn= is %c%c\r\n", hexCCIn[0],hexCCIn[1]);
//pc.printf("hexDDDDIn= is %c%c%c%c\r\n", hexDDDDIn[0],hexDDDDIn[1],hexDDDDIn[2],hexDDDDIn[3]);
//pc.printf("decCCIn= is %d\r\n", decCCIn);
//pc.printf("decDDDDIn= is %d\r\n", decDDDDIn);
if (decCCIn==1) { //control built in LED
//PwmOut led(LED_RED); //set to red just for now, change later if nec.
//Get desired intensity
double ledIntensity=(double)(hexToDecSub(bufferIn, 4, 5));
if (bufferIn[3]=='0') { //red
//PwmOut r(LED_RED);
r=(255-ledIntensity)/255;
} else if(bufferIn[3]=='1') {
//PwmOut g(LED_GREEN);
g=(255-ledIntensity)/255;
} else if (bufferIn[3]=='2') {
//PwmOut led(LED_BLUE);
b=(255-ledIntensity)/255;
}
pc.printf("%s", bufferOut);
} else if(decCCIn==27) { //CC=1b, Enable pump
pumpStep.write(0.50f); // set fixed duty cycle to 50% (hold position?)
ledConfirmSent();
runPump=true;
//pc.printf("%s\n", bufferOut); //No output SP & MP instead via computePressureCommand
ledConfirmSent();
} else if(decCCIn==28) { //CC=1c, Define new pressure setPoint
setPoint=((double)(decDDDDIn))/100; //converted fron hex above
} else if (decCCIn==29) {//CC=1d, Deactivate pump
pumpStep.write(0.0f);
ledConfirmSent();
runPump=false;
pc.printf("%s", bufferOut);
ledConfirmSent();
}
}
int main()
{
r.period(0.001f);
g.period(0.001f);
b.period(0.001f);
r=1;
g=1;
b=1;
//pc.printf("\r\n"); //Important
timer.start();
while(1) {
scanf("%10s", bufferIn);
//Turn built in LED blue (at half intensity) to confirm command recieved
ledConfirmReceive();
runWhenNewSerialIn();
//Always keep running computePressureCommand if requred
if(runPump) {
computePressureCommand();
}
//pc.printf("NEW value of '%s' is %ld\n", hexDDDDIn, decDDDDIn);
//Turn built in LED green (at half intensity) to confirm reponse sent back
}
}
