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