go_away

Author Topic: Working with clockGetus() method (Webbotlib)  (Read 561 times)

0 Members and 1 Guest are viewing this topic.

Offline GertlexTopic starter

  • Supreme Robot
  • *****
  • Posts: 742
  • Helpful? 23
  • Nuclear Engineer Roboticist
Working with clockGetus() method (Webbotlib)
« on: August 11, 2011, 09:09:43 PM »
I'm trying to use the clockGetus() method to do some timing stuff for walking gaits, but that's irrelevant, I think.

Two problems:
-clockGetus() works in the appControl looping method, but not in other methods I define... Fixed... having a %ld for an int type was throwing off the later %ld's in the rprintf line....
-I'm a bit confused on how I should approach doing math with the clockGetus() method's results.

For clockGetus(), I get nicely increasing values from the rprintf commands tied to my pressing the button on the Axon.  However, when i do the same in the method timeIncrAndMove, the value is all over the place.

My pared down code.  This successfully compiles.  I'm using Webbotlib V1.x... I'll update to 2.x after I get this stuff cleared up :P
Code: [Select]
#define RPRINTF_COMPLEX
#include "hardware.h"
//#define UART1_RX_BUFFER_SIZE 40
//#include <sys/axon2.h>
#include <Gait/GaitDesigner.h>
#include <servos.h>
#include <rprintf.h>
#include "NumaB.h"

int myUniversalSpeed = 500;
int light = 1;
int outvar = 0;
int llama = 1;
int my_t = 100;

ACTUATOR_LIST all[] = {&servo11.actuator,&servo21.actuator,&servo31.actuator,&servo41.actuator,
&servo12.actuator,&servo22.actuator,&servo32.actuator,&servo42.actuator,
&servo13.actuator,&servo23.actuator,&servo33.actuator,&servo43.actuator,
&servo14.actuator,&servo24.actuator,&servo34.actuator,&servo44.actuator};
//GAIT_DESIGNER gait = MAKE_GAIT_DESIGNER(all, UART1, (BAUD_RATE)115200);
GAIT_DESIGNER gait = MAKE_GAIT_DESIGNER(all, UART2, (BAUD_RATE)115200);

// Initialise the hardware
void appInitHardware(void) {
initHardware();
gaitDesignerInit(&gait);
// rprintfInit(&UART2toBluetoothSerialSendByte);
rprintfInit(&uart1SendByte);
}

// Initialise the software
TICK_COUNT appInitSoftware(TICK_COUNT loopStart){
// numafinit();
rprintf("It begins...\n");
return 0;
}
// This is the main loop
TICK_COUNT appControl(LOOP_COUNT loopCount, TICK_COUNT loopStart) {

if(SWITCH_pressed(&button)){
if(light == 1){
//
LED_on(&statusLED);
delay_ms(30);

rprintf("on %ld\n", clockGetus());
light = 0;
}
else{
// LED
LED_off(&statusLED);

delay_ms(30);
my_t = timeIncrAndMove(my_t);
///////////////////////////////////////////////////
delay_ms(30);

rprintf("off %ld and my_t: %d\n", clockGetus(), my_t);
light = 1;

}
delay_ms(500);
}

// To test if it is released then
if(SWITCH_released(&button)){
// released

}
return 0;
}

int timeIncrAndMove(int my_t){
// this_step += 1;
// my_t = this_step/steps * 200 - 100;
my_t = (clockGetus() - gaitStartTime)/cycleTime * 200 - 100;
if(my_t > 100){
my_t -= 200;
gaitStartTime = clockGetus() - ((my_t + 100)/200 * cycleTime);
}
rprintf("Current time: %ld ... %ld ... %ld \n", my_t, cycleTime, clockGetus());
// moveServos(my_t);
return my_t;

}

The header file I'm including
Code: [Select]
#include <math.h>

#ifndef BUILDING_LIBRARY  //magic...
#define BUILDING_LIBRARY
#include <rprintf.h>
#include <timer.h>

#endif

#ifndef NUMAB_LOAD
#define NUMAB_LOAD

int timeIncrAndMove(int my_t);

int this_step = 0;

int steps = 8;

uint32_t gaitStartTime = -100;

uint32_t cycleTime = 10000000;

#endif

Example output to hyperterminal. Note the last number in the lines starting with "Current time" is what I'm getting from clockGetus()
Code: [Select]
on 6496196
Current time: -1769930852 ... 701694104 ... 701694060
off 7126629 and my_t: -100
on 7720810
Current time: -1769930852 ... -640417640 ... -640417666
off 8351546 and my_t: -100
on 8945775
Current time: -1769930852 ... -1976369000 ... -1976369007
off 9576708 and my_t: -100
on 10171033
Current time: -1769996188 ... 1037500568 ... 1037500580
off 10802142 and my_t: 100
on 11396387
Current time: -1769996188 ... -267190120 ... -267190090
off 12027248 and my_t: 100
on 12621504
Current time: -1769996188 ... -1579089768 ... -1579089719
off 13252815 and my_t: 100

No idea what's going on there...

************************
Second question...

What I'm trying to implement for now in timeIncrAndMove is the following looping algorithm... but question is not about the algorithm itself, so you can skip it unless you're interested.
(Basically, I want my_t to always be in the range -100 to 100.)
1) my_t = ( (current_time - loop_start_time) / loop_time_length) * 200 - 100
2) if my_t > 100, subtract 200 from my_t   ***note below
3) loop_start_time = current time - ( (my_t + 100) / 200) * loop_time_length
4) rest of code stuff happens and repeat

***Note that step 2 assumes that the time elapsed between loops is always less than the loop_time_length.

Doing the above requires going from the unsigned int (uint32_t) to a signed int for the -100 to 100 range.  Do I need to do my math in the range of 0 to 200, then cast the value as a regular integer and assign it to a separate variable?  Ultimately I'm not certain how to properly (let alone efficiently) handle math with different variable types.  I might be doing it OK already... can't tell with the clockGetus() function not working :)

Thanks!
« Last Edit: August 12, 2011, 08:09:19 PM by Gertlex »
I

Offline Webbot

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 2,132
  • Helpful? 108
Re: Working with clockGetus() method (Webbotlib)
« Reply #1 on: August 17, 2011, 04:24:27 AM »
Not sure if you got a reply to this or whether you figured it out yourself.

Your 'problem' line is
Code: [Select]

rprintf("Current time: %ld ... %ld ... %ld \n", my_t, cycleTime, clockGetus());

Since 'my_t' is just an 'int' then it should use '%d'. The others are 32 bit ints so are correctly using '%ld' - so it should say:
Code: [Select]

rprintf("Current time: %d ... %ld ... %ld \n", my_t, cycleTime, clockGetus());
Its a common problem with 'C' as the compiler doesn't check that you've got the right '%' entries for the parameters.
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 GertlexTopic starter

  • Supreme Robot
  • *****
  • Posts: 742
  • Helpful? 23
  • Nuclear Engineer Roboticist
Re: Working with clockGetus() method (Webbotlib)
« Reply #2 on: August 17, 2011, 06:49:10 PM »
Thanks for getting back to me on that... I did indeed figure it out.  It then also took me a while to realize that I needed to do the float-enabled rprintf option when I generate code with project designer.

Right now, I'm chugging along with putting code in.
I

 


Get Your Ad Here

data_list