Page 1 of 2 12 LastLast
Results 1 to 10 of 18

Thread: On Bioloid: How to read the sensors' value with Matlab?

  1. Question On Bioloid: How to read the sensors' value with Matlab?

    I want to read the input of my IR sensor with MATLAB, but I'm not capable.

    I've checked on the Bioloid site under Software Help > Embedded C > Example > IR Sensor, the C exemple:

    Code:
    PORTA &= ~0x80; // ADC Port 1 IR ON
     
    _delay_us(12); // Short Delay for rising sensor signal
    ADCSRA |= (1 << ADIF); // AD-Conversion Interrupt Flag Clear
    ADCSRA |= (1 << ADSC); // AD-Conversion Start
     
    while( !(ADCSRA & (1 << ADIF)) ); // Wait until AD-Conversion complete
     
    PORTA = 0xFC; // IR-LED Off
     
    printf( "%d\r\n", ADC); // Print Value on USART
    _delay_ms(50);
    but I don't get this code neither.
    For example, what does this means: |= ?
    Last edited by cybernetics; 07-27-2011 at 08:41 AM. Reason: addition of the location of the example
    Mathieu, student in physical engineering at Laval University

  2. #2

    Talking Re: On Bioloid: How to read the sensors' value with Matlab?

    It is amazing you go to a University where they neither help you with C, or with Google.

    You need a C tutorial !

    Look here:
    http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B

    It's not your fault, I guess Laval goes on the list of degree factories! I hope the is cos you got a scholarship, else you are being ripped off.

  3. #3
    Join Date
    Sep 2010
    Location
    ಠ_ಠ
    Posts
    2,313
    Images
    27
    Rep Power
    278

    Re: On Bioloid: How to read the sensors' value with Matlab?

    Which port? There are six available ADC ports (labeled with a number of '|' on the CM-510 exterior). You can only read from one at a time and must select one before you attempt to acquire a value. Check my post on the Robotis QnA for a compilation of the preprocessor definitions/macros from the Robotis example code. I tried to explain almost everything available to the CM-510/700. All else fails, RTFM.

    Seriously though, a c/c++ tutorial will do wonders. I still refer to this site frequently for the specifics of the standard c library functions.

    Code:
     
    // The 'DDRA', 'PORTA', and 'PINA' are preprocessor definitions specific to the AVR 
    //  microcontroller architecture (internal registers): RTFM for more 
    // Read/Write to 'PORTA' controls the usage of pull-up resistors when in input mode 
    // Read/Write to 'PORTA' also controls the state (high or low) when in output mode 
    // Read/Write to 'DDRA' controls whether Pins on PORTA are in input or output mode 
    // Read from 'PINA' gives the current state of the pins on PORTA (high or low) 
    // Write a '1' to any bit of 'PINA' toggles the current state of the pin if 
    //  in output mode (bit-wise XOR). 
    // So in earlier code, the line: 
    DDRA |= 0x80; 
    // sets PIN7 of PORTA to be an output pin. 
     
    // So later, when we encounter: 
    PORTA &= ~0x80; 
    // This is bit-wise AND'ing the current value of PORTA with the one's complement 
    //  of 0x80 and storing it back into PORTA 
    // expanded form: PORTA = (PORTA&0x7F); 
    // This clears bit-7 of PORTA, which forces PIN7 of PORTA to a low state while 
    //  leaving all other pins on PORTA to be exactly as they were. 
     
    // You are attempting to use "PORT 1" of the CM-510 with an IR sensor, so you have 
    //  to select which ADC port/channel you intend to use (fortunately, Robotis made 
    //  their "PORT 1" use ADC channel 1) 
    ADMUX = 1; 
    // Selects channel 1 as the current ADC port/channel as input to the converter 
     
    _delay_us(12); // Short Delay for rising sensor signal 
     
    // Next, you are logical OR'ing the ADCSRA register of the ATmega2561 with 
    //  '1' left-bit shifted 'ADIF' times (where ADIF is a preprocessor definition for 
    //  an integer value corresponding to the bit location within the register. 
    //  I do not recall its exact value and do not feel like downloading the 
    //  datasheet again to find out.) 
    // e.g. (1<<4) == (0b0000 0001<<4) == (0b0001 0000) == (0x10) == 1*(2^4) 
    // if ADIF were == 4, the expanded form would be: ADCSRA = (ADCSRA | (0x10)); 
    // which sets bit-4 of ADCSRA 
    ADCSRA |= (1 << ADIF); // AD-Conversion Interrupt Flag Clear 
    // The next line begins the new conversion 
    ADCSRA |= (1 << ADSC); // AD-Conversion Start 
     
    // while ( the ADC complete flag bit is clear );  // !(0)==1, !(N!=0)==0; (0 to 1 and nonzero to 0) 
    while( !(ADCSRA & (1 << ADIF)) ); // Wait until AD-Conversion complete 
     
    PORTA = 0xFC; // IR-LED Off 
    printf( "%d\r\n", ADC); // Print Value on USART 
    _delay_ms(50);
    Last edited by tician; 07-27-2011 at 06:21 PM.
    Please pardon the pedantry... and the profanity... and the convoluted speech pattern...
    "You have failed me, Brain!"
    [git][mech][hack]
    gives free advice only on public threads

  4. Question Re: On Bioloid: How to read the sensors' value with Matlab?

    I've been trying for a couple of hours to traduce this in Matlab and here is what I've got:

    Code:
    bitand(wait_time,~0x80); % ADC Port 1 IR ON
     
    wait_time(0.012); % Short Delay for rising sensor signal
    
    
    ADCSRA=bitand(ADCSRA,(bitsll(1,ADIF))); % AD-Conversion Interrupt Flag Clear
    ADCSRA=bitand(ADCSRA,(bitsll(1,ADSC))); % AD-Conversion Start
     
    while( ~(ADCSRA & bitsll(1,ADIF)) ); % Wait until AD-Conversion complete
     
    PORTA = 0xFC; % IR-LED Off
     
    fprintf('%d\n', ADC); % Print Value on USART
    wait_time(0.050);
    And it doesn't work -_-
    How do I put the "0x80" in Matlab?
    Last edited by cybernetics; 07-27-2011 at 02:08 PM. Reason: improved code
    Mathieu, student in physical engineering at Laval University

  5. #5
    Join Date
    Sep 2010
    Location
    ಠ_ಠ
    Posts
    2,313
    Images
    27
    Rep Power
    278

    Re: On Bioloid: How to read the sensors' value with Matlab?





    The Robotis Embedded-C code for the CM-5/510/700 is meant to run on the CM-5/510/700 only. 'ADCSRA', 'DDRA', 'PORTA', 'PINA', 'ADC', and many others are all C-preprocessor definitions/macros which make sense only when compiled for the AVR ATmega2561 microcontroller on the CM-5/510/700. You cannot convert the Embedded-C examples to MATLAB or any other programming language that cannot be run on an AVR microcontroller.

    As to "0x80", I have two words for you: 'F1' and 'google'.

    Hopefully I will be able to fix my last post when I get home to a functional internet connection.
    Please pardon the pedantry... and the profanity... and the convoluted speech pattern...
    "You have failed me, Brain!"
    [git][mech][hack]
    gives free advice only on public threads

  6. Re: On Bioloid: How to read the sensors' value with Matlab?

    ok I thought there was a way to read sensors' values with Matlab (hence my original question)

    Oh I didn't know F1 could open a tab.

    By the way, I do not post any questions before having looked on the Internet for at least 1 hour. I kind of get what is 0x80, but I just don't really know how to convert it in numbers.

    I'll try using Matlab and C at the same time.

    Thanks.
    Mathieu, student in physical engineering at Laval University

  7. #7
    Join Date
    Sep 2010
    Location
    ಠ_ಠ
    Posts
    2,313
    Images
    27
    Rep Power
    278

    Re: On Bioloid: How to read the sensors' value with Matlab?

    Gave myself a good smack upside the head for somehow NOT reading the 'Matlab' in the thread title.

    'F1' - the nearly universal key to press for "HELP".


    I'm just rambling on now...
    Smallest possible unit is a 'bit'. There are two states '1' and '0'. There are four bits in a 'nibble', which is a single hexadecimal digit. There are eight bits, or two nibbles, in a byte.

    When considering only a single byte (two hexadecimal digits):
    0x80 [hexadecimal] == 0b1000 0000 [binary (notation varies)] == (1<<7) [c/c++ bit-shift left operation] == 1*(2^7) [unsigned decimal] == 128 [unsigned decimal] == -128 [signed decimal]

    More signed conversion examples (that should be correct):
    0x7F [hex] == 127 [unsigned decimal] == 127 [signed decimal]
    0x8F [hex] == 143 [unsigned decimal] == -113 [signed decimal]
    1000 1111 => (one's complement or bit-wise negation) => 0111 0000 => (plus 1) => 0111 0001 => -(64+32+16+1) == -113
    0x82 [hex] == 130 [unsigned decimal] == -126 [signed decimal]
    1000 0010 => 0111 1101 => 0111 1110 => -(64+32+16+8+4+2) == -126
    0xFF [hex] == 255 [unsigned decimal] == -1 [signed decimal]
    1111 1111 => 0000 0000 => 0000 0001 => -(1) == -1
    Please pardon the pedantry... and the profanity... and the convoluted speech pattern...
    "You have failed me, Brain!"
    [git][mech][hack]
    gives free advice only on public threads

  8. Post Re: On Bioloid: How to read the sensors' value with Matlab?

    ok thanks a lot.

    I'm going to program in C finally in order to use the IR sensors and the gyro, and then I'm going to try to export the data in a Excel file and then access it with Matlab to do graphics.

    When I run the program to read the IR sensor in C, this line
    Code:
    while( !(ADCSRA & (1 << ADIF)) ); // Wait until AD-Conversion complete
    never (well I've wait a couple of minutes) stops. Do you know why?

    Thanks

    Edit: The value always stays zero.
    Last edited by cybernetics; 07-28-2011 at 09:16 AM.
    Mathieu, student in physical engineering at Laval University

  9. Question Re: On Bioloid: How to read the sensors' value with Matlab?

    Now it works for port 1, but it gives be value between 0 and 50 instead of 500 (as with RoboPlus Manager), but it's still good.
    Last edited by cybernetics; 07-29-2011 at 02:47 PM.
    Mathieu, student in physical engineering at Laval University

  10. Re: On Bioloid: How to read the sensors' value with Matlab?

    However, when I put more than one sensor, let's say two IR sensors, the data vary between 0 and 10 and are really not precised. Do you know why? Here's a part of my code:

    Code:
    while(1)
    {
        iteration+=1;
    
        DDRA |= 0x80;
    
        PORTA &= ~0x80; // ADC Port 1 IR ON
    
        ADCSRA = (1 << ADEN) | (1 << ADPS2) | (1 << ADPS1); // ADC Enable, Clock 1/64div.
    
        ADMUX =  1; //ADC_PORT_1; // ADC Port 1 Select
    
        _delay_ms(12); // Short Delay for rising sensor signal
        ADCSRA |= (1 << ADIF); // AD-Conversion Interrupt Flag Clear
        ADCSRA |= (1 << ADSC); // AD-Conversion Start
    
        while( !(ADCSRA & (1 << ADIF)) ); // Wait until AD-Conversion complete
     
        PORTA = 0xFC; // IR-LED Off
    
        printf( "%d\r\n", ADC); // Print Value on USART
        _delay_ms(50);
        
        if(iteration>4500)
            break;
        IR_data[1][iteration]=ADC;
    
    
    
        DDRA |= 0x80;
    
        PORTA &= ~0x80; // ADC Port 1 IR ON
    
        ADCSRA = (1 << ADEN) | (1 << ADPS2) | (1 << ADPS1); // ADC Enable, Clock 1/64div.
    
        ADMUX =  2; //ADC_PORT_2; // ADC Port 2 Select
    
        _delay_ms(12); // Short Delay for rising sensor signal
        ADCSRA |= (1 << ADIF); // AD-Conversion Interrupt Flag Clear
        ADCSRA |= (1 << ADSC); // AD-Conversion Start
    
        while( !(ADCSRA & (1 << ADIF)) ); // Wait until AD-Conversion complete
     
        PORTA = 0xFC; // IR-LED Off
    
        printf( "%d\r\n", ADC); // Print Value on USART
        _delay_ms(50);
    
        if(iteration>4500)
            break;
        IR_data[2][iteration]=ADC;
    Last edited by cybernetics; 07-29-2011 at 02:49 PM.
    Mathieu, student in physical engineering at Laval University

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Bioloid The Ultimate Bioloid Thread
    By tom_chang79 in forum Humanoids, Walkers & Crawlers
    Replies: 436
    Last Post: 09-22-2011, 12:27 PM
  2. Sensors! Where can I find sensors!
    By pengyou in forum Arbotix, Microcontrollers, Arduino
    Replies: 7
    Last Post: 06-12-2011, 07:58 PM
  3. Discussion Thinking about Bioloid..
    By LinuxGuy in forum Robotics General Discussion
    Replies: 5
    Last Post: 05-28-2010, 06:31 AM
  4. Question(s) Writing an educatonal book on robotics
    By darkback2 in forum Robotics General Discussion
    Replies: 107
    Last Post: 05-24-2010, 11:51 PM
  5. News/Announcement Want to win a Bioloid? Announcing the TRC Tutorial Contest!
    By DresnerRobotics in forum Project Showcase
    Replies: 28
    Last Post: 01-30-2010, 12:49 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •