write character buffer in Bluetooth Low Energy - ios

I am working on a Bluetooth Low Energy supported application.
I will be communicating with a hardware device that supports a characterstic. Lets assume that the characteristic is defined as "TestCharacteristic".
Based on the scenario, I want to write different data to the same characteristic.
In my iOS app, i will have two buttons.
I want to send a character buffer of 20 byte on clicking the first button and different character buffer of 20 byte on clicking the second button.
OnButton 1 Click:
BluetoothGattCharacteristic charac = Service
.getCharacteristic("TestCharacteristic");
if (charac == null) {
Log.e(TAG, "char not found!");
return false;
}
byte[20] value = {0xEF, 1, 1, 0, 0, 0, 0xEF, 0, 0, 0, 0, 0, 0xEF, 1, 2, 0, 0, 0, 0xEF, 0, 0, 0, 0, 0, 0xEF, 0, 0, 0, 0, 0 };
charac.setValue(value);
boolean status = mBluetoothGatt.writeCharacteristic(charac);
OnButton 2 Click:
BluetoothGattCharacteristic charac = Service
.getCharacteristic("TestCharacteristic");
if (charac == null) {
Log.e(TAG, "char not found!");
return false;
}
byte[20] value = {0xDC, 0, 0, 0 ,0 , 0xDC, 0, 0, 0 ,0, 0xDC, 0, 0, 0 ,0, 0xDC, 0, 0, 0 ,0};
charac.setValue(value);
boolean status = mBluetoothGatt.writeCharacteristic(chara
c);
Is above operation possible?
Can I send different values for the same characteristic?
Thanks & Regards,
Phil

Related

Intel IPP Erode Access Violation Exception

I made a simple script using the ErodeBorder function in IPP, and I want to use ippiErode_1u_C1R_L. I am having trouble using ippiErode_1u_C1R_L I keep getting an AccessViolation Exception. First script shows a functioning code, second script shows an attempt to use ippiErode_1u_C1R_L.
Working Code:
int width = 1600;
int height = 594;
int binSize = 118800;
int binStep = ceil(width / 8);
IppiSize roi = { width, height };
Ipp8u* workBin = (Ipp8u*)ippsMalloc_8u(binSize);
Ipp8u* defectMask = (Ipp8u*)ippsMalloc_8u(binSize);
Ipp8u* origBin = GetMask(); //Same size as workBin
Ipp8u mask[9] = { 1, 1, 1,
1, 0, 1,
1, 1, 1 };
IppiSize maskSize = { 3, 3 };
int pSpecSize = 0, pBufferSize = 0;
ippiMorphologyBorderGetSize_1u_C1R(roi, maskSize, &pSpecSize, &pBufferSize);
Ipp8u* pBuffer = (Ipp8u*)ippsMalloc_8u(pBufferSize);
IppiMorphState* pSpec = (IppiMorphState*)ippsMalloc_8u(pSpecSize);
ippiMorphologyBorderInit_1u_C1R(roi, mask, maskSize, pSpec, pBuffer);
ippiErodeBorder_1u_C1R(origBin, binStep, 0, workBin, binStep, 0, roi, ippBorderRepl, 0, pSpec, pBuffer);
ippiErodeBorder_1u_C1R(workBin, binStep, 0, defectMask, binStep, 0, roi, ippBorderRepl, 0, pSpec, pBuffer);
ippiErodeBorder_1u_C1R(defectMask, binStep, 0, workBin, binStep, 0, roi, ippBorderRepl, 0, pSpec, pBuffer);
ippiErodeBorder_1u_C1R(workBin, binStep, 0, defectMask, binStep, 0, roi, ippBorderRepl, 0, pSpec, pBuffer);
Throws Exception when calling ippiErode_1u_C1R_L():
int width = 1600;
int height = 594;
int binSize = 118800;
int binStep = ceil(width / 8);
IppiSizeL roi_L = { width, height };
Ipp8u* workBin = (Ipp8u*)ippsMalloc_8u(binSize);
Ipp8u* defectMask = (Ipp8u*)ippsMalloc_8u(binSize);
Ipp8u* origBin = GetMask(); //Same size as workBin
Ipp8u mask[9] = { 1, 1, 1,
1, 0, 1,
1, 1, 1 };
IppiSizeL maskSize = { 3, 3 };
IppSizeL pSpecSize = 0, pBufferSize = 0;
ippiErodeGetBufferSize_L(roi_L, maskSize, ipp1u, 1, &pBufferSize);
ippiErodeGetSpecSize_L(roi_L, maskSize, &pSpecSize);
Ipp8u* pBuffer = (Ipp8u*)ippsMalloc_8u_L(pBufferSize);
IppiMorphStateL* pSpec = (IppiMorphStateL*)ippsMalloc_8u_L(pSpecSize);
IppStatus initSizeStat = ippiErodeInit_L(roi_L, mask, maskSize, pSpec);
ippiErode_1u_C1R_L(origBin, binStep, 0, workBin, binStep, 0, roi_L, ippBorderRepl, 0, pSpec, pBuffer);

