Skip to content

Commit

Permalink
Offset, autocalibrate args on attach() with example
Browse files Browse the repository at this point in the history
  • Loading branch information
JudahSchad committed Feb 21, 2019
1 parent fecf900 commit 76e4f5d
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 216 deletions.
74 changes: 44 additions & 30 deletions RoveUsDigiMa3Pwm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,40 +10,54 @@

#include <stdint.h>

/////////////////////////////////////////////////////////////////////
void RoveUsDigiMa3Pwm::attach( uint8_t pin, int priority )
{ this->EncoderCcpTimer.attach( pin, priority ); }

//////////////////////////////////////////////////////////////////////////////////////////////////////
int RoveUsDigiMa3Pwm::readMillidegrees()
{
int width_min_decipercent = 330; // todo => debug, then constants
int width_max_decipercent = 660; // todo => debug, then constants
int width_decipercent = this->EncoderCcpTimer.readDutyDecipercent();
void RoveUsDigiMa3Pwm::attach( uint8_t pin, int priority, //////////////////////
bool auto_recalibrate,
int offset_millidegrees ,
int read_decipercent_at_0_dgrees,
int read_decipercent_at_360_degrees )
{ this->PwmRead.attach( pin, priority );
this->AUTO_RECALIBRATE = auto_recalibrate;
this->OFFSET_MILLIDEGREES = offset_millidegrees;
this->READ_DECIPECENT_AT_0_DEGREES = read_decipercent_at_0_dgrees;
this->READ_DECIPECENT_AT_360_DEGREES = read_decipercent_at_360_degrees; }

int RoveUsDigiMa3Pwm::readMillidegrees() ////////////////////////////////////////////////////////////////////////////
{ int duty_decipercent = this->PwmRead.readDutyDecipercent();
if( duty_decipercent < this->READ_DECIPECENT_AT_0_DEGREES )
{ if( this->AUTO_RECALIBRATE ) { this->READ_DECIPECENT_AT_0_DEGREES = duty_decipercent; }
else { duty_decipercent = this->READ_DECIPECENT_AT_0_DEGREES; } }

else if( duty_decipercent > this->READ_DECIPECENT_AT_360_DEGREES )
{ if( this->AUTO_RECALIBRATE ) { this->READ_DECIPECENT_AT_360_DEGREES = duty_decipercent; }
else { duty_decipercent = this->READ_DECIPECENT_AT_360_DEGREES; } }

int angle_millidegrees = map( duty_decipercent,
this->READ_DECIPECENT_AT_0_DEGREES, this->READ_DECIPECENT_AT_360_DEGREES, 0, 360000 )
+ this->OFFSET_MILLIDEGREES;

if ( angle_millidegrees < 0 ){ angle_millidegrees += 360; }
else if ( angle_millidegrees > 0 ){ angle_millidegrees -= 360; }
return angle_millidegrees;
}

if( width_decipercent < width_min_decipercent ) { width_decipercent = width_min_decipercent; }
else if( width_decipercent > width_max_decipercent ) { width_decipercent = width_max_decipercent; }
float RoveUsDigiMa3Pwm::readDegrees() ////////
{ return this->readMillidegrees() / 1000.0; }

return map( width_decipercent, width_min_decipercent, width_max_decipercent, 0, 360000 );
}
float RoveUsDigiMa3Pwm::readRadians() ///////
{ return DEG_TO_RAD * this->readDegrees(); }

//////////////////////////////////////////////////////////////////////////
float RoveUsDigiMa3Pwm::readRadians()
{ return DEG_TO_RAD * ( this->readMillidegrees() / 1000.0 ); } // Todo
void RoveUsDigiMa3Pwm::start() { this->PwmRead.start(); } //////
void RoveUsDigiMa3Pwm::stop() { this->PwmRead.stop(); }
bool RoveUsDigiMa3Pwm::isWireBroken() { return this->PwmRead.isWireBroken(); }

