Society of Robots - Robot Forum

Software => Software => Topic started by: helicopter on December 05, 2014, 10:00:19 PM

Title: MD25 Motor Driver encoder read problem
Post by: helicopter on December 05, 2014, 10:00:19 PM
Hi Guys

I am having troubles getting the motor driver to read the encoder for a EMG30 motor from Devantech.
All other data requests can be returned to USB to display on hyperterminal no problems but when it comes to receiving 4 bytes of data I am stuck!
I have tried most options (arrays, etc) but have yet to crack it! I am missing something.  I have searched and used other examples from the internet all without success.

The Data sheet for the MD25:

When a read encoder command is issued the MD25 will send out 4 bytes high byte first, which should be put together to form a 32 bit signed number. For example a GET ENCODER 1 command may return 0x00,0x10,0x56,0x32.

So declare a 32 bit signed variable in your program, for C:
long result;
 result = serin() << 24;    // (0x00 shifted 24 bits left, effectively * 16777216)
 result += serin() << 16;   // (0x10 shifted 16 bits left, effectively * 65536)
 result += serin() << 8;    // (0x56 shifted 8 bits left, effectively * 256)
 result += serin();         /  (0x32)
result now equals 1070642 decimal or 0x105632 hex. If the highest bit was set then it would be -ve.
 read encoders will send encoder count 1 and then encoder count 2 but is put together in exactly the same way. The registers can be zeroed at any time by writing 0x35 to the MD25.


The encoder read function example from for serial connection is

long readEncoder(){                        // Function to read and display the value of both encoders, returns value of first encoder
  long result1 = 0;
  long result2 = 0;
  while(Serial.available() < 8){}          // Wait for 8 bytes, first 4 encoder 1 values second 4 encoder 2 values
  result1 =;                 // First byte for encoder 1, HH.
  result1 <<= 8;
  result1 +=;                // Second byte for encoder 1, HL
  result1 <<= 8;
  result1 +=;                // Third byte for encoder 1, LH
  result1 <<= 8;
  result1  +=;               // Fourth byte for encoder 1, LL
  result2 =;
  result2 <<= 8;
  result2 +=;
  result2 <<= 8;
  result2 +=;
  result2 <<= 8;
  result2 +=;

A month of failures and I have no other option but to ask for help!

Does anyone have a code for this function to use to grab 4 bytes of data and return a value for use as a counter to get a motor to stop at a given angle!
I am using the Axon 2 with the Devantech MD25 driver.

The joys of programing!
Otherwise the Axon 2 is a great piece of kit, 5 stars for that bad boy SoR!!

Thanks Heli

Title: Re: MD25 Motor Driver encoder read problem
Post by: Webbot on December 06, 2014, 08:16:32 AM
Both those algorithms look ok and are effectively doing the same thing. The first one shifts they byte to the right place before adding to the result. The second adds each byte into the result and repeatedly shifts the result - ie the first byte will get shifted 3 times and 3 x 8 = 24.

You dont say what libraries you are using eg WebbotLib, WebbotLibStudio, Arduino, Atmel AVR Studio 6, WinAVR. or something else.
The reason for asking is that you may actually be building up the 'long' ok - but falling down in the way that you are trying to print it as a number over the USB port. So would be interesting to know how you are doing that.