Author Topic: Newbie questions on Webbotlib  (Read 1553 times)

0 Members and 1 Guest are viewing this topic.

Offline constfangTopic starter

  • Beginner
  • *
  • Posts: 5
  • Helpful? 0
Newbie questions on Webbotlib
« on: September 09, 2010, 06:39:23 AM »
Well, I think it's kind of stupid to open a whole new thread just to ask a very simple and stupid question on the Webbotlib so let's make this a place to ask newbie question (Maybe Webbot can use this thread to update the FAQ section on his Webbot's manual)
Ok, here's the main part, my question:
In my traditional WinAVR program (without Webbot), I usually create several variables inside the main() function (but outside the while loop) I thought that the function "appInitSoftware" in Webbotlib was supposed to have the same function but turns out that it's not (obviously) so what I've been doing when I wanted to create a new variable is to put it outside of every function like this:

Code: [Select]
#include "hardware.h"

volatile boolean previouslyReleasedButton1=1;
volatile boolean previouslyReleasedButton2=1;
DUTY_CYCLE dutyRight = 85;
DUTY_CYCLE dutyLeft = 100;
int i;

// Initialise the hardware
void appInitHardware(void) {
    initHardware();
}

// Initialise the software
TICK_COUNT appInitSoftware(TICK_COUNT loopStart){
    pin_low(leftMotorEnable);  // Turn on left motor
    pin_low(rightMotorEnable); // Turn on right motor
    return 0;
}

// This is the main loop
TICK_COUNT appControl(LOOP_COUNT loopCount, TICK_COUNT loopStart) {
.....

I know it's the wrong way to do it but, then, what is the correct way?

Offline Webbot

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 2,165
  • Helpful? 111
    • Webbot stuff
Re: Newbie questions on Webbotlib
« Reply #1 on: September 09, 2010, 11:18:15 AM »
What you've done is fine and will actually produce smaller and faster code because the global variables can be accessed directly; whereas having them in your old 'main' but outside of the loop means that they are stored on the stack and are referenced relative to the current stack pointer. This added indirection needs more code and is therefore slower.

However: I notice that you've also defined an 'int i;' as a global variable. Assuming this is something like a for loop counter then these are better as local variables (ie defined in the function that uses them) ie:

Code: [Select]
TICK_COUNT appControl(LOOP_COUNT loopCount, TICK_COUNT loopStart) {
   int i;
   ...
   for( i=0; i<10;i++){
       ...
   }
   ...
}
The reason being that the value of 'i' is only relevant inside the loop.

But this is just a standard C programming thing - not a WebbotLib thing.

The appInitSoftware routine in WebbotLib is only ever called once and is used to initialise any such values to an initial value. For example: a photovore may read the current light readings and store them into a global variable as an 'ambient' light level. The appControl function (which is called repeatedly) can then compare the latest light levels against the ambient level.

Webbot Home: http://webbot.org.uk/
WebbotLib online docs: http://webbot.org.uk/WebbotLibDocs
If your in the neighbourhood: http://www.hovinghamspa.co.uk

Offline constfangTopic starter

  • Beginner
  • *
  • Posts: 5
  • Helpful? 0
Re: Newbie questions on Webbotlib
« Reply #2 on: September 09, 2010, 11:52:26 PM »
Thank you Webbot for enlightening me, the reason I tried to put the index 'i' outside of the "while" loop is because somehow I thought that declaring a variable will take some time to process, and instead of declaring a variable again and again in every loop, it would be faster if I declare it just once, outside of the appControl. Am I completely wrong?

Offline Webbot

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 2,165
  • Helpful? 111
    • Webbot stuff
Re: Newbie questions on Webbotlib
« Reply #3 on: September 10, 2010, 03:33:17 PM »
Thank you Webbot for enlightening me, the reason I tried to put the index 'i' outside of the "while" loop is because somehow I thought that declaring a variable will take some time to process, and instead of declaring a variable again and again in every loop, it would be faster if I declare it just once, outside of the appControl. Am I completely wrong?

The C compiler will look at all local variables (ie inside a function) and will calculate how many bytes are need on the stack. Note that this is done at compile time - not run time. It then adds code to the function to reserve this number of bytes and is very quick. ie if 8 bytes were needed then its: StackPointer -= 8
Note that these local variable should be assumed to have a random value - ie you need to initialise them to a known value.
The benefit of local variables is that once the declaring function exits then the space is recovered and can be used again.

Global variables (ie defined at the top of your program) are automatically initialised to 0 but the amount of space they require is permanently allocated and cannot be reused for other things. But global variables are faster to access.

Webbot Home: http://webbot.org.uk/
WebbotLib online docs: http://webbot.org.uk/WebbotLibDocs
If your in the neighbourhood: http://www.hovinghamspa.co.uk

 


Get Your Ad Here

data_list