Code:
#include "KMotionDef.h"
#include "init_definitions.h" // this loads up the mapping of input/output channels and bits to names
#include "function_definitions.h" // load up all the definitions
// save file to disk for plotting
main()
{
//float current_time = Time_sec();
//printf("c:\\Temp\\P%f_I%f_D%fTime%f.txt\n", ch0->P, ch0->I, ch0->D, current_time);
float current_time = Time_sec();
float target_time;
float target_time2;
double current_position;
short int DACoutput;
float D_term = 1;
float P_term = 0.06;
FILE *f;
char s[256];
ch0->Enable=FALSE;
ch0->LimitSwitchOptions=0x103;
ch0->LimitSwitchNegBit=138;
ch0->LimitSwitchPosBit=139;
f=fopen("c:\\Temp\\logdata.txt","wt");
if (!f)
{
printf("Unable to open file\n");
return;
}
//// Run back and forth
printf("\n\nstart - move\n");
chan[0].Position=0.0; // set current position to Zero
while(current_position < 10000.0 && current_position > -10000.0) {
current_position = ch0->Position;
current_time = Time_sec();
DACoutput = DAC_Buffer[2]-2048;
fprintf(f, "%f,%f,%d\n", current_time, current_position, 100*DACoutput);
DAC(2,-65);
//if (current_position > 10000.0 || current_position < -10000.0) {
// ch0->Enable=FALSE;
// DAC(1,0);
// printf("too far\n");
// break;
//}
}
DAC(2,0);
printf("end - move\n");
current_time = Time_sec();
target_time = current_time + 1;
while(current_time < target_time) {
current_time = Time_sec();
DACoutput = DAC_Buffer[2]-2048;
fprintf(f, "%f,%f,%d\n", current_time, current_position, 100*DACoutput); // make a line to show when stopped
}
printf("start + move\n");
while(current_position > 0.0) {
current_position = ch0->Position;
current_time = Time_sec();
DACoutput = DAC_Buffer[2]-2048;
fprintf(f, "%f,%f,%d\n", current_time, current_position, 100*DACoutput);
DAC(2,75);
//if (current_position > 10000.0 || current_position < -10000.0) {
// ch0->Enable=FALSE;
// DAC(1,0);
// printf("too far\n");
// break;
//}
}
printf("end + move\n");
DAC(2,0);
//// End back and forth
/*
current_time = Time_sec();
target_time = current_time + 10;
printf("Start axis motion\n");
ch0->Enable=TRUE;
chan[0].Position=1500.0; // set current position to Zero
ch0->Dest=0.0;
// this only moves in one direction (-1500 to 0 seems to work. Is CW on end).
//DAC control moves in both. integer sign issue?
// what's the difference between this syntax and chan[0].Position?
// It was briefly working with this new syntax??
while(current_time < target_time) {
current_position = ch0->Position;
current_time = Time_sec();
DACoutput = DAC_Buffer[1]-2048;
fprintf(f, "%f,%f,%d\n", current_time, current_position, DACoutput);
//printf("%f,%f\n", current_time, current_position);
if (current_position > 7500.0 || current_position < -7500.0) {
ch0->Enable=FALSE;
DAC(1,0);
}
}*/
fclose(f);
printf("completed write\n");
ch0->Enable=FALSE;
DAC(1,0);
}
// Then in Octave: A = importdata('c:\Temp\logdata.txt', ',')
// plot(A)