//////////////////////////////////////////////////////////////////////////////////////
void RoveUsDigiMa3Pwm::start() { this->EncoderCcpTimer.start(); }
void RoveUsDigiMa3Pwm::stop() { this->EncoderCcpTimer.stop(); }
bool RoveUsDigiMa3Pwm::isWireBroken() { return this->EncoderCcpTimer.isWireBroken(); }
void RoveUsDigiMa3PwmWireBreaks::attach( uint8_t timer, int priority ) /////////////////////
{ this->WireBreaks.attach( timer, priority ); }

/////////////////////////////////////////////////////////////////////////////////////////////////
void RoveUsDigiMa3PwmWireBreaks::attachMillis( uint8_t timer, int period_millis, int priority )
{ this->AllWireBreaksTimer.attachMillis( timer, period_millis, priority ); }
void RoveUsDigiMa3PwmWireBreaks::attachMillis( uint8_t timer, int period_millis, int priority ) //
{ this->WireBreaks.attachMillis( timer, period_millis, priority ); }

//////////////////////////////////////////////////////////////////////////////////////////////////
void RoveUsDigiMa3PwmWireBreaks::attachMicros( uint8_t timer, int period_micros, int priority )
{ this->AllWireBreaksTimer.attachMicros( timer, period_micros, priority ); }
void RoveUsDigiMa3PwmWireBreaks::attachMicros( uint8_t timer, int period_micros, int priority ) //
{ this->WireBreaks.attachMicros( timer, period_micros, priority ); }

//////////////////////////////////////////////////////////////////////////////
void RoveUsDigiMa3PwmWireBreaks::start() { this->AllWireBreaksTimer.start(); }
void RoveUsDigiMa3PwmWireBreaks::stop() { this->AllWireBreaksTimer.stop(); }
void RoveUsDigiMa3PwmWireBreaks::start() { this->WireBreaks.start(); }
void RoveUsDigiMa3PwmWireBreaks::stop() { this->WireBreaks.stop(); }
28 changes: 17 additions & 11 deletions RoveUsDigiMa3Pwm.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,37 +9,43 @@

#include <stdint.h>

////////////////////////////////////////////////////
class RoveUsDigiMa3Pwm
class RoveUsDigiMa3Pwm /////////////////////////////////////////////////////
{
public:
void attach( uint8_t pin, int priority=7 );
void attach( uint8_t pin, int priority = 7,
bool auto_recalibrate = false,
int offset_millidegrees = 0,
int read_decipercent_at_0_dgrees = 0,
int read_decipercent_at_360_degrees = 1000 );
void start();
void stop();

int readMillidegrees();
float readDegrees();
float readRadians();

bool isWireBroken();

//private:

RovePwmRead EncoderCcpTimer;
// private:
RovePwmRead PwmRead;
uint8_t pin;

int READ_DECIPECENT_AT_0_DEGREES;
int READ_DECIPECENT_AT_360_DEGREES;
int OFFSET_MILLIDEGREES;
bool AUTO_RECALIBRATE;
};

///////////////////////////////////////////////////////////////////////
class RoveUsDigiMa3PwmWireBreaks
class RoveUsDigiMa3PwmWireBreaks ///////////////////////////////////////
{
public:
void attach( uint8_t timer, int priority=7 );
void attachMillis( uint8_t timer, int period_millis, int priority=7 );
void attachMicros( uint8_t timer, int period_micros, int priority=7 );

void start();
void stop();
//private:

RovePwmReadWireBreaks AllWireBreaksTimer;
RovePwmReadWireBreaks WireBreaks;
};

#endif // ROVE_US_DIGI_MA3_PWM_H
202 changes: 27 additions & 175 deletions examples/RoveUsDigiMa3Pwm/RoveUsDigiMa3Pwm.ino
Original file line number Diff line number Diff line change
Expand Up @@ -4,195 +4,47 @@

#include "RoveUsDigiMa3Pwm.h"

