我需要对1 us水平进行精确定时,以计算pwm波的占空比变化.
背景
我正在使用Gumstix Over Water COM(https://www.gumstix.com/store/app.php/products/265/),它有一个运行在499.92 BogoMIPS的单核ARM Cortex-A8处理器(Gumstix页面声称根据/ proc/cpuinfo,推荐使用800Mhz的1Ghz.操作系统是基于内核版本2.6.34的Linux的Angstrom Image版本,它在Gumstix Water COM上有库存.
问题
我已经对Linux中的精确计时做了相当多的阅读(并且已经尝试了大部分计划)并且共识似乎是使用clock_gettime()并引用CLOCK_MONOTONIC是最好的方法.(我本来希望使用RDTSC寄存器进行定时,因为我有一个具有最小省电能力的内核,但这不是Intel处理器.)所以这里是奇数部分,而clock_getres()返回1,表明分辨率为1 ns实际的时序测试表明,最小分辨率为30517ns或(它不能重合)恰好是32.768KHz时钟周期之间的时间.这就是我的意思:
// Stackoverflow example
#include <stdio.h>
#include <time.h>
#define SEC2NANOSEC 1000000000
int main( int argc, const char* argv[] )
{
// //////////////// Min resolution test //////////////////////
struct timespec resStart, resEnd, ts;
ts.tv_sec = 0; // s
ts.tv_nsec = 1; // ns
int iters = 100;
double resTime,sum = 0;
int i;
for (i = 0; i<iters; i++)
{
clock_gettime(CLOCK_MONOTONIC, &resStart); // start timer …Run Code Online (Sandbox Code Playgroud) 所以我使用python来调用共享C++库中的方法.我有一个问题是将numpy 2D数组转换为C++ 2D short函数作为函数输入.我有一个创建了一个展示问题的玩具示例.随意编译并试用!
这是python代码(soexample.py):
# Python imports
from ctypes import CDLL
import numpy as np
# Open shared CPP library:
cpplib=CDLL('./libsoexample.so')
cppobj = cpplib.CPPClass_py()
# Stuck on converting to short**?
array = np.array([[1,2,3],[1,2,3]])
cpplib.func_py(cppobj,array)
Run Code Online (Sandbox Code Playgroud)
这是C++库(soexample.cpp):
#include <iostream>
using namespace std;
class CPPClass
{
public:
CPPClass(){}
void func(unsigned short **array)
{
cout << array[0][0] << endl;
}
};
// For use with python:
extern "C" {
CPPClass* CPPClass_py(){ return new CPPClass(); }
void func_py(CPPClass* myClass, unsigned short **array)
{
myClass->func(array); …Run Code Online (Sandbox Code Playgroud) 所以我使用python来调用共享C++库中的方法.我有一个问题,从C++返回到Python的双重问题.我有一个创建了一个展示问题的玩具示例.随意编译并试用.
这是python代码(soexample.py):
# Python imports
from ctypes import CDLL
import numpy as np
# Open shared CPP library:
cpplib=CDLL('./libsoexample.so')
cppobj = cpplib.CPPClass_py()
# Stuck on converting to short**?
x = cpplib.func_py(cppobj)
print 'x =', x
Run Code Online (Sandbox Code Playgroud)
这是C++(soexample.cpp):
#include <iostream>
using namespace std;
class CPPClass
{
public:
CPPClass(){}
void func(double& x)
{
x = 1.0;
}
};
// For use with python:
extern "C" {
CPPClass* CPPClass_py(){ return new CPPClass(); }
double func_py(CPPClass* myClass)
{
double x;
myClass->func(x);
return x;
}
} …Run Code Online (Sandbox Code Playgroud)