Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
convert LSW and MSW to floating point

Subject: convert LSW and MSW to floating point

From: Vihang Patil

Date: 13 Apr, 2012 14:28:18

Message: 1 of 6

Hello
I am getting an array of values from an encoder which is attached to an Data Acquisition Device. The values that I get are in 2 arrays of LSW's amd MSW's
I want that the data to be converted in to floating point representation so that I can use it later for plotting data
I have also made a function to achieve this but the problem is that the function contains bin2dec() which is very slow and due to this my Data Acquisition device throws me an error of Buffer OverRun

function angle = encoder_angle(val)
%Function to calculate Angle from encoder
MSB = val(:,1);
LSB = val(:,2);

LSB = dec2bin(LSB,16);
MSB = dec2bin(MSB,16);
quad_angle = bin2dec([MSB LSB]); % this is slowing my DAQ device
[quad_angle] = TwosComplement(quad_angle, 32);
angle = quad_angle*360/144000; % encoder PPR is 36000, my DAQ sends (36000* 4)
angle = angle';

example:
Q1 =[0,65497;0,65503;0,65509;0,65517;0,65523;0,65529;1,1;1,7;1,13;1,21;1,27;]
angle = encoder_angle(Q1)
angle =

  163.7425 163.7575 163.7725 163.7925 163.8075 163.8225 163.8425 163.8575 163.8725 163.8925 163.9075


I need the above function to operate much faster..
Thanks

Vihang Patil

Subject: convert LSW and MSW to floating point

From: Roger Stafford

Date: 13 Apr, 2012 17:02:32

Message: 2 of 6

"Vihang Patil" <vihang_patil@yahoo.com> wrote in message <jm9d62$hg1$1@newscl01ah.mathworks.com>...
> LSB = dec2bin(LSB,16);
> MSB = dec2bin(MSB,16);
> quad_angle = bin2dec([MSB LSB]); % this is slowing my DAQ device
- - - - - - - - -
  Instead of converting to and from binary strings, why not do this:

 quad_angle = 2^16*val(:,1)+val(:,2);

Roger Stafford

Subject: convert LSW and MSW to floating point

From: James Tursa

Date: 13 Apr, 2012 17:02:32

Message: 3 of 6

What is class(val) ?

Subject: convert LSW and MSW to floating point

From: Vihang Patil

Date: 14 Apr, 2012 11:01:22

Message: 4 of 6

"James Tursa" wrote in message <jm9m78$rnt$1@newscl01ah.mathworks.com>...
> What is class(val) ?

val is of class --> double

Thanks

Vihang

Subject: convert LSW and MSW to floating point

From: Vihang Patil

Date: 14 Apr, 2012 11:56:14

Message: 5 of 6


> Instead of converting to and from binary strings, why not do this:
>
> quad_angle = 2^16*val(:,1)+val(:,2);
>
> Roger Stafford

Thanks Roger..
That helped

Vihang

Subject: convert LSW and MSW to floating point

From: James Tursa

Date: 14 Apr, 2012 18:15:30

Message: 6 of 6

"Vihang Patil" <vihang_patil@yahoo.com> wrote in message <jmble2$bq2$1@newscl01ah.mathworks.com>...
> "James Tursa" wrote in message <jm9m78$rnt$1@newscl01ah.mathworks.com>...
> > What is class(val) ?
>
> val is of class --> double
>
> Thanks
>
> Vihang

Why is it double if you are importing it from a device and you are attempting to use the "bits"? I would have expected it to be uint16. Did you convert it to double? And why are the MSB and LSB separated in memory? I would have expected them to be next to each other.

James Tursa

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us