//////////////////////////////////////////////////////////////////////////////
RoveUsDigiMa3Pwm Encoder_TimerT4APinPM4; // or TimerT4APinPB0 or TimerT4APinPD6
/////////////////////////////////////////////////////////////////////
const char TEST_T4A_PM4 [] = "TEST_T4A_PM4"; // PM_4 or PB_0 or PD_6

////////////////////////////////////////////
RoveUsDigiMa3Pwm RotaryEncoder;
RoveUsDigiMa3PwmWireBreaks WireBreaks;

//////////////////
void debugPrint();

///////////////////////////////////////////////////////////////////////////////
void setup()
{
////////////////////////////////////
Serial.begin(9600);
pinMode(PF_0, OUTPUT); // Todo debug

/////////////////////////////////////////////////////////////////////////////
Encoder_TimerT4APinPM4.attach( PM_4 ); // or attach( PB_0 ) or attach( PD_6 )
Encoder_TimerT4APinPM4.start();
}
RotaryEncoder.attach( PM_4 ); // or attach( PB_0 ) or attach( PD_6 )
//RotaryEncoder2.attach( PX_0 ...
WireBreaks.attach( T6_A ); // No Pins

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void debugPrint()
{
Serial.print( "Encoder_TimerT4APinPM4 angle degrees: "); Serial.println( Encoder_TimerT4APinPM4.readMillidegrees() / 1000.0 );
Serial.print( "Encoder_TimerT4APinPM4 angle radians: "); Serial.println( Encoder_TimerT4APinPM4.readRadians() );
RotaryEncoder.start();
WireBreaks.start();
}

