Society of Robots - Robot Forum

Software => Software => Topic started by: ruben301088 on November 24, 2011, 05:35:46 AM

Title: how to store hardware components as variables
Post by: ruben301088 on November 24, 2011, 05:35:46 AM
Hi, I've looked through the 50$ tutorial, namely the sourcecode for the robot. I'm currently at the University studying Computer Science, and have taken a slight interesst in robotics.
So, my questions is this: I'd like to know how your sourcecode knows that the servo_left variable is the left servo on the hardware. Or backwards, how does the chip know, what variable stores what command for what piece of hardware?
Is there a binary coded accesspoint to all points on the chip, or how does it work?

I would appreciate all you can tell me  :)
My goal is to write and construct myself, without using the programs listed in the tutorial. That is, compiling my own C code and transferring the .hex to the chip through the computers console  :D
Title: Re: how to store hardware components as variables
Post by: newInRobotics on November 24, 2011, 07:09:54 AM
So, my questions is this: I'd like to know how your sourcecode knows that the servo_left variable is the left servo on the hardware. Or backwards, how does the chip know, what variable stores what command for what piece of hardware?
It does not, but You do, hence you manually assign variable name to the pin or certain register of the pin (or other required part of the controller, eg. timer) to be precise. When value of the variable changes in runtime, that certain register simply uses value stored in it and does all the calculations with new value. Is that what You were asking for?
Title: Re: how to store hardware components as variables
Post by: newInRobotics on November 24, 2011, 07:14:47 AM
I've just realised what You were asking for. At the top of Your code You create macros that basically change the name say from PORTB1 to LEFT_SERVO. Since You declare that You want to give pin the other name, You can access that pin with its new name. This is used to make code more readable, as LEFT_SERVO is easier to understand than PORTB1.

Code: [Select]
#define LEFT_SERVO  PORTB1
Title: Re: how to store hardware components as variables
Post by: ruben301088 on November 26, 2011, 11:32:55 AM
That makes sense.

At the top of Your code You create macros that basically change the name say from PORTB1 to LEFT_SERVO. Since You declare that You want to give pin the other name, You can access that pin with its new name. This is used to make code more readable, as LEFT_SERVO is easier to understand than PORTB1.

Code: [Select]
#define LEFT_SERVO  PORTB1

However, how do i know that PORTB1 refers to a pin? or how does C know? is it stored in some library? or on the chip?
And thanks for your answer... It cleared up part of my confusion :P
Title: Re: how to store hardware components as variables
Post by: joe61 on November 26, 2011, 11:42:34 AM
The macros are spelled out in header files. There's a header file for each chip type. Generally there's a port definition and a set of pin definitions for each port. You look at the data sheet and see that pin x does what you want, you connect device y to it, and use the macros to set the pin on or off, read a voltage from it, etc.

A port macro resolves to the address of the port in memory. The pin macros break out to the pin numbers within a port. So to set pin PB3 in PORTB you'd do

Code: [Select]
PORTB |= (1 << PB3);
If you download the avr-gcc toolchain you can examine the header files. If you compare those with the information in the data sheet for whatever chip you're using, you'll see the correspondence.

I'm thinking in terms of Atmel chips, I presume that PIC and other architectures work much the same way.

Joe
Title: Re: how to store hardware components as variables
Post by: ruben301088 on November 26, 2011, 12:26:04 PM
That was very helpful. Thank you.

I also checked out the open source Arduino project. They pretty much cut it out for me :)
Also, people who are new to robotics might wanna check out Arduino. They offer simple boards already soldered together, with easy to use free open source software. Worth having a look at ;) I'm considering getting one of their boards