Author Topic: Rotational Indicator Counter  (Read 2319 times)

joko markono

  • Full Member
  • ***
  • Posts: 132
    • View Profile
Rotational Indicator Counter
« on: May 22, 2023, 06:17:02 AM »
Hi All,

I want to do a counting on number of turn on the rotational indicator on both direction.
For example, if my arrow has made twice clockwise turn, it will show 2 on my apps.
same for CCW. How can I do that on VB.net for this control?

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5322
    • View Profile
    • AdvancedHMI
Re: Rotational Indicator Counter
« Reply #1 on: May 22, 2023, 10:32:00 AM »
Maybe something like this:
Code: [Select]
    Private Sub RotationalIndicator1_ValueChanged(sender As Object, e As EventArgs) Handles RotationalIndicator1.ValueChanged
        Dim turns As Single = RotationalIndicator1.Value / 360
        RotationalIndicator1.Text = turns.ToString("0.0")
    End Sub

joko markono

  • Full Member
  • ***
  • Posts: 132
    • View Profile
Re: Rotational Indicator Counter
« Reply #2 on: May 22, 2023, 05:59:04 PM »
I have tried that but that only true if the direction starts from 0,1,2,3.... 359
what if it starts like this, 0,359,358,357,356....1?

also if the value pass trough 0 again, then the no. of turn will turn back to zero.
« Last Edit: May 22, 2023, 06:01:04 PM by joko markono »

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5322
    • View Profile
    • AdvancedHMI
Re: Rotational Indicator Counter
« Reply #3 on: May 22, 2023, 06:41:56 PM »
The only way to make multiple continuous rotations is to keep going beyond 360

joko markono

  • Full Member
  • ***
  • Posts: 132
    • View Profile
Re: Rotational Indicator Counter
« Reply #4 on: August 30, 2023, 02:33:00 AM »
how to convert this to vb.net please?
it looks like C# and I used the converter but no luck.

Code: [Select]
int Start_Angle;    // The first compass reading to mark our beginning rotation.
                    // signed int, will range 0 to 359
int Last_Angle;     // preserve the value of the last reading.
                    // signed int, will range 0 to 359
int Current_Angle;  // computed position for where we are now.
                    // signed int, will range -720 to +720

void Init_Rotation(int Current_Pos);
void Get_Rotation(int Current_Pos);
                                   
void main(void)
{
  Init_Rotation(10);
  Get_Rotation(20);
 
  Init_Rotation(10);
  Get_Rotation(350);
 
  Init_Rotation(90);
  Get_Rotation(271);
 
  Init_Rotation(20);
  Get_Rotation(10); 
 
  Init_Rotation(350);
  Get_Rotation(10); 
 
  Init_Rotation(271);
  Get_Rotation(90); 
}

void Init_Rotation(int Current_Pos)
{
  // set Start_Angle, Last_Angle,
  // and Current_Angle to the current reading
  Current_Angle = Last_Angle = Start_Angle = Current_Pos;   
}

void Get_Rotation(int Current_Pos)
{
  // will take the current reading,
  // compute the Current_Angle,
  // then update Last_Angle, and Current_Angle
  int Delta;
 
  // compute delta angle
  Delta = Current_Pos - Last_Angle;
 
  // put Delta into range of
  // -180 <= angle <= 180
  if (Delta > 180) Delta -= 360;
  if (Delta < -180) Delta += 360;
 
  // update Current_Angle
  Current_Angle += Delta;
 
  // update last reading with our current reading
  Last_Angle = Current_Pos;   
}

my main problem is that I don't quite understand this part:
Code: [Select]
void main(void)
{
  Init_Rotation(10);
  Get_Rotation(20);
 
  Init_Rotation(10);
  Get_Rotation(350);
 
  Init_Rotation(90);
  Get_Rotation(271);
 
  Init_Rotation(20);
  Get_Rotation(10); 
 
  Init_Rotation(350);
  Get_Rotation(10); 
 
  Init_Rotation(271);
  Get_Rotation(90); 
}

