TI SensorTag 2 CC2650 Servis Calculations (IR temperature - MPU9250) - swift

How can I calculate IR temperature in CC2650.
TI Temperature : F000AA00-0451-4000-B000-000000000000
Temperature Data: f000aa01-0451-4000-b000-000000000000
I try to calculate object and ambient based on data in Temperature Data characteristic. Object data is higher then IR temperature showed in TI application.
Swift Code:
static func calculateObjectAndAmbient(objectRaw:Int16, ambientRaw:Int16) -> (Double, Double)
{
let ambient = Double(ambientRaw)/128.0;
let vObj2 = Double(objectRaw)*0.00000015625;
let tDie2 = ambient + 273.15;
let s0 = 6.4*pow(10,-14);
let a1 = 1.75*pow(10,-3);
let a2 = -1.678*pow(10,-5);
let b0 = -2.94*pow(10,-5);
let b1 = -5.7*pow(10,-7);
let b2 = 4.63*pow(10,-9);
let c2 = 13.4;
let tRef = 298.15;
let s = s0*(1+a1*(tDie2 - tRef)+a2*pow((tDie2 - tRef),2));
let vOs = b0 + b1*(tDie2 - tRef) + b2*pow((tDie2 - tRef),2);
let fObj = (vObj2 - vOs) + c2*pow((vObj2 - vOs),2);
let object = pow(pow(tDie2,4) + (fObj/s),0.25) - 273.15;
return (object, ambient)
}
I also want to calculate MPU9250Service data.
Service = "F000AA80-0451-4000-B000-000000000000"
Characteristic Data = "F000AA81-0451-4000-B000-000000000000"
Characteristic Config = "F000AA82-0451-4000-B000-000000000000"
Is there a manual ? I would like to access Gyro., Accel., Magn., data.
Sorry about my english.
Thank you in advance.

Calculation of Object and Ambient is changed in CC2650.
So new swift calculations in here if anyone needed;
static func calculateObjectAndAmbient(objectRaw:Int16, ambientRaw:Int16) -> (Double, Double)
{
let SCALE_LSB = 0.03125;
let a = objectRaw >> 2;
let Obj = Double(a) * SCALE_LSB
let b = ambientRaw >> 2;
let Amb = Double(b) * SCALE_LSB
return (Obj, Amb)
}
More Details : sensortag2015
More Details : TI Wiki

Related

How to separate integral part and fractional part in Swift? An how to transform fractional part into Int

I separated fractional part, but now I need to make that part as Int.
For example:
0.5 -> 50.63 -> 63
let a1 = 2.5
let a2 = 7.63
let a3 = 8.81
let a4 = 99.0
let a1Frac = a1.truncatingRemainder(dividingBy: 1)
let a2Frac = a2.truncatingRemainder(dividingBy: 1)
let a3Frac = a3.truncatingRemainder(dividingBy: 1)
let a4Frac = a4.truncatingRemainder(dividingBy: 1)
First you should use Swift Decimal type if you would like to preserve your fractional digits precision. Second to get its fraction value you can check this post. Once you have your decimal fractions all you need is to get their significand value:
extension Decimal {
func rounded(_ roundingMode: NSDecimalNumber.RoundingMode = .bankers) -> Decimal {
var result = Decimal()
var number = self
NSDecimalRound(&result, &number, 0, roundingMode)
return result
}
var whole: Decimal { self < 0 ? rounded(.up) : rounded(.down) }
var fraction: Decimal { self - whole }
}
let a1 = Decimal(string: "2.5")!
let a2 = Decimal(string: "7.63")!
let a3 = Decimal(string: "8.81")!
let a4 = Decimal(string: "99")!
let a1Frac = a1.fraction // 0.5
let a2Frac = a2.fraction // 0.63
let a3Frac = a3.fraction // 0.81
let a4Frac = a4.fraction // 0
let a1significand = a1Frac.significand // 5
let a2significand = a2Frac.significand // 63
let a3significand = a3Frac.significand // 81
let a4significand = a4Frac.significand // 0
If you need to convert your Decimal values to Int you can simply cast it to NSDecimalNumber and get its intValue or add those extensions to your project to add the same NSDecimalNumber functionality to Swift Decimal type:
extension Decimal {
var decimalNumber: NSDecimalNumber { self as NSDecimalNumber}
var intValue: Int { decimalNumber.intValue }
}
a1significand.intValue // 5
a2significand.intValue // 63
a3significand.intValue // 81
a4significand.intValue // 0

