Hello,

I am a engineering student and my group and I are working on a project that uses an ultrasonic robot to scan specimens and collect ultrasonic data. The robot has 5 degrees of freedom and can be controlled manually or with a program using LinuxCNC-HAL-STEP-MM. The ultrasonic transducer is at the end of the robot's arm. We have a few G code files that tell the robot to run certain paths. The robot is working just fine and the ultrasonic system also functions the way it should, we just need to piece everything together in order to achieve what we want.
Ultimately we want to give the robot a path to run, along the path the ultrasonic device will take data points, these data points will be stored in a file, from this file a 3D image can be created.
Our group consists of two mechanical majors and an electrical major so programming isn't our cup of tea.

As of right now our main problems are writing a code that moves the robot along a path, pauses and collects ultrasonic data (amplitude peaks in this case). The collection of ultrasound data is what eludes us. So far, our code that he HAL STEP reads and executes is:

G20 (set unit to english)
G90 (global)
F50 (set feedrate)
G01 (linear)
#10=2.5 (#10 x distance)
#11=2.5 (#11 y distance)
#12=0.01 (#12 x step distance)
#13=0.1 (#13 y step distance)
#14=0.0 (#14 is x position now)
#15=0.0 (#15 is y position now)
O1001 while [#14 LT #10] (loop on x)
O1002 while [#15 LT #11] (loop on y)
x#14 y#15 (move to the next point)
#15=[#15 + #13] (inc y)
G4 P1.0 (Dwell - no motion for P seconds. This is where the command to get data from the Ultrasound instrument should be)
(need to call M106 - not yet created m file - )
(M106 P#14 Q#15)
O1002 endwhile
#15=0.0 (reset y, inc x)
#14=[#14 + #12]
O1001 endwhile
x0 y0 (go home)
M30

Our group doesn't know how to write the command to get data from the ultrasound instrument. Any assistance solving this problem would be great.

The computer talks to the ultrasonic device using a RS232 cable and gather information using c++. Here is the code that is used to gather information.
/* compile this with

g++ rs232.c sonicwave.cpp gnuplot_i.hpp -o sonichack

or write a make file

execute with ./sonichack x y

*/

#include "gnuplot_i.hpp" //Gnuplot class handles POSIX-Pipe-communikation with Gnuplot
//tell the compiler we are using some imported C functions
extern "C"
{
int RS232_OpenComport(int cport_nr, int bdrate);
int RS232_CloseComport(int cport_nr);
int RS232_PollComport(int, unsigned char *, int);
int RS232_SendByte(int, unsigned char);
int RS232_SendBuf(int, unsigned char *, int);
}
unsigned int get_word();
unsigned int get_byte();
#define BUF_SIZE 10000
unsigned char sysbuf[BUF_SIZE];
unsigned char localbuf[BUF_SIZE];

int cport_nr=0; /* port */

#define NSB 224 // number of bytes in a scan


unsigned int get_byte()
{
int localidx=0;
int n=0;
do
{
n = RS232_PollComport(cport_nr, sysbuf, BUF_SIZE-1);
if(n>0)
{
for(unsigned int k=0;k<n;k++)
{
localbuf[localidx]=sysbuf[k];
localidx++;
}
}
usleep(1000);
}
while(localidx<0);
printf("%x ",localbuf[0]);
return localbuf[0];
}

unsigned int get_scan()
{
int localidx=0;
int n=0;
do
{
n = RS232_PollComport(cport_nr, sysbuf, BUF_SIZE-1);
if(n>0)
{
for(unsigned int k=0;k<n;k++)
{
localbuf[localidx]=sysbuf[k];
//printf("%d ",sysbuf[k]);
localidx++;
}
}
usleep(1000);
}
while(localidx<NSB); // this is the number of bytes returned from the instrument for each scan
return localidx;
}


int main (int argc, char *argv[])
{

// x and y values will be passed in from the code in EMC robot control package
//see if the x,y location was passed in

//argv[1] is the x value
//argv[2] is the y value
if(argc < 3)
{
printf("need x and y passed in\n");
exit(-1);
}



// setup and open the spectrum port
int bdrate=9600; /* baud */
int comport_open_success=false;

if(RS232_OpenComport(cport_nr, bdrate))
{
comport_open_success=false;
printf("Can not open comport\n");
exit(-1);
}
printf("opened port\n");
comport_open_success=true;
/*
// here is how we ask what the present gain setting is
RS232_SendByte(cport_nr, 'G'); //6b
RS232_SendByte(cport_nr, 'A'); //21
RS232_SendByte(cport_nr, 'N'); //21
RS232_SendByte(cport_nr, '?'); //21
RS232_SendByte(cport_nr, '\r');

//usleep(100000);

int n=RS232_PollComport(cport_nr, sysbuf, BUF_SIZE-1);
printf("got %d chars\n",n);
sysbuf[n]=0;
printf("%s\n",sysbuf);
*/

// here we run forever just for a demo
// in the real program we would run thru the below once and leave
//for(;

//{

printf("x %s ",argv[1]);
printf("y %s ",argv[2]);
// get the waveform
RS232_SendByte(cport_nr, 'U');
RS232_SendByte(cport_nr, 'P');
RS232_SendByte(cport_nr, 'W');
RS232_SendByte(cport_nr, '=');
RS232_SendByte(cport_nr, '0');
RS232_SendByte(cport_nr, '\r');

//usleep(10000);

int d=get_scan();
//printf(" %d \n",d);

for(int i=0;i<NSB;i++)
printf("%d ",localbuf[i]);

printf("\n\n");

// the data is in argv[1], argv[2], and localbuf, append it to the image data file




// get an instance of gnuplot
Gnuplot g1("lines");
//set t wxt persist
//
//Gnuplot::set_terminal_std("--persist");
std::string csp="set t wxt persist";
g1.cmd(csp);
std::vector<double> x, y;

for (int i = 1; i < NSB-2; i++) // fill double arrays x, y
// the first and last 2 points are not really data
// the first is somekind of "begin data marker and maybe the last few are a checksum
{
x.push_back((double)i); // distance
y.push_back((double)localbuf[i]); // amplitude

}

g1.set_grid();
g1.set_style("lines").plot_xy(x,y,"");
g1.set_xlabel("distance");
g1.set_ylabel("Amplitude");

//}

RS232_CloseComport(cport_nr);

}

This is the c++ file that needs to be called to grab a point of ultrasonic data, we are having trouble making this happen. I believe right now when the file is ran it doesn't stop at just one point but continuously takes the same point. This is a problem too.

Any input would be greatly appreciated, let me know if you would like more information about the equipment or programs we are using!

Thanks!

MTechStudent