版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
1、關(guān)于Touch Panel校準(zhǔn)算法的研究Haiyong Lei1. 概述由于存在機(jī)械誤差和ADC誤差, touch panel獲取的點(diǎn)坐標(biāo)與屏幕上的點(diǎn)坐標(biāo)總是存在偏差, 所以touch panel需要通過一定的軟件校準(zhǔn).2. 校準(zhǔn)算法目前普遍采用的是5點(diǎn)校準(zhǔn), 即取屏幕四角各一個點(diǎn)加上中心一個點(diǎn), 作為校準(zhǔn)的基準(zhǔn)參考, 如圖1.圖1: 校準(zhǔn)點(diǎn)的位子五點(diǎn)的坐標(biāo)定義為:左下: x0y0 (屏幕坐標(biāo)), X0Y0 (TP坐標(biāo))左上: x0y1 (屏幕坐標(biāo)), X0Y1 (TP坐標(biāo))右下: x1y0 (屏幕坐標(biāo)), X1Y0 (TP坐標(biāo))右上: x1y1 (屏幕坐標(biāo)), X1Y1 (TP坐標(biāo))中間:
2、xcyc (屏幕坐標(biāo)), XcYc (TP坐標(biāo))2.1 算法1算法1基于如下公式把TP坐標(biāo)轉(zhuǎn)換成屏幕坐標(biāo):x_screen = Kx * (X_tp - X_offset) / SCF + x_offsety_screen = Ky * (Y_tp - Y_offset) / SCF + y_offset其中Kx,y是X/Y軸比例因子:Kx = (x1 - x0) / (X1 - X0) * SCFKy = (y1 - y0) / (Y1 - Y0) * SCFX_offset用于消除偏離誤差:x_offset = xcy_offset = ycX_offset = XcY_offset =
3、YcSCF是SCALE FACTOR這里用于整數(shù)計算時保持計算精度。五個基準(zhǔn)點(diǎn)包含的冗余信息, 可用于排除人為誤差, 假定一個合理誤差sigma,則以下公式近式成立:|X0 - X0| sigma, |X1 - X1| sigma|Y0 - Y0| sigma, |Y1 - Y1| sigma|Xc - (X1 + X0 + X1 + X0) / 4| sigma|Yc - (Y1 + Y0 + Y1 + Y0) / 4 | sigma校準(zhǔn)完成后,Kx,y和X/Y_offset四個參數(shù)予以永久保存。算法1的優(yōu)點(diǎn)是比較簡單,缺點(diǎn)是無法消除TP屏傾斜誤差。對于精度要求不高場合是個合適的選擇。2.2
4、 算法2算法2是tslib中采用的校準(zhǔn)算法, tslib是開源TP算法庫, 廣泛使用在各種touch panel應(yīng)用場合,特別是Android平臺。tslib包含多種算法, 校準(zhǔn)是其中之一, 具體公式如下:x_screen = (A0 * X_tp + A1 * Y_tp + A2) / A6y_screen = (A3 * X_tp + A4 * Y_tp + A5) / A6系數(shù)A0到A5經(jīng)計算得出, A6是SCALE FACTOR.算法2的優(yōu)點(diǎn)是效果最好,缺點(diǎn)是計算復(fù)雜,特別A0到A5的計算需要用到浮點(diǎn),如果處理器不支持浮點(diǎn)運(yùn)算的話,需要轉(zhuǎn)換為定點(diǎn)運(yùn)算。3 實(shí)現(xiàn)3.1 算法1/* * C
5、opyright (C) 2015. * * Author: Haiyong Lei * */#ifndef TS_CALIB_H#define TS_CALIB_H/ DEBUG#define TS_DEBUG#ifdef TS_DEBUG#include #define TS_LOG printf#else#define TS_LOG#endif#define N_CALIB_POINTS5 / number points for calibration/ NAME OF POINTS#define LEFT_BOTTOM0#define LEFT_TOP1#define RIGHT_BO
6、TTOM2#define RIGHT_TOP3#define CENTER4/ FLIP FLAGS#define FLIP_X1#define FLIP_Y2/ ERRORS#define E_SUCCESS0#define E_TOO_MANY_POINTS-1#define E_BAD_POINT_X0-2#define E_BAD_POINT_X1-3#define E_BAD_POINT_Y0-4#define E_BAD_POINT_Y1-5#define E_BAD_POINT_XCENTER-6#define E_BAD_POINT_YCENTER-7struct ts_cal
7、ib_data int scr_xN_CALIB_POINTS;/ screen calibration pointsint scr_yN_CALIB_POINTS;int scr_xres;/ screen resolutionint scr_yres;int scr_xcenter;/ screen centerint scr_ycenter;int tp_xN_CALIB_POINTS;/ touch panel calibration pointsint tp_yN_CALIB_POINTS;int tp_xres;/ touch panel resolutionint tp_yres
8、;int tp_xcenter;/ touch panel centerint tp_ycenter;int tp_xyflip;/ touch panel axis xy flip flagsint Kx;/ axis x ratioint Ky;/ axis y ratioint precision;/ calibration precisionint scaling;/ scaling factor;/* initialize touch panel calibration data: Params: scr_xres: screen x resolution scr_yres: scr
9、een i resolution tp_xres: tp x resolution tp_yres: tp y resolution Return:*/void ts_init(struct ts_calib_data *calib, int scr_xres, int scr_yres,int tp_xres, int tp_yres);/* set touch panel xy direction flip flags: Params: tp_xyflip: set the flag to flip Return:*/void ts_set_xyflip(struct ts_calib_d
10、ata *calib, int tp_xyflip);/* reset touch panel xy direction flip flags: Params: tp_xyflip: set the flag to no flip Return:*/void ts_reset_xyflip(struct ts_calib_data *calib, int tp_xyflip);/* set calibration precision: Params: precision: the minimum in distance of two touch panel readingsthey can b
11、e considered the same. The unit is the touchpanel reading unit. Return:*/void ts_set_precision(struct ts_calib_data *calib, int precision);/* set calibration scaling: Params: scaling: the scaling factor, the default is 8192. Return:*/void ts_set_scaling(struct ts_calib_data *calib, int scaling);/* s
12、et calibration data: Params: Kx: the ratio of axis x. Ky: the ratio of axis y. tp_xcenter: touch panel center points axis x. tp_ycenter: touch panel center points axis y. Return:*/void ts_set_calib_data(struct ts_calib_data *calib, int Kx, int Ky,int tp_xcenter, int tp_ycenter);/* smooth N of touch
13、panel continous readings: Params: x_in: N x reading of touch panel. y_in: N y reading of touch panel. n: the number of N. x_out: smoothed x reading output. y_out: smoothed y reading output. Return:*/void ts_smooth_xy(int *x_in, int *y_in, int n, int* x_out, int* y_out);/* convert touch panel coordin
14、ate to screen using the calibration data: Params: tp_x: x reading of touch panel. tp_y: y reading of touch panel. scr_x: screen x position output. scr_y: screen y position output. Return:*/void ts_linear_map_xy(struct ts_calib_data *calib, int tp_x, int tp_y, int *scr_x, int *scr_y);/* add calibrati
15、on point Params: n: the Nth point used by calibration, N 5. scr_x: x position in screen coordinate. scr_y: y position in screen coordinate. tp_x: x reading of touch panel. tp_y: y reading of touch panel. Return: E_SUCCESS if succeed.*/int ts_add_calib_point(struct ts_calib_data *calib, int n, int sc
16、r_x, int scr_y, int tp_x, int tp_y);/* process calibration: Params: Return: E_SUCCESS if succeed. Note: use ts_add_calib_point to feed 5 points before calibration.*/int ts_calibrate(struct ts_calib_data *calib);#endif/* * Copyright (C) 2015. * * Author: Haiyong Lei * * The touchscreen calibration li
17、bray. * */#include #include ts_calib.h#define PRECISION40/ related to touch panel resolution, / example: if touch panel resolution is 4096,/ the precision is 40/4096.#define SCALING8192void ts_init(struct ts_calib_data *calib, int scr_xres, int scr_yres,int tp_xres, int tp_yres)TS_LOG(ts_calib init
18、with resultion: screen: W%dx,H%d, tp: W%dx,H%d,scr_xres, scr_yres, tp_xres, tp_yres);memset(calib, 0, sizeof (struct ts_calib_data);calib-scr_xres = scr_xres;calib-scr_yres = scr_yres;calib-scr_xcenter = scr_xres / 2;calib-scr_ycenter = scr_yres / 2;calib-tp_xres = tp_xres;calib-tp_yres = tp_yres;ca
19、lib-tp_xcenter = tp_xres / 2;calib-tp_ycenter = tp_yres / 2;calib-Kx = scr_xres * SCALING / tp_xres;calib-Ky = scr_yres * SCALING / tp_yres;calib-precision = PRECISION;calib-scaling = SCALING;void ts_set_xyflip(struct ts_calib_data *calib, int tp_xyflip)TS_LOG(ts_calib set xyflip: %d,tp_xyflip);cali
20、b-tp_xyflip |= tp_xyflip;void ts_reset_xyflip(struct ts_calib_data *calib, int tp_xyflip)TS_LOG(ts_calib reset xyflip: %d,tp_xyflip);calib-tp_xyflip &= tp_xyflip;void ts_set_precision(struct ts_calib_data *calib, int precision)TS_LOG(ts_calib set precision: %d,precision);calib-precision = precision;
21、void ts_set_scaling(struct ts_calib_data *calib, int scaling)TS_LOG(ts_calib set scaling: %d,scaling);calib-scaling = scaling;void ts_set_calib_data(struct ts_calib_data *calib, int Kx, int Ky,int tp_xcenter, int tp_ycenter)TS_LOG(ts_calib set Kx/Ky: %d,%d, center: X%d,Y%d,Kx, Ky, tp_xcenter, tp_yce
22、nter);calib-Kx = Kx;calib-Ky = Ky;calib-tp_xcenter = tp_xcenter;calib-tp_ycenter = tp_ycenter;/ smooth N points by averagingvoid ts_smooth_xy(int *x_in, int *y_in, int n, int* x_out, int* y_out)int x_sum = 0;int y_sum = 0;int i;/ average n samplesfor (i = 0; i tp_xyflip & FLIP_X)tp_x = calib-tp_xres
23、 - tp_x;if (calib-tp_xyflip & FLIP_Y)tp_y = calib-tp_yres - tp_y;if (*scr_x)int x = (calib-Kx * (tp_x - calib-tp_xcenter) / calib-scaling+ calib-scr_xcenter;/ boundary checkif (x calib-scr_xres)x = calib-scr_xres;*scr_x = x;if (*scr_y)int y = (calib-Ky * (tp_y - calib-tp_ycenter) / calib-scaling+ ca
24、lib-scr_ycenter;/ boundary checkif (y calib-scr_yres)y = calib-scr_yres;*scr_y = y;int ts_add_calib_point(struct ts_calib_data *calib, int n, int scr_x, int scr_y, int tp_x, int tp_y)if (n = N_CALIB_POINTS)TS_LOG(ts_calib error: %dth point is out of range.,n);return E_TOO_MANY_POINTS;if (calib-tp_xy
25、flip & FLIP_X)tp_x = calib-tp_xres - tp_x;if (calib-tp_xyflip & FLIP_Y)tp_y = calib-tp_yres - tp_y;calib-scr_xn = scr_x;calib-scr_yn = scr_y;calib-tp_xn = tp_x;calib-tp_yn = tp_y;TS_LOG(ts_calib add %dth point: screen: X%d,Y%d, tp: X%d,Y%d,n, scr_x, scr_y, tp_x, tp_y);return E_SUCCESS;/ variance(a0,
26、 a1, ., an) =/ 1/n * (a0 - a)2 + (a1 - a)2 + . (an - a)2,/ where, a = (a0 + a1 + . an) / n/static int variance(int p0, int p1)int pt = (p0 + p1) /2;int v = (p0 - pt) * (p0 - pt) +(p1 - pt) * (p1 - pt);return v /2;/* * the calibration procedure requires to collect five points: * * * (x0,y1 )-(x1,y1)
27、* | | * | | * | | * | | * | | * | (x2,y2) | * | | * | | * | | * | | * | | * (x0 ,y0 )-(x1 ,y0) * * To linear convert touch panel coordinate to screen, we * use formula: * * x = Kx * (X - X2) / SCALING + x2 * y = Ky * (Y - Y2) / SCALING + x2 * * The ratio Kx,y can be computed by: * * Kx = (x1 - x0) /
28、 (X1 - X0) * SCALING * Ky = (y1 - y0) / (Y1 - Y0) * SCALING * * x, y: screen coordinates, X, Y: touch panel coordinates. * The SCALING factor is to keep calculation precision. * * The equation needs two points to solve, but we have five.The * redundant points can be used to valid points themselves,
29、* points are considered enough good if: * * variance(X0, X0) precision2 * variance(X1, X1) precision2 * variance(Y0, Y0) precision2 * variance(Y1, Y1) precision2 * * and for the center point: * * X2 = (X1 + X0) + (X1 + X0) / 4 * Y2 = (Y1 + Y0) + (Y1 + Y0) / 4 * variance(X2, X2) precision2 * variance
30、(Y2, Y2) precision * calib-precision;x00 = calib-tp_xLEFT_BOTTOM;x01 = calib-tp_xLEFT_TOP;x10 = calib-tp_xRIGHT_BOTTOM;x11 = calib-tp_xRIGHT_TOP;y00 = calib-tp_yLEFT_BOTTOM;y01 = calib-tp_yRIGHT_BOTTOM;y10 = calib-tp_yLEFT_TOP;y11 = calib-tp_yRIGHT_TOP;if (v = variance(x00, x01) square_prec)TS_LOG(t
31、s_calib error: bad point x0: %d,%d, variance: %d%d.,x00, x01, v, square_prec);return E_BAD_POINT_X0;if (v = variance(x10, x11) square_prec)TS_LOG(ts_calib error: bad point x1: %d,%d, variance: %d%d.,x10, x11, v, square_prec);return E_BAD_POINT_X1;if (v = variance(y00, y01) square_prec)TS_LOG(ts_cali
32、b error: bad point y0: %d,%d, variance: %d%d.,y00, y01, v, square_prec);return E_BAD_POINT_Y0;if (v = variance(y10, x11) square_prec)TS_LOG(ts_calib error: bad point y1: %d,%d, variance: %d%d.,y10, y11, v, square_prec);return E_BAD_POINT_Y1;x20 = calib-tp_xCENTER;y20 = calib-tp_yCENTER;x21 = (x10 +
33、x11 + x00 + x01) / 4;y21 = (y10 + y11 + y00 + y01) / 4;if (v = variance(x20, x21) square_prec)TS_LOG(ts_calib error: bad point x2: %d,%d, variance: %d%d.,x20, x21, v, square_prec);return E_BAD_POINT_XCENTER;if (v = variance(y20, y21) square_prec)TS_LOG(ts_calib error: bad point y2: %d,%d, variance: %d%d.,y20, y21, v, square_prec);return E_BAD_POINT_YCENTER;scr_xdist = calib-scr_xRIGHT_BOTTOM -
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 棒球鞋項目評價分析報告
- 提花紙板相關(guān)項目實(shí)施方案
- 珠寶首飾零售合同
- 倉儲物流解決方案
- 2024版裝載機(jī)租賃合同范本
- 2024版五金家電鍋爐買賣合同范本
- 2024版運(yùn)輸合同 保管合同 倉儲合同
- 舉一反三系列高二高考數(shù)學(xué)同步及復(fù)習(xí)資料人教A版選擇性必修2專題4.12 數(shù)學(xué)歸納法(重難點(diǎn)題型檢測)(含答案及解析)
- 店鋪買賣合同
- 2024年小學(xué)五年級班主任計劃范文(三篇)
- 產(chǎn)說會邀約技巧話術(shù)
- 順豐物流員工手冊
- 山西普通高中學(xué)生學(xué)籍登記表完整優(yōu)秀版
- 陜西_介紹ppt
- 危大工程安全管理專項培訓(xùn)
- 咯血的臨床診療指南
- 工資證明范本
- chinsc變頻器說明書
- 不明原因肺炎個案調(diào)查表
- 《自動化生產(chǎn)線安裝與調(diào)試》單元設(shè)計.docx
- 高血壓患者自我管理小組活動記錄
評論
0/150
提交評論