Swift Charts Candle chart does not correctly showing

I am using th swift Charts library. I'm trying to show a candle chart but it does not correctly showing data.
After running it will be show just one item but if i will zoom in it will be show all items,
how immediately showing every item after running without needed to zoom ?
func setupCandleChart() {
// candleChartView.autoScaleMinMaxEnabled = true // if enable this doesn't show anything
setCandleDataCount(5, range: 30)
}
func setCandleDataCount(_ count: Int, range: UInt32) {
let yVals1 = (0..<count).map { (i) -> CandleChartDataEntry in
let mult = range + 1
let val = Double(arc4random_uniform(40) + mult)
let high = Double(arc4random_uniform(9) + 8)
let low = Double(arc4random_uniform(9) + 8)
let open = Double(arc4random_uniform(6) + 1)
let close = Double(arc4random_uniform(6) + 1)
let even = i % 2 == 0
return CandleChartDataEntry(x: Double(i), shadowH: val + high, shadowL: val - low, open: even ? val + open : val - open, close: even ? val - close : val + close)
}
let set1 = CandleChartDataSet(entries: yVals1, label: "Data Set")
let data = CandleChartData(dataSet: set1)
candleChartView.data = data
}
if i will zoom:

How can I test an input condition after translating the source code using Coco/R?

I have a function that I want to test in Swift. It has been generated using Coco/R. I have an input statement which I want to test if it provides the desired output using the generated code (Parser.swift).
I haven't tried anything out yet since I don't know where to start.
func Addition {
var x = input.a
var y = input.b
let z: Int?
z = x + y
return z
}
Expected Result:
Input File: a = 10
b = 5
Output: 15
Open XCode , creat new PlayGround:
Then Try this:
import Foundation
struct InputFormat {
var a : Int
var b : Int
}
func addition(input: InputFormat) -> Int {
let x = input.a
let y = input.b
let z = x + y
return z
}
let input = InputFormat(a: 10, b: 5)
print(addition(input: input))
It was the nearest way to get your code into test.
Hope it helps.

How to obtain refereced method (destination) namespace

