libraries / Mitov / Mitov_SpeedToSpeedAndDirection.hon commit Added link to project report (97a3ba0)
   1////////////////////////////////////////////////////////////////////////////////
   2//                                                                            //
   3//     This software is supplied under the terms of a license agreement or    //
   4//     nondisclosure agreement with Mitov Software and may not be copied      //
   5//     or disclosed except in accordance with the terms of that agreement.    //
   6//         Copyright(c) 2002-2016 Mitov Software. All Rights Reserved.        //
   7//                                                                            //
   8////////////////////////////////////////////////////////////////////////////////
   9
  10#ifndef _MITOV_SPEED_TO_SPEED_AND_DIRECTION_h
  11#define _MITOV_SPEED_TO_SPEED_AND_DIRECTION_h
  12
  13#include <Mitov.h>
  14
  15namespace Mitov
  16{
  17        class SpeedToSpeedAndDirection : public Mitov::CommonSink
  18        {
  19                typedef Mitov::CommonSink inherited;
  20
  21        public:
  22                OpenWire::SourcePin     DirectionOutputPin;
  23                OpenWire::SourcePin     SpeedOutputPin;
  24
  25        public:
  26                bool    Enabled : 1;
  27                bool    InverseForward : 1;
  28                bool    InverseReverse : 1;
  29
  30        public:
  31                void    SetEnabled( bool AValue ) 
  32                {
  33                        if( Enabled == AValue )
  34                                return;
  35
  36                        Enabled = AValue;
  37                        UpdateOutputs();
  38                }
  39
  40                void    SetInverseForward( bool AValue ) 
  41                {
  42                        if( InverseForward == AValue )
  43                                return;
  44
  45                        InverseForward = AValue;
  46                        UpdateOutputs();
  47                }
  48
  49                void    SetInverseReverse( bool AValue ) 
  50                {
  51                        if( InverseReverse == AValue )
  52                                return;
  53
  54                        InverseReverse = AValue;
  55                        UpdateOutputs();
  56                }
  57
  58        protected:
  59                float   FCurrentSpeed = 0.5f;
  60
  61        protected:
  62                virtual void DoReceive( void *_Data )
  63                {
  64                        float ASpeed = constrain( *(float *)_Data, 0.0, 1.0 );
  65                        if( FCurrentSpeed == ASpeed )
  66                                return;
  67
  68                        FCurrentSpeed = ASpeed;
  69                        UpdateOutputs();
  70                }
  71
  72                void    UpdateOutputs()
  73                {
  74                        if( Enabled )
  75                        {
  76                                float AOutSpeed = abs( FCurrentSpeed - 0.5 ) * 2;
  77                                bool ADirection = FCurrentSpeed > 0.5;
  78
  79                                if( ADirection )
  80                                {
  81                                        if( InverseForward )
  82                                                AOutSpeed = 1 - AOutSpeed;
  83                                }
  84
  85                                else
  86                                {
  87                                        if( InverseReverse )
  88                                                AOutSpeed = 1 - AOutSpeed;
  89                                }
  90
  91                                SpeedOutputPin.Notify( &AOutSpeed );
  92                                DirectionOutputPin.Notify( &ADirection );
  93                        }
  94
  95                        else
  96                        {
  97                                SpeedOutputPin.SendValue( 0 );
  98                                DirectionOutputPin.SendValue( false );
  99                        }
 100                }
 101
 102        protected:
 103                virtual void SystemStart()
 104                {
 105                        inherited::SystemStart();
 106                        UpdateOutputs();
 107                }
 108
 109        public:
 110                SpeedToSpeedAndDirection() :
 111                        Enabled( true ),
 112                        InverseForward( false ),
 113                        InverseReverse( false )
 114                {
 115                }
 116
 117        };
 118}
 119
 120#endif