Understanding spacer/z3 unsat proof

We use spacer as a backend CHC solver and want to get proof when the result is unsat.
I think that the proof is sufficient to construct a counterexample(a graph reachable to a bad state) but spacer's output is difficult to understand for me. There are some unfamiliar words like asserted or hyper-res.
I couldn't find any document to understand that output. If there is any, please tell me.
I'm really happy if I can construct a counterexample similar to Eldarica's one[0].
For example,
0: FALSE -> 1
1: main_h_30(0, 0, 0, 0, 0, 1, 0, 0, -1, 0, 0, 1) -> 2
2: main_h_28(0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1) -> 3
3: main_h_24(0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1) -> 4
4: main_h_22(0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1) -> 5
5: main_h_21(0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1) -> 6
6: main_h_18(0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1) -> 7
7: main_h_16(0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1) -> 8
8: main_h_13(0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1) -> 9
9: main_h_11(0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1) -> 10
10: main_h_9(0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1) -> 11
11: main_h_8(0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1) -> 12
12: main_h_7(0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1) -> 13
13: main_h_6(0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1) -> 14
14: main_h_5(0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1) -> 15
15: main_h_4(0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1) -> 16
16: main_h_3(0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1) -> 17
17: main_h_2(0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1) -> 18
18: main_h_1(0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1)
[0] http://logicrunch.it.uu.se:4096/~wv/eldarica/?ex=Prolog%2Flistcounter.unsat

OpenCV, how can we normalize a Mat min to max and max to min?