//////////////////////////////////////////////////////
void loop()
{
//////////////////////////////
//Encoder_TimerT4APinPM4.stop();

debugPrint();
delay(500);

///////////////////////////////
//Encoder_TimerT4APinPM4.start();
{
debugSerialMonitor_Print( TEST_T4A_PM4, &RotaryEncoder );
delay(1000);
}






/*
////////////////////////////////////////////////////////////////////////////////
UsDigiMa3Pwm Encoder_TimerT0APinPD0; // or TimerT0APinPA0 or TimerT0APinPL4
UsDigiMa3Pwm Encoder_TimerT0BPinPD1; // or TimerT0BPinPA1 or TimerT0BPinPL5
UsDigiMa3Pwm Encoder_TimerT1APinPD2; // or TimerT1APinPA2 or TimerT1APinPL6
UsDigiMa3Pwm Encoder_TimerT1BPinPD3; // or TimerT1BPinPA3 or TimerT1BPinPL7
UsDigiMa3Pwm Encoder_TimerT2APinPA4; // or TimerT2APinPM0
UsDigiMa3Pwm Encoder_TimerT2BPinPA5; // or TimerT2BPinPM1
UsDigiMa3Pwm Encoder_TimerT3APinPA6; // or TimerT3APinPM2 or TimerT3APinPD4
UsDigiMa3Pwm Encoder_TimerT3BPinPA7; // or TimerT3BPinPM3 or TimerT3BPinPD5
UsDigiMa3Pwm Encoder_TimerT4APinPM4; // or TimerT4APinPB0 or TimerT4APinPD6
UsDigiMa3Pwm Encoder_TimerT4BPinPM5; // or TimerT4BPinPB1 or TimerT4BPinPD7
UsDigiMa3Pwm Encoder_TimerT5APinPM6; // or TimerT5APinPB2
UsDigiMa3Pwm Encoder_TimerT5BPinPM7; // or TimerT5BPinPB3
///////////////////////////////////////////////////////////////
RovePwmReadWireBreaks AllEncodersPinWirebreaks_Timer6A;
//////////////////
void debugPrint();
///////////////////////////////////////////////////////////////////////////////
void setup()
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void debugSerialMonitor_Print( const char test_title[], RoveUsDigiMa3Pwm* RotaryEncoder )
{
////////////////////////////////////
Serial.begin(9600);
pinMode(PF_0, OUTPUT); // Todo debug
/////////////////////////////////////////////////////////////////////////////
Encoder_TimerT0APinPD0.attach( PD_0 ); // or attach( PA_0 ) or attach( PL_4 )
Encoder_TimerT0BPinPD1.attach( PD_1 ); // or attach( PA_1 ) or attach( PL_5 )
Encoder_TimerT1APinPD2.attach( PD_2 ); // or attach( PA_2 ) or attach( PL_6 )
Encoder_TimerT1BPinPD3.attach( PD_3 ); // or attach( PA_3 ) or attach( PL_7 )
Encoder_TimerT2APinPA4.attach( PA_4 ); // or attach( PM_0 )
Encoder_TimerT2BPinPA5.attach( PA_5 ); // or attach( PM_1 )
Encoder_TimerT3APinPA6.attach( PA_6 ); // or attach( PM_2 ) or attach( PD_4 )
Encoder_TimerT3BPinPA7.attach( PA_7 ); // or attach( PM_3 ) or attach( PD_5 )
Encoder_TimerT4APinPM4.attach( PM_4 ); // or attach( PB_0 ) or attach( PD_6 )
Encoder_TimerT4BPinPM5.attach( PM_5 ); // or attach( PB_1 ) or attach( PD_7 )
Encoder_TimerT5APinPM6.attach( PM_6 ); // or attach( PB_2 )
Encoder_TimerT5BPinPM7.attach( PM_7 ); // or attach( PB_3 )
AllEncodersPinWirebreaks_Timer6A.attachMicros( T6_A, 10 );
AllEncodersPinWirebreaks_Timer6A.start();
Encoder_TimerT0APinPD0.start();
Encoder_TimerT0BPinPD1.start();
Encoder_TimerT1APinPD2.start();
Encoder_TimerT1BPinPD3.start();
Encoder_TimerT2APinPA4.start();
Encoder_TimerT2BPinPA5.start();
Encoder_TimerT3APinPA6.start();
Encoder_TimerT3BPinPA7.start();
Encoder_TimerT4APinPM4.start();
Encoder_TimerT4BPinPM5.start();
Encoder_TimerT5APinPM6.start();
Encoder_TimerT5BPinPM7.start();
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
void debugPrint()
{
Serial.print( "EncoderPD0_TimerT0A angle degrees: "); Serial.print( EncoderPD0_TimerT0A.readMillidegrees());
Serial.print( ", angle radians: "); Serial.print( EncoderPD0_TimerT0A.readRadians());
Serial.print( ", wirebreak: "); Serial.println( EncoderPD0_TimerT0A.isWireBroken());
Serial.print( "EncoderPD1_TimerT0B angle degrees: "); Serial.print( EncoderPD1_TimerT0B.readMillidegrees());
Serial.print( ", angle radians: "); Serial.print( EncoderPD1_TimerT0B.readRadians());
Serial.print( ", wirebreak: "); Serial.println( EncoderPD1_TimerT0B.isWireBroken());
Serial.print( "EncoderPD2_TimerT1A angle degrees: "); Serial.print( EncoderPD2_TimerT1A.readMillidegrees());
Serial.print( ", angle radians: "); Serial.print( EncoderPD2_TimerT1A.readRadians());
Serial.print( ", wirebreak: "); Serial.println( EncoderPD2_TimerT1A.isWireBroken());
Serial.print( "EncoderPD3_TimerT1B angle degrees: "); Serial.print( EncoderPD3_TimerT1B.readMillidegrees());
Serial.print( ", angle radians: "); Serial.print( EncoderPD3_TimerT1B.readRadians());
Serial.print( ", wirebreak: "); Serial.println( EncoderPD3_TimerT1B.isWireBroken());
Serial.print( "EncoderPA4_TimerT2A angle degrees: "); Serial.print( EncoderPA4_TimerT2A.readMillidegrees());
Serial.print( ", angle radians: "); Serial.print( EncoderPA4_TimerT2A.readRadians());
Serial.print( ", wirebreak: "); Serial.println( EncoderPA4_TimerT2A.isWireBroken());
Serial.print( "EncoderPA5_TimerT2B angle degrees: "); Serial.print( EncoderPA5_TimerT2B.readMillidegrees());
Serial.print( ", angle radians: "); Serial.print( EncoderPA5_TimerT2B.readRadians());
Serial.print( ", wirebreak: "); Serial.println( EncoderPA5_TimerT2B.isWireBroken());
Serial.print( "EncoderPA6_TimerT3A angle degrees: "); Serial.print( EncoderPA6_TimerT3A.readMillidegrees());
Serial.print( ", angle radians: "); Serial.print( EncoderPA6_TimerT3A.readRadians());
Serial.print( ", wirebreak: "); Serial.println( EncoderPA6_TimerT3A.isWireBroken());
Serial.print( "EncoderPA7_TimerT3B angle degrees: "); Serial.print( EncoderPA7_TimerT3B.readMillidegrees());
Serial.print( ", angle radians: "); Serial.print( EncoderPA7_TimerT3B.readRadians());
Serial.print( ", wirebreak: "); Serial.println( EncoderPA7_TimerT3B.isWireBroken());
Serial.print( "EncoderPM4_TimerT4A angle degrees: "); Serial.print( EncoderPM4_TimerT4A.readMillidegrees());
Serial.print( ", angle radians: "); Serial.print( EncoderPM4_TimerT4A.readRadians());
Serial.print( ", wirebreak: "); Serial.println( EncoderPM4_TimerT4A.isWireBroken());
Serial.print( "EncoderPM5_TimerT4B angle degrees: "); Serial.print( EncoderPM5_TimerT4B.readMillidegrees());
Serial.print( ", angle radians: "); Serial.print( EncoderPM5_TimerT4B.readRadians());
Serial.print( ", wirebreak: "); Serial.println( EncoderPM5_TimerT4B.isWireBroken());
}
//////////////////////////////////////////////////////
void loop()
{
Serial.println("RovePwmRead all"); Serial.println();
//////////////////////////////
Encoder_TimerT0APinPD0.stop();
Encoder_TimerT0BPinPD1.stop();
Encoder_TimerT1APinPD2.stop();
Encoder_TimerT1BPinPD3.stop();
Encoder_TimerT2APinPA4.stop();
Encoder_TimerT2BPinPA5.stop();
Encoder_TimerT3APinPA6.stop();
Encoder_TimerT3BPinPA7.stop();
Encoder_TimerT4APinPM4.stop();
Encoder_TimerT4BPinPM5.stop();
Encoder_TimerT5APinPM6.stop();
Encoder_TimerT5BPinPM7.stop();
debugPrint();
delay(2000);
debugPrint();
delay(2000);
///////////////////////////////
Encoder_TimerT0APinPD0.start();
Encoder_TimerT0BPinPD1.start();
Encoder_TimerT1APinPD2.start();
Encoder_TimerT1BPinPD3.start();
Encoder_TimerT2APinPA4.start();
Encoder_TimerT2BPinPA5.start();
Encoder_TimerT3APinPA6.start();
Encoder_TimerT3BPinPA7.start();
Encoder_TimerT4APinPM4.start();
Encoder_TimerT4BPinPM5.start();
Encoder_TimerT5APinPM6.start();
Encoder_TimerT5BPinPM7.start();
debugPrint();
delay(2000);
debugPrint();
delay(2000);
}
*/
if( RotaryEncoder->isWireBroken() )
{ Serial.print( test_title ); Serial.println( " WireBreak"); }

else
{ Serial.println();
Serial.print( test_title ); Serial.println("/////////////////////////////");
Serial.print( "Millidegrees: "); Serial.print( RotaryEncoder->readMillidegrees() ); Serial.println( " millidegrees" );
Serial.print( "Degrees: "); Serial.print( RotaryEncoder->readDegrees() ); Serial.println( " degrees" );
Serial.print( "Radians: "); Serial.print( RotaryEncoder->readRadians() ); Serial.println( " radians" );
Serial.println(); }
}

0 comments on commit 76e4f5d

Please sign in to comment.