what is does actually?
« Last Edit: August 30, 2023, 04:22:19 AM by joko markono »

Godra

  • Hero Member
  • *****
  • Posts: 1447
    • View Profile
Re: Rotational Indicator Counter
« Reply #5 on: August 30, 2023, 06:55:38 PM »
That code looks like somebody was testing certain values.

Maybe try the following code, it has not been tested by me and might need some corrections:

Code: [Select]
    Dim Delta As Integer
    Dim Last_Angle As Integer = 360
    Dim Total_Turns As Single

    Private Sub RotationalIndicator1_ValueChanged(sender As Object, e As EventArgs) Handles RotationalIndicator1.ValueChanged
        Dim Current_Angle As Integer = RotationalIndicator1.Value

        '* Determine Delta (CW or CCW)
        If Current_Angle > 180 AndAlso Last_Angle = 360 Then
            Delta = Last_Angle - Current_Angle
        ElseIf Current_Angle <= 180 AndAlso Last_Angle = 360 Then
            Delta = Current_Angle
        Else
            Delta = Current_Angle - Last_Angle
        End If

        Total_Turns = Total_Turns + ( Delta / 360 )

        RotationalIndicator1.Text = Total_Turns.ToString("0.0")

        If Current_Angle = 0 Then
            Last_Angle = 360
        Else
            Last_Angle = Current_Angle
        End If
    End Sub


bachphi

  • Hero Member
  • *****
  • Posts: 671
    • View Profile
Re: Rotational Indicator Counter
« Reply #6 on: August 31, 2023, 10:41:17 AM »
This is what I have for determine rotational direction:

         0/360

  90               270

         180

Code: [Select]
CCW = ((Target>Current) AND (Target-Current < 180)) OR ((Current>Target) AND (Current-Target>=180))

CW = ((Target>Current) AND (Target-Current >= 180)) OR ((Current>Target) AND (Current-Target<180))

Say you are at 90, want to move to 180. My brain can easily tell that to move CCW for shortest distance, but for machine, it needs to do some calculation first.
« Last Edit: August 31, 2023, 10:58:12 AM by bachphi »
===================================================
This is NOT alt.read.my.mind.
No such thing is sh^t-for-brains unless you are posting to alt.read.my.mind.
===================================================

joko markono

  • Full Member
  • ***
  • Posts: 132
    • View Profile
Re: Rotational Indicator Counter
« Reply #7 on: September 04, 2023, 04:54:45 AM »
That code looks like somebody was testing certain values.

Maybe try the following code, it has not been tested by me and might need some corrections:

Code: [Select]
    Dim Delta As Integer
    Dim Last_Angle As Integer = 360
    Dim Total_Turns As Single

    Private Sub RotationalIndicator1_ValueChanged(sender As Object, e As EventArgs) Handles RotationalIndicator1.ValueChanged
        Dim Current_Angle As Integer = RotationalIndicator1.Value

        '* Determine Delta (CW or CCW)
        If Current_Angle > 180 AndAlso Last_Angle = 360 Then
            Delta = Last_Angle - Current_Angle
        ElseIf Current_Angle <= 180 AndAlso Last_Angle = 360 Then
            Delta = Current_Angle
        Else
            Delta = Current_Angle - Last_Angle
        End If

        Total_Turns = Total_Turns + ( Delta / 360 )

        RotationalIndicator1.Text = Total_Turns.ToString("0.0")

        If Current_Angle = 0 Then
            Last_Angle = 360
        Else
            Last_Angle = Current_Angle
        End If
    End Sub

I tried this and seems working but because I don't have the compass with me, I can't tell whether it is 100% working. my only concern is the indicator will not display 360, after 359, it will be 0 again. so, I can't tell if the code is fully correct until I got the physical compass later.