I want to normalize a Mat to the min value goes to 255 and max goes to 0 (normalize the Mat between 0~255).
For example, if we have an array like [0.02, 0.002, 0.0002] after normalization I want to get a result like this: [3, 26, 255], but now when I am using NORM_MINMAX I got [255, 26, 3].
But I did not find any function to do the inverted operation of the NORM_MINMAX.
Code used:
cv::Mat mat(10, 10, CV_64F);
mat.setTo(0);
mat.row(0) = 0.02;
mat.row(1) = 0.002;
mat.row(2) = 0.0002;
cv::normalize(mat, mat, 255, 0, cv::NORM_MINMAX);
mat.convertTo(mat, CV_8UC1);
std::cout << mat << std::endl;
Result is:
[255, 255, 255, 255, 255, 255, 255, 255, 255, 255;
26, 26, 26, 26, 26, 26, 26, 26, 26, 26;
3, 3, 3, 3, 3, 3, 3, 3, 3, 3;
0, 0, 0, 0, 0, 0, 0, 0, 0, 0;
0, 0, 0, 0, 0, 0, 0, 0, 0, 0;
0, 0, 0, 0, 0, 0, 0, 0, 0, 0;
0, 0, 0, 0, 0, 0, 0, 0, 0, 0;
0, 0, 0, 0, 0, 0, 0, 0, 0, 0;
0, 0, 0, 0, 0, 0, 0, 0, 0, 0;
0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
But I want the inverse of the above result.
Update: When I subtract 255 from the mat like:
cv::subtract(255, mat, mat, mat); // the last mat acts as mask
std::cout << mat << std::endl;
Result is:
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0;
229, 229, 229, 229, 229, 229, 229, 229, 229, 229;
252, 252, 252, 252, 252, 252, 252, 252, 252, 252;
0, 0, 0, 0, 0, 0, 0, 0, 0, 0;
0, 0, 0, 0, 0, 0, 0, 0, 0, 0;
0, 0, 0, 0, 0, 0, 0, 0, 0, 0;
0, 0, 0, 0, 0, 0, 0, 0, 0, 0;
0, 0, 0, 0, 0, 0, 0, 0, 0, 0;
0, 0, 0, 0, 0, 0, 0, 0, 0, 0;
0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
I finally found the way how to calculate, below are the steps:
By using inverse proportions formula, we can easily calculate the inverse of the NORM_MINMAX
x = a*b/c
Where the a= min value of the mat element, b=255 (max value), and c= the element which we want to calculate it.
cv::Mat mat(10, 10, CV_64F);
mat.setTo(0);
mat.row(0) = 0.02;
mat.row(1) = 0.002;
mat.row(2) = 0.0002;
std::cout << mat<< std::endl;
// craete a mask
cv::Mat mask(mat.size(), CV_8U);
mask.setTo(0);
mask.row(0) = 255;
mask.row(1) = 255;
mask.row(2) = 255;
// find the min value
double min;
cv::minMaxLoc(mat, &min, nullptr, nullptr, nullptr, mask);
std::cout << "min=" << min << std::endl;
// unfortunately opencv divide operation does not support mask, so we need some extra steps to perform.
cv::Mat result, maskNeg;
cv::divide(min*255, mat, result); // this is the magic line
cv::bitwise_not(mask, maskNeg);
mat.copyTo(result, maskNeg);
std::cout << result << std::endl;
// convert to 8bit
result .convertTo(result , CV_8UC1);
std::cout << "the final result:" << std::endl;
std::cout << temp << std::endl;
And the outputs:
original mat
[0.02, 0.02, 0.02, 0.02, 0.02, 0.02, 0.02, 0.02, 0.02, 0.02;
0.002, 0.002, 0.002, 0.002, 0.002, 0.002, 0.002, 0.002, 0.002, 0.002;
0.0002, 0.0002, 0.0002, 0.0002, 0.0002, 0.0002, 0.0002, 0.0002, 0.0002, 0.0002;
0, 0, 0, 0, 0, 0, 0, 0, 0, 0;
0, 0, 0, 0, 0, 0, 0, 0, 0, 0;
0, 0, 0, 0, 0, 0, 0, 0, 0, 0;
0, 0, 0, 0, 0, 0, 0, 0, 0, 0;
0, 0, 0, 0, 0, 0, 0, 0, 0, 0;
0, 0, 0, 0, 0, 0, 0, 0, 0, 0;
0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
min=0.0002
the calculated min-max
[2.55, 2.55, 2.55, 2.55, 2.55, 2.55, 2.55, 2.55, 2.55, 2.55;
25.5, 25.5, 25.5, 25.5, 25.5, 25.5, 25.5, 25.5, 25.5, 25.5;
255, 255, 255, 255, 255, 255, 255, 255, 255, 255;
0, 0, 0, 0, 0, 0, 0, 0, 0, 0;
0, 0, 0, 0, 0, 0, 0, 0, 0, 0;
0, 0, 0, 0, 0, 0, 0, 0, 0, 0;
0, 0, 0, 0, 0, 0, 0, 0, 0, 0;
0, 0, 0, 0, 0, 0, 0, 0, 0, 0;
0, 0, 0, 0, 0, 0, 0, 0, 0, 0;
0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
the final result:
[ 3, 3, 3, 3, 3, 3, 3, 3, 3, 3;
26, 26, 26, 26, 26, 26, 26, 26, 26, 26;
255, 255, 255, 255, 255, 255, 255, 255, 255, 255;
0, 0, 0, 0, 0, 0, 0, 0, 0, 0;
0, 0, 0, 0, 0, 0, 0, 0, 0, 0;
0, 0, 0, 0, 0, 0, 0, 0, 0, 0;
0, 0, 0, 0, 0, 0, 0, 0, 0, 0;
0, 0, 0, 0, 0, 0, 0, 0, 0, 0;
0, 0, 0, 0, 0, 0, 0, 0, 0, 0;
0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
Yes, It is what I want.

How to understand the math behind the CTM(current transformation matrix)?

When I written some test code about modifying CTM, I found it can't be explain with The Math Behind the Matrices in Quartz 2D Programming Guide. The test code is as follow:
// {a, b, c, d, tx, ty}
NSLog(#"UIKit CTM:%#\n", NSStringFromCGAffineTransform(CGContextGetCTM(ctx)));
CGContextSaveGState(ctx);
CGContextTranslateCTM(ctx, 0, CGRectGetHeight(rect));// 1
NSLog(#"Quartz part 1 CTM:%#\n", NSStringFromCGAffineTransform(CGContextGetCTM(ctx)));
CGContextScaleCTM(ctx, 1, -1);// 2
NSLog(#"Quartz CTM:%#\n", NSStringFromCGAffineTransform(CGContextGetCTM(ctx)));
CGContextTranslateCTM(ctx, 0, CGRectGetHeight(rect)); // 3
NSLog(#"UIKit part 1 CTM:%#\n", NSStringFromCGAffineTransform(CGContextGetCTM(ctx)));
CGContextScaleCTM(ctx, 1, -1);// 4
NSLog(#"UIKit part 2 CTM:%#\n", NSStringFromCGAffineTransform(CGContextGetCTM(ctx)));
CGContextRestoreGState(ctx);
The output:
2017-09-29 09:51:27.166 QuartzDemo[53287:31120880] UIKit CTM:[2, 0, 0, -2, 0, 1136]
2017-09-29 09:51:27.167 QuartzDemo[53287:31120880] Quartz part 1 CTM:[2, 0, 0, -2, 0, 0]
2017-09-29 09:51:27.167 QuartzDemo[53287:31120880] Quartz CTM:[2, 0, -0, 2, 0, 0]
2017-09-29 09:51:27.167 QuartzDemo[53287:31120880] UIKit part 1 CTM:[2, 0, -0, 2, 0, 1136]
2017-09-29 09:51:27.167 QuartzDemo[53287:31120880] UIKit part 2 CTM:[2, 0, 0, -2, 0, 1136]
First, let's focus on UIKit CTM transforms to Quartz CTM, we use array express matrix, on line 1:
[2, 0, 0, 0, -2, 0, 0, 1136, 1] x [1, 0, 0, 0, 1, 0, tx1, ty1, 1] = [2, 0, 0, 0, -2, 0, 0, 0, 1]
then
[1, 0, 0, 0, 1, 0, tx1, ty1, 1] = [1, 0, 0, 0, 1, 0, -1136, 1]
so in this case CGContextTranslateCTM(ctx, 0, CGRectGetHeight(rect)); equal to [1, 0, 0, 0, 1, 0, -1136, 1], Question 1: Where is minus sign come from?
On line 2:
[2, 0, 0, 0, -2, 0, 0, 0, 1] x [sx1, 0, 0, 0, sy1, 0, 0, 0, 1] = [2, 0, 0, -0, 2, 0, 0, 0, 1]
then
[sx1, 0, 0, 0, sy1, 0, 0, 0, 1] = [1, 0, 0, 0, -1, 0, 0, 0, 1]
so CGContextScaleCTM(ctx, 1, -1); equal to [1, 0, 0, 0, -1, 0, 0, 0, 1], this result is the same as theory value.
On line 3:
[2, 0, 0, -0, 2, 0, 0, 0, 1] x [1, 0, 0, 0, 1, 0, tx2, ty2, 1] = [2, 0, 0, -0, 2, 0, 0, 1136, 1]
then
[1, 0, 0, 0, 1, 0, tx2, ty2, 1] = [2, 0, 0, 0, 2, 0, 0, 1136, 1]
this result also the same as theory value.
On line 4:
[2, 0, 0, 0, 2, 0, 0, 1136, 1] x [sx2, 0, 0, 0, sy2, 0, 0, 0, 1] = [2*sx2, 0, 0, 0, 2*sy2, 0, 0, 1136*sy2, 1]
then
[2*sx2, 0, 0, 0, 2*sy2, 0, 0, 1136*sy2, 1] = [2, 0, 0, 0, -2, 0, 0, 1136, 1]
2*sx2 = 2 => sx2 = 1; but
2*sy2 = -2 (1)
1136*sy2 = 1136 (2)
In equation (1) sy2 = -1, but in equation (2) sy2 = 1, So Question 2: Why this happened? How to explain this case?

Kalman filter 3D implementation

I want to implement the kalman filter for a moving object in r3 (X,Y,Z-coordinate) in OpenCV.
I tried to understand the OpenCV documentation but this is really not helpful and very rare.
The syntax for the initialization is:
KalmanFilter::KalmanFilter ( int dynamParams, int measureParams, int
controlParams = 0, int type = CV_32F )
In my case, is dynamParams = 9 and measureParams=3?
And what is the transitionMatrix in my case?
In that case the Transition Matrix A looks like:
A = [1, 0, 0, v, 0, 0, a, 0, 0;
0, 1, 0, 0, v, 0, 0, a, 0;
0, 0, 1, 0, 0, v, 0, 0, a;
0, 0, 0, 1, 0, 0, v, 0, 0;
0, 0, 0, 0, 1, 0, 0, v, 0;
0, 0, 0, 0, 0, 1, 0, 0, v;
0, 0, 0, 0, 0, 0, 1, 0, 0;
0, 0, 0, 0, 0, 0, 0, 1, 0;
0, 0, 0, 0, 0, 0, 0, 0, 1]
With
v = dt
a = 0.5*dt^2
See http://campar.in.tum.de/Chair/KalmanFilter
I found out, that for the 3D-Case often the position, velocity and acceleration is used. That means, that for the OpenCV implementation dynamParams=9 and measureParams=3 is right.

Resources