I am using NDepend 2018.1.1. and I need to obtain the namespace of the target of the following NDepend Query
//
// <Name>Relevant Methods</Name>
//
let e9 = "Sys.Core.App.CommonService"
let e10 = "Sys.Core.App.CommonService3GProvider"
let e17 = "Sys.Core.App.UnderwritingService3GProvider"
let e18 = "Sys.Core.App.UniquePerson.Actions"
let e19 = "Sys.Core.App.UniquePerson"
let e1 = "Sys.Co.Application.UnderwritingService"
let e2 = "Sys.Co.Application.UnderwritingService3GProvider"
let e3 = "Sys.Core.App.Accounting.Services"
let e4 = "Sys.Core.App.Accounting.Services.Provider"
let e5 = "Sys.Core.App.BillingService"
let e6 = "Sys.Core.App.BillingService3GProvider"
let e7 = "Sys.Core.App.ClaimsService"
let e8 = "Sys.Core.App.ClaimsService3GProvider"
let e11 = "Sys.Core.App.EventsService"
let e12 = "Sys.Core.App.EventsService3GProvider"
let e13 = "Sys.Core.App.IntegrationService"
let e14 = "Sys.Core.App.IntegrationService3GProvider"
let e15 = "Sys.Core.App.SecurityServices"
let e16 = "Sys.Core.App.UnderwritingService"
/**/
let ensamblados = from m in
Assemblies.WithName(e1).Concat(
Assemblies.WithName(e2)).Concat(
Assemblies.WithName(e3)).Concat(
Assemblies.WithName(e4)).Concat(
Assemblies.WithName(e5)).Concat(
Assemblies.WithName(e6)).Concat(
Assemblies.WithName(e9)).Concat(
Assemblies.WithName(e10)).Concat(
Assemblies.WithName(e11)).Concat(
Assemblies.WithName(e12)).Concat(
Assemblies.WithName(e13)).Concat(
Assemblies.WithName(e14)).Concat(
Assemblies.WithName(e15)).Concat(
Assemblies.WithName(e16)).Concat(
Assemblies.WithName(e17)).Concat(
Assemblies.WithName(e18)).Concat(
Assemblies.WithName(e19))
select m
//Obtain the types and members of the assemblies
from dest in ensamblados
let targets = dest.ChildTypesAndMembers.ToHashSetEx()
let methodsUser = Application.Methods.UsingAny(targets)
let typesUser = Application.Types.UsingAny(targets)
//Search all the sources that call any of the targets Type or Members
//Concatenar los metodos y los tipos de orig
let results = from orig in methodsUser //.Concat((IEnumerable<IMember>)typesUser)
// HERE need to obtain the namespace of the target of the following
select new {
orig
,t=orig.FullName
,NumRefAlOrig = orig.IsMethod ? orig.AsMethod.MethodsCalled.Intersect(targets).Concat(orig.AsMethod.FieldsUsed.Intersect(targets)).Count() :
orig.AsType.TypesUsed.Intersect(targets).Count()
,OrigenEsMetodo= orig.IsMethod
,OrigenTipo= orig.IsMethod ? "Metodo":"Tipo"
}
/*Metodos*/
from rr in results
where rr.OrigenEsMetodo
select rr
I have tried different methods from the NDepend framework but this is my first NDepende Query and I feel that I am missing something very obvious
First you can simplify by writing
let ensamblados = from m in
Assemblies.WithNameIn(
"Sys.Co.Application.UnderwritingService",
"Sys.Co.Application.UnderwritingService3GProvider",...)
or
let ensamblados = from m in
Assemblies.WithNameLike("Sys.Co.Application.")
.Concat( Assemblies.WithNameLike("Sys.Core.App.")
We'll complete this answer when the question will be clearer

How can I plot data from a Swift sandbox?

I am practicing with Swift 3.x and I need to plot some data. The problem is that I only really have IBM's online Swift sandbox to work with. The purpose of the plotting is to understand how single-precision code is affected by summations:
I wrote some code to do this, but now I have no clue how to plot this. I doubt Swift can somehow bring up a window for plotting, let alone do so when run through the online sandbox.
Side note: I might be able to VNC into a Mac computer at my university to use Xcode. If I paste the same code into an Xcode project, could it make plots?
Here is the code in case you wanted to see it. I need to now run this code for N=1 to N=1,000,000.
import Foundation
func sum1(N: Int) -> Float {
var sum1_sum: Float = 0.0
var n_double: Double = 0.0
for n in 1...(2*N) {
n_double = Double(n)
sum1_sum += Float(pow(-1.0,n_double)*(n_double/(n_double+1.0)))
}
return sum1_sum
}
func sum2(N: Int) -> Float {
var sum2_sum: Float = 0.0
var n_double: Double = 0.0
var sum2_firstsum: Float = 0.0
var sum2_secondsum: Float = 0.0
for n in 1...N {
n_double = Double(n)
sum2_firstsum += Float((2.0*n_double - 1)/(2.0*n_double))
sum2_secondsum += Float((2.0*n_double)/(2.0*n_double + 1))
}
sum2_sum = sum2_secondsum - sum2_firstsum //This is where the subtractive cancellation occurs
return sum2_sum
}
func sum3(N: Int) -> Float {
var sum3_sum: Float = 0.0
var n_double: Double = 0.0
for n in 1...N {
n_double = Double(n)
sum3_sum += Float(1/(2.0*n_double*(2.0*n_double + 1)))
}
return sum3_sum
}
print("Sum 1:", sum1(N: 1000000))
print("Sum 2:", sum2(N: 1000000))
print("Sum 3:", sum3(N: 1000000))
Yes, #TheSoundDefense is right. There is no plotting output from the Swift Sandbox directly. However, I recommend that you still use the Swift Sandbox. Just run the code, and copy and paste the output in comma-delimited format to Excel or MATLAB to plot it. I did some tweaking to your sum2 as an example, while also making it a bit more functional in the process:
func sum2(N: Int) -> Float {
let a: Float = (1...N).reduce(0) {
let nDouble = Double($1)
return Float((2.0 * nDouble - 1) / (2.0 * nDouble)) + $0
}
let b: Float = (1...N).reduce(0) {
let nDouble = Double($1)
return Float((2.0 * nDouble) / (2.0 * nDouble + 1)) + $0
}
return b - a
}
let N = 10
let out = (1...N).map(){ sum2(N: $0)}
let output = out.reduce(""){$0 + "\($1), "}
print(output)
0.166667, 0.216667, 0.240476, 0.254365, 0.263456, 0.269867, 0.274629, 0.278306, 0.28123, 0.283611,