WebbotLib AVR library
WebbotLib It just does it
  C++ documentation  C documentation

core.h

This file is always included by your project.
It defines some standard macros and data types and also provides the main entry point for your program.

 

Function

 


int16_t interpolate(int16_t value, int16_t minVal, int16_t maxVal, int16_t minRtn, int16_t maxRtn)

This will interpolate a value from one range of values into its equivalent in another range of signed numbers by using the following parameters:-
value - The value from range 1
minVal,MaxVal - specify the start and end of range 1
minRtn,maxRtn - specify the start and end of range 2
The function will convert 'value' into its equivalent in range 2.
Mathematically it will return:
minRtn+((value-minVal)*(maxRtn-minRtn)/(maxVal-minVal))
Example: assume that you have a value in a variable called 'theValue' that stores a value in the range 0 to 2048 and you want to convert this into the range -512 to +512 and store the new value into a variable called 'newValue'. Then you could write:-
int16_t newValue = interpolate(theValue, 0, 2048, -512, 512);
Here are some examples of what would be returned:-
Note that no range checking is performed. So if theValue contained twice the input limit then it will return twice the output limit. ie if theValue=4096 then the returned value will be 1024.
If you want to limit the values to make sure they are within a given range then use the CLAMP command in libdefs.h. For example:-
uint16_t newValue = interpolate(theValue, 0, 2048, -512, 512);
newValue = CLAMP(newValue,-512,512); // limit the answer to be in the range -512 to 512

uint16_t interpolateU(int16_t value, int16_t minVal, int16_t maxVal, uint16_t minRtn, uint16_t maxRtn)

This will interpolate a value from one range of values into its equivalent in another range of unsigned numbers by using the following parameters:-
value - The value from range 1
minVal,MaxVal - specify the start and end of range 1
minRtn,maxRtn - specify the start and end of range 2
The function will convert 'value' into its equivalent in range 2.
Mathematically it will return:
minRtn+((value-minVal)*(maxRtn-minRtn)/(maxVal-minVal))
Example: assume that you have a value in a variable called 'theValue' that stores a value in the range 0 to 2048 and you want to convert this into the range 300 to 800 and store the new value into a variable called 'newValue'. Then you could write:-
uint16_t newValue = interpolateU(theValue, 0, 2048, 300, 800);
Note that no range checking is performed. So if theValue contained twice the input limit then it will return twice the output limit. ie if theValue=4096 then the returned value will be 1600.
If you want to limit the values to make sure they are within a given range then use the CLAMP command in libdefs.h. For example:-
uint16_t newValue = interpolateU(theValue, 0, 2048, 300, 800);
newValue = CLAMP(newValue,300,800); // limit the answer to be in the range 300 to 800

double interpolatef(double value, double minVal, double maxVal, double minRtn, double maxRtn)

This will interpolate a floating point value from one range of values into its equivalent in another range of signed numbers by using the following parameters:-
value - The value from range 1
minVal,MaxVal - specify the start and end of range 1
minRtn,maxRtn - specify the start and end of range 2
The function will convert 'value' into its equivalent in range 2.
Mathematically it will return:
minRtn+((value-minVal)*(maxRtn-minRtn)/(maxVal-minVal))
Example: assume that you have a value in a variable called 'theValue' that stores a value in the range 0 to 20.48 and you want to convert this into the range -5.12 to +5.12 and store the new value into a variable called 'newValue'. Then you could write:-
double newValue = interpolatef(theValue, 0, 20.48, -5.12, 5.12);
Here are some examples of what would be returned:-
Note that no range checking is performed. So if theValue contained twice the input limit then it will return twice the output limit. ie if theValue=40.96 then the returned value will be 10.24.
If you want to limit the values to make sure they are within a given range then use the CLAMP command in libdefs.h. For example:-
double newValue = interpolatef(theValue, 0, 20.48, -5.12, 5.12);
newValue = CLAMP(newValue,-5.12,5.12); // limit the answer to be in the range -5.12 to 5.12

uint32_t isqrt(uint32_t x)

Performs a fast square root function on a whole number - returning the nearest whole number answer and without requiring the floating point library.

delay_cycles(uint32_t __cycles)

Pause for the given number of clock cycles.
Note that the actual delay will vary depending upon the speed of the processor. In order to remove this dependency it is recommended that you use either the MS_TO_CYCLES or US_TO_CYCLES macro to convert an actual time into the number of clock cycles. For example: to delay by 100uS you should write:
delay_cycles(US_TO_CYCLES(100));
It is recommended that you use this function, rather than delay_ms or delay_us, if the delay is a fixed value and especially if the delay period is short. The reason being that the delay_ms and delay_us routines will first have to do some multiplications and divisions in order to convert the delay into cycles and the overhead of these calculations can sometimes take longer than the delay you require. Using the MS_TO_CYCLES and US_TO_CYCLES macros for a fixed delay means that the compiler will perform these calculations at compile time and thereby reduce the overhead at runtime.

delay_ms(uint32_t __ms)

Pause for the given number of ms.
This is similar to clockWaitms but will also work in app_init where the clock is not available.

delay_us(uint32_t __us)

Pause for the given number of µs.
This is similar to clockWaitus but will also work in app_init where the clock is not available.

uint64_t MS_TO_CYCLES(ms)

Convert milliseconds into a number of cycles.
Because the cpu speed is only known when building an end-user program then this macro is not available if you are writing a new library function.
Also see: delay_ms, delay_cycles

uint64_t US_TO_CYCLES(us)

Convert microseconds into a number of cycles.
Because the cpu speed is only known when building an end-user program then this macro is not available if you are writing a new library function.
Also see: delay_us, delay_cycles

PRINTF(stream,format, args...)

Output a formatted string to any Stream.
The new 'streams' functionality actually provides much more friendly ways to output data ( such as print ) where you don't need to understand the link required between the format string and the data type you are showing. However: they have certain functional limits - eg they wont allow you to print a number right-aligned within a given field width. Hence this function is provided as a low-level alternative for those familiar with C format strings and also for those people who are migrating their code away from the old WebbotLib 'rprintf' commands.
When using this function the formatting string is automatically stored in program memory.
Note that due to the way C works then neither the compiler, nor the runtime, can verify that the % entries in the format string match the data type of the given parameters. Consequently: if you attempt to print out various variables in one go and there is a mismatch between the format string and the parameters that follow then C will print out what looks like garbage. If in doubt, or to debug a problem, then only output one value per call.

memoryDump(FILE* stream,const void* data, size_t offset, size_t len)

Dumps an area of memory in tabular format showing both hexadecimal and ASCII values.
The first parameter specifies where you want the output dumping to.
The second parameter specifies the memory address, the third is the byte offset starting at that address, and the last parameter specifies the number of bytes to dump out.
So if you have an array such as:
char buffer[50];
Then you can dump out its contents to the standard output using:-
memoryDump(stdout, buffer, 0, 50);

Valid XHTML 1.0 Transitional