Pages: [1]   Go Down
Author Topic: Trying to detect angular twist deformation  (Read 588 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am trying to detect angular twist deformation on a rotor shaft. The idea is to have two sensors along the splines that will output a high voltage when a spline goes across the sensors. If there is a twist in the spline then the sensors will be tripped at different times (extremely small, but still varying times). The program will have two inputs, one for each sensor, and two outputs, pass&fail LED lights.

Here is my code:
Code:
//Program will take two inputs, find time difference between their position as they spin
//The time differences are then averaged out and compared to the time threshold
//Two LEDs are used to designate pass or fail
int X=8;                //8&6 have to be used because they are the serial inputs
int Y=6;                //8 is RX2 and 6 is RX3
int ledPin=30;             //30 & 31 are digital pins
int ledPin2=31;            //They will be connected to LED signals
  int p=570;              //this is the total number of data points collected
  int difference[569];    //570 assumes the shaft rotates at 60rpm for 30 seconds
  int count=0;           //this will increase the array position after each cycle through the loop
  int a,b,c=0;           //these will be the time inputs from the sensors and the temporary differential
  int Z=0;                //Z is the time threshold found from testing
 
void setup(){
  difference[569]=1234;    //Value is arbitrary; used to test when array is full later
  pinMode(ledPin, OUTPUT);    //ledPin and ledPin2 are the two output pins connected to the LEDs
  pinMode(ledPin2, OUTPUT);    //ledPin is the fail light and ledPin2 is the pass light
  Serial.begin(9600);      //initializes serial ports
  Serial2.begin(9600);    //9600 is baud rate and is the standard rate for arduino
  Serial3.begin(9600);
  delay(15000);                    //this will allow the shaft to reach the designated rotating speed
}                     

void loop(){
  if(Serial2.available()>0){      //if there is an
    timestamp();}
  if(Serial3.available()>0){
    timestamp2();}
 
  if(a=!0){
    c=a-b;                     //it will start with the first value of the array and will save the differential here
    difference[count]=c;         //it will take p data points and will stop when the last value is no longer 14
  }                            //the last value won't be 14 after taking value because it is measuring in microseconds
  a=b=c=0;
 
  if(difference[569]=!1234){                                 
  int S=0;                          //this will be the sum of all the differentials
  for(count=0;count!=p;count++){
    S=S+difference[count];
  }                                  //this adds up all the differential amounts into one
  int average=S/p;                  //this is the average time difference
 
  if(average>=Z){
    digitalWrite(ledPin, HIGH);
  }                                   //if the average was above the time threshold the FAIL LED will lightup
  if(average<Z){
    digitalWrite(ledPin2, HIGH);
  }                                    //if the average was below the time threshold the PASS LED will lightup
  delay(60000);           //this will delay the program for 1 minute to allow the technicians to see the results
  }
}

void timestamp(){
   a=(int)micros();}
void timestamp2(){
   b=(int)micros();}

//LIMITATION
//The program assumes that sensor X will definitely be HIGH before or at the same time as sensor Y

I am using a Mega 2560. The program has to be precise to 27 microseconds for the time difference but that shouldn't be an issue.

The problem, I believe, is that the inputs will always be receiving data even when the voltage is low. Is there a way to make serial.available only trip when a high voltage is inputted? And would the program even work if that is accomplished or am I going to have to try something completely different?

Thank you
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48569
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
  difference[569]=1234;    //Value is arbitrary; used to test when array is full later
Writing off the end of the array is not a good way to do that.

Code:
  if(a=!0){
This is assigning not 0 to a. It is not testing a.

Quote
The program has to be precise to 27 microseconds for the time difference but that shouldn't be an issue.
The time measurements are based on when you detected that there was serial data to read. You can't expect the arrival of serial data to be synchronized to an external event to any degree of accuracy.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I tried to use digital pins before using this code:

Code:
for(count=0;difference[569]==1234;count++){
    while(digitalRead(X)==LOW){
    }                              //it will loop doing nothing until sensor X is over 3V
    a=micros();                    //the current time will be saved
    while(digitalRead(Y)==LOW){
    }                              //it will loop doing nothing until sensor Y is over 3V
    b=micros();                    //the current time will be saved
    c=a-b;                     //it will start with the first value of the array and will save the differential here
    difference[count]=c;         //it will take p data points and will stop when the last value is no longer 14
  }

but it didn't work. I figured the while loop wouldn't work the way I had it so I tried serial inputs instead.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48569
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
for(count=0;difference[569]==1234;count++){
The middle clause is a while condition. Why isn't the while condition count < 569?

The 569 is STILL off the end of the array, so difference[569] is random garbage.
Logged

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12579
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I don't get how you expect to achieve any timing at all from a serial port, let alone high precision timing from a serial port running at 9600 rpm. Do you have some sort of sensor that is outputting a serial signal?

What sort of deflection are you envisaging? Am I right to understand that you only want to detect presence/absence of twist, and not measure the amount? Can you clarify the sort of timing you expect in the signals from these sensors i.e. at what frequency is the shaft rotating and what magnitude of angular deflection are you trying to detect? From what I'm imagining, to have any chance of detecting small deflections in a rotating shaft you'd need to get very good timing resolution and I'd expect to need to achieve that in hardware, not a microcontroller listening to a serial port lumbering along at 9600 bps.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Johannesburg. UTC+2
Offline Offline
Faraday Member
**
Karma: 82
Posts: 3882
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
extremely small, but still varying times

Quote
27 microseconds

Presumably that's based on some calculations to do with the applied torque, the expected resistive torque, the torsional rigidity of the material and the geometry of the shaft? Hence the expected strain and given the speed of rotation, an anticipated time lag?

Cunning approach, provided the time lag is discernable....

Logged

The Elders of the Internet know who I am.....

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 96
Posts: 4781
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

As Paul notes, you shouldn't even try using serial data.
You should be watching digital pins and recording times of changes ASAP. You might run tight loops down to 1 or 2 usecs each but the smallest micros() resolution is 4 -- way less than 27.

If you could put reflective or dark spots on the shaft and detect reflection/not from detectors through apertures (to control where the edges are) then you could calibrate that under no-torque.

There's going to be a difference at zero torque just because of tolerances. You don't expect to make those perfect, do you? Just get the data under different conditions and work with that.

Arrrrg! You guys are killing me!

Aluksik, you should NOT wait inside loop() for a pin to go LOW. That is blocking code.

Code:
void loop()
{
    a = b = 0UL; // make a and b global unsigned longs
    if (digitalRead(X)==HIGH)        a=micros();                    //the current time will be saved
    if (digitalRead(Y)==HIGH)        b=micros();                    //the current time will be saved

// and from there you have a bit of work, not a simple a - b at all!
// Maybe better you fill a data array and do the difference processing later.
    if ( a || b ) {

Logged

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The inputs are coming a comparator chip. Unfortunately, I will be testing different shafts in a short time so I cannot add anything to the shafts.

GoForSmoke, I didn't realize the time the loop will take so that should be fine. I will work on it using that code.

Thanks everyone.
Logged

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 96
Posts: 4781
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You are already getting inputs I see.

I should have thought, the splines can serve as 'markers' as long as they reflect. But even then, don't assume perfection in a world that uses the word tolerances. If you don't know what that means, ask a machinist.

Logged

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

Johannesburg. UTC+2
Offline Offline
Faraday Member
**
Karma: 82
Posts: 3882
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

But even then, don't assume perfection in a world that uses the word tolerances.

And don't confuse precision with accuracy.....
Logged

The Elders of the Internet know who I am.....

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48569
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
And don't confuse precision with accuracy.....
Or any of them for repeatability.
Logged

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 96
Posts: 4781
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

When you're going to match sensor events occurring at two different locations on a rotating shaft and expect them to match so exactly without torque that you can measure twist as the absolute difference down to 27 usecs when there is torque, what the heck do you call that?

You see that first you have to synch the events close enough that 27 usecs difference has meaning?

But why bother to physically synchronize what you can easily pull from data? This should be possible using spots made with a marker.

BTW, Machinery's Handbook probably has a formula to figure the twist on a shaft of the diameter at the base of the splines (the root diameter of screw threads) and length required as well as the material of the spline shaft which the same book should also give. For designers that book is "The Bible".

Logged

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

Johannesburg. UTC+2
Offline Offline
Faraday Member
**
Karma: 82
Posts: 3882
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
For designers that book is "The Bible"

I think my brother still has our Dad's version from c1960....
Logged

The Elders of the Internet know who I am.....

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 96
Posts: 4781
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

We had about 1970 version when I was in that school. I have a computer version somewhere and it does go into more detail on tensile behavior, doesn't have all the same tables I remember.

Besides it's more fun to put something through tests. You never get quite the same as the book says.

Logged

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

Pages: [1]   Go Up
Jump to: