假设iPhone放在一张平台上.我想确定桌面的角度,其中0的角度意味着桌子与重力矢量完全垂直.我使用以下公式:
radians = atanf (z / sqrt (x^2 + y^2)
Run Code Online (Sandbox Code Playgroud)
在.h
double accelerationXAverage;
double accelerationYAverage;
double accelerationZAverage;
double accelerationXSum;
double accelerationYSum;
double accelerationZSum;
int readingsCount;
Run Code Online (Sandbox Code Playgroud)
在他们中
#define kThresholdMovementHigh 0.05
- (void)accelerometer:(UIAccelerometer *)accelerometer
didAccelerate:(UIAcceleration *)acceleration
{
// did device move a lot? if so, reset sums
if (fabsf(acceleration.x - accelerationXAverage) > kThresholdMovementHigh ||
fabsf(acceleration.y - accelerationYAverage) > kThresholdMovementHigh ||
fabsf(acceleration.z - accelerationZAverage) > kThresholdMovementHigh )
{
NSLog(@"deviceDidMove a lot");
accelerationXSum = acceleration.x;
accelerationYSum = acceleration.y;
accelerationZSum = acceleration.z;
readingsCount = 1; …Run Code Online (Sandbox Code Playgroud) 使用Alexa 中的新实体解析,嵌套字典变得非常嵌套。引用深度嵌套值的最pythonic 方式是什么?我如何编写每行不超过 79 个字符的代码?
这是我目前拥有的,虽然它有效,但我很确定有更好的方法:
if 'VolumeQuantity' in intent['slots']:
if 'resolutions' in intent['slots']['VolumeQuantity']:
half_decibels = intent['slots']['VolumeQuantity']['resolutions']['resolutionsPerAuthority'][0]['values'][0]['value']['name'].strip()
elif 'value' in intent['slots']['VolumeQuantity']:
half_decibels = intent['slots']['VolumeQuantity']['value'].strip()
Run Code Online (Sandbox Code Playgroud)
这是来自 alexa 的 json 的部分示例
{
"type": "IntentRequest",
"requestId": "amzn1.echo-api.request.9a...11",
"timestamp": "2018-03-28T20:37:21Z",
"locale": "en-US",
"intent": {
"name": "RelativeVolumeIntent",
"confirmationStatus": "NONE",
"slots": {
"VolumeQuantity": {
"name": "VolumeQuantity",
"confirmationStatus": "NONE"
},
"VolumeDirection": {
"name": "VolumeDirection",
"value": "softer",
"resolutions": {
"resolutionsPerAuthority": [
{
"authority": "amzn1.er-authority.echo-blah-blah-blah",
"status": {
"code": "ER_SUCCESS_MATCH"
},
"values": [
{
"value": {
"name": "down", …Run Code Online (Sandbox Code Playgroud)