Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

amc bugfix reading aea3 bitshift #420

Merged
merged 1 commit into from
Sep 29, 2023

Conversation

sgiraz
Copy link
Contributor

@sgiraz sgiraz commented Sep 29, 2023

What's new

  • Fix the bitshift reading of chip_MA730 (aka AEA3) on the AMC board

Brief description of the reading

We know that the chip_MA730 has a resolution of 14 bits, so it can count values from 0 to 16383.
We also know that in order to perform a reading we have to send 2 bytes to the clock source.
Finally, from the Datasheet, we know that the first bit of the Byte read is a dummy bit and the last bit of the second Byte is a zero-padded bit.
Let's see how to read the case of maximum value in summary:

Step 0 (initial conditions)

7 6 5 4 3 2 1 0
Buffer[0] [dummy] 1 1 1 1 1 1 1
Buffer[1] 1 1 1 1 1 1 1 [zero padded]
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
position - - - - - - - - - - - - - - - -

Step 1 (clip Buffer[0] by removing the dummy bit (mask it with 0x7F) and shift it to the left by 7 positions)

15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
position - 0 1 1 1 1 1 1 1 0 0 0 0 0 0 0

Step 2 (shift Buffer[1] by 1 position to the right, then perform the OR operation with position)

15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
position - 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1

In the last step, position contains our final 14-bit value ---> 16383.

Furthermore here is the code used to validate the chip_MA730:

#include <iostream>
#include <bitset>
#include <cstdint>

int main(void) {
    uint16_t position = {0};
    uint8_t _databuffer[2] = {0};

    // generate values from 0 to 16383 and try to validate them using the bitshift formula
    
    for(int i = 0; i < 128; i++) {
        
        for(int j = 0; j < 128; j++) {
            
            position = (_databuffer[0] << 7) | (_databuffer[1] >> 1);
            std::cout << std::bitset<8>(_databuffer[0]) << "" 
                      << std::bitset<8>(_databuffer[1]) << " --> "
                      << "position: " << position << std::endl;
            
            _databuffer[1] += 2;
        }
         _databuffer[0] += 1;
    }
    
    return 0;
}

cc @valegagge @ale-git

@sgiraz sgiraz added the bugfix label Sep 29, 2023
@sgiraz sgiraz requested a review from marcoaccame September 29, 2023 10:39
@sgiraz sgiraz self-assigned this Sep 29, 2023
@marcoaccame marcoaccame merged commit 8640e60 into robotology:devel Sep 29, 2023
valegagge pushed a commit to ale-git/icub-firmware that referenced this pull request Oct 3, 2023
@sgiraz sgiraz deleted the feat/aea3_reading_bugfix branch October 13, 2023 08:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants