libcflie

Firmware/software/electronics/mechanics
Slaxx
Member
Posts: 86
Joined: Tue Mar 03, 2015 11:19 am
Location: Switzerland

Re: libcflie

Post by Slaxx »

Is ist possible that not every package that is actually sent is received by the Crazyflie?

Did anyone test it on Windows yet?
lkdo
Beginner
Posts: 2
Joined: Mon Nov 30, 2015 8:00 am

Re: libcflie

Post by lkdo »

Hi. I've just started with libcflie a few weeks ago, and I made a MinGW compile on Windows with minimal changes. I am now looking into the block logging part, and receving data from the Crazyflie. I cannot get the 1000 Hz frequency though, i get about 10 packages per second. I'm looking into it, and my plan is to also compile with VC++, and see if the package frequency improves.
arnaud
Bitcraze
Posts: 2538
Joined: Tue Feb 06, 2007 12:36 pm

Re: libcflie

Post by arnaud »

Hi,
The log is going to have 100Hz update rate maximum (the time period setting is in 10ms step).
lkdo
Beginner
Posts: 2
Joined: Mon Nov 30, 2015 8:00 am

Re: libcflie

Post by lkdo »

Hi. It seems that, by default, the libcflie library logs at 10Hz. To log at 100Hz, the second parameter of in the call: m_tocLogs->registerLoggingBlock has to be set to 10000, instead of 1000. Alternatively, the formula for d10thOfMS in the registerLoggingBlock() function can be modified, to properly reflect the relationship between frequency and number of 10 ms.

On another note, I have added code for the extraction of the timestamp from the log Block, and I noticed that packages (very) often arrive out of order. This is a timestamp sequence example: "8417 8427 8437 8447 8713 8723 8733 8743 8753 8763 8773 8783 8793 8803 8813 8823 8577 8587 8597 8607 8617 8627 8637 8647 8657 8667" ( 84xx 87xx 88xx 85xx 86xx) . The numbers are miliseconds. It does not seem to be the client code.
MCFurry
Beginner
Posts: 3
Joined: Tue Jan 05, 2016 12:10 pm

Re: libcflie

Post by MCFurry »

For those interested, I succesfully compiled libcflie and the examples in windows with some help of Luminita from this forum. Steps are roughly as follows:

- Install MinGW: Download and make sure the system PATH variable is up to date after installing.
- Install cmake-gui: Download (Installs with GUI automatically)
- Downloadlibcflie source of course: Here
- Extract libcflie source to C:\libcflie-master (Or any other folder, but I'll use C:\ as my root folder)
- Download libusb-1.0: Download
- Extract libusb to C:\libusb-1.0
- Download GLFW (Version <3.0!!): Download
- Extract to C:\glfw-2.7.9

- First compile glfw:
* In a command prompt cd to glfw root folder and do: mingw32-make win32-mingw
This should automatically build the .dll which should now be in: 'C:\glfw-2.7.9\lib\win32\glfw.dll'
- libusb-1.0 comes precompiled already. Its either in 'C:\libusb-1.0\MinGW32\dll' or 'C:\libusb-1.0\MinGW64\dll', choose the one which corresponds to your architecture.

- If you want these libraries to be available system-wide copy both .dll's to 'C:\Windows\System32'. Otherwise make sure they are placed in the same folder as your executables or update the PATH variable.

- Copy the necessary header files:
* copy the folder 'GL' from 'C:\glfw-2.7.9\include' to 'C:\libcflie-master\include'
* copy the folder 'libusb-1.0' from 'C:\libusb-1.0\include' to 'C:\libcflie-master\include'

- Fix some things in the libcflie code:
* In CCrazyflie.cpp (\src\cflie\) on line 226: clock_gettime() does not work on windows. Replace this code:

Code: Select all

struct timespec tsTime;
  clock_gettime(CLOCK_MONOTONIC, &tsTime);
  
  return tsTime.tv_sec + double(tsTime.tv_nsec) / NSEC_PER_SEC;
with this code:

Code: Select all

 struct timeval tsTime;
  win_clockgettime(CLOCK_MONOTONIC, &tsTime);
  
  return tsTime.tv_sec + double(tsTime.tv_usec) / 1000000.0;
* On line 28 add: '#include "time.h"'
* Underneath '#include <cflie/CCrazyflie.h>' add this custom code for the windows gettime function (thanks to Luminita ;) )

Code: Select all

LARGE_INTEGER getFILETIMEoffset()
{
    SYSTEMTIME s;
    FILETIME f;
    LARGE_INTEGER t;

    s.wYear = 1970;
    s.wMonth = 1;
    s.wDay = 1;
    s.wHour = 0;
    s.wMinute = 0;
    s.wSecond = 0;
    s.wMilliseconds = 0;
    SystemTimeToFileTime(&s, &f);
    t.QuadPart = f.dwHighDateTime;
    t.QuadPart <<= 32;
    t.QuadPart |= f.dwLowDateTime;
    return (t);
}

int win_clockgettime(int X, struct timeval *tv)
{
    LARGE_INTEGER           t;
    FILETIME            f;
    double                  microseconds;
    static LARGE_INTEGER    offset;
    static double           frequencyToMicroseconds;
    static int              initialized = 0;
    static BOOL             usePerformanceCounter = 0;

    if (!initialized) {
        LARGE_INTEGER performanceFrequency;
        initialized = 1;
        usePerformanceCounter = QueryPerformanceFrequency(&performanceFrequency);
        if (usePerformanceCounter) {
            QueryPerformanceCounter(&offset);
            frequencyToMicroseconds = (double)performanceFrequency.QuadPart / 1000000.;
        } else {
            offset = getFILETIMEoffset();
            frequencyToMicroseconds = 10.;
        }
    }
    if (usePerformanceCounter) QueryPerformanceCounter(&t);
    else {
        GetSystemTimeAsFileTime(&f);
        t.QuadPart = f.dwHighDateTime;
        t.QuadPart <<= 32;
        t.QuadPart |= f.dwLowDateTime;
    }

    t.QuadPart -= offset.QuadPart;
    microseconds = (double)t.QuadPart / frequencyToMicroseconds;
    t.QuadPart = microseconds;
    tv->tv_sec = t.QuadPart / 1000000;
    tv->tv_usec = t.QuadPart % 1000000;
    return (0);
}

double currentTime() 
{
	struct timeval tvTime;
	win_clockgettime(0, &tvTime);
	return tvTime.tv_sec + double(tvTime.tv_usec) * 1000 / NSEC_PER_SEC;
}

* In replugging.cpp (\src\examples\): remove or comment out lines 62-64:

Code: Select all

struct timespec tmWait;
  tmWait.tv_sec = 0;
  tmWait.tv_nsec = 500000000;
* on line 75 replace

Code: Select all

 nanosleep(&tmWait, NULL);
with:

Code: Select all

sleep(50);
- Fix libraries in the makefile:
* open CMakeLists.txt from 'C:\libcflie-master' in a text editor
* on line 46 replace 'GL GLU' with 'opengl32' to replace the opengl library with the windows one

- Open cmake-gui and setup things:
* in the field 'Where is the source code' set: 'C:\libcflie-master'
* in the field 'Where to build the binaries' set: 'C:\libcflie-master\build' (Create this build folder yourself first!)
* Click 'Configure'
* Choose 'MinGW Makefiles' and 'Use default native compilers'
* Neglect the error
* In the 'GLFW_LIB' field put: 'C:\glfw-2.7.9\lib\win32\glfw.dll'
* Click configure again and neglect the error
* In the 'USB_LIB' field put: 'C:\libusb-1.0\MinGW64\dll\libusb-1.0.dll' (or the MinGW32 folder on a 32 bits system)
* Click configure again and then generate.
* close cmake-gui
- Compile everything:
* open a command prompt and cd to the folder: 'C:\libcflie-master\build'
* do: mingw32-make

That should be it!
The static library is built in: 'C:\libcflie-master\lib' and the example executables are built in: 'C:\libcflie-master\bin'

Hopefully this little tut is useful to someone. It probably is not the neatest way of compiling libcflie on windows, so feel free to comment on this! I succeeded in compiling it on both Win7 64bits and Win10 64bits using MinGW. I didn't try VC++ so if anyone did feel free to add instructions for this.
Post Reply