libraries / Mitov / Mitov_L9110SDualMotorDriver.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_L9110S_DUAL_MOTOR_h
  11#define _MITOV_L9110S_DUAL_MOTOR_h
  12
  13#include <Mitov.h>
  14
  15namespace Mitov
  16{
  17        class L9110SDualMotorDriverChannel : 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 = true;
  27
  28        public:
  29                void    SetEnabled( bool AValue ) 
  30                {
  31                        if( Enabled == AValue )
  32                                return;
  33
  34                        Enabled = AValue;
  35                        UpdateOutputs();
  36                }
  37
  38        protected:
  39                float   FCurrentSpeed = 0.5f;
  40
  41        protected:
  42                virtual void DoReceive( void *_Data ) override
  43                {
  44                        float ASpeed = constrain( *(float *)_Data, 0.0, 1.0 );
  45                        if( FCurrentSpeed == ASpeed )
  46                                return;
  47
  48                        FCurrentSpeed = ASpeed;
  49                        UpdateOutputs();
  50                }
  51
  52                void    UpdateOutputs()
  53                {
  54                        if( Enabled )
  55                        {
  56                                float AOutSpeed = abs( FCurrentSpeed - 0.5 ) * 2;
  57                                bool ADirection = FCurrentSpeed > 0.5;
  58
  59                                if( ADirection )
  60                                        AOutSpeed = 1 - AOutSpeed;
  61
  62                                SpeedOutputPin.Notify( &AOutSpeed );
  63                                DirectionOutputPin.Notify( &ADirection );
  64                        }
  65
  66                        else
  67                        {
  68                                SpeedOutputPin.SendValue( 0 );
  69                                DirectionOutputPin.SendValue( false );
  70                        }
  71                }
  72
  73        protected:
  74                virtual void SystemStart() override
  75                {
  76                        inherited::SystemStart();
  77                        UpdateOutputs();
  78                }
  79
  80        };
  81//---------------------------------------------------------------------------
  82}
  83
  84#endif