WP7:将加速度计和指南针数据转换为模拟运动 API

2023-12-02

我正在为 Windows Phone 7.1 (Mango) 编写一个小型示例应用程序,并希望使用组合运动 API 来显示设备的运动。我需要编写模拟类,以便在使用不支持设备所有传感器的模拟器时能够测试我的应用程序。

我已经编写了一个简单的模拟类来模拟指南针(它只是模拟旋转设备)和模拟器中实际可用的加速度计。

现在我必须为 Motion API 编写一个新的模拟对象,但我希望我可以使用指南针和加速度计的值来计算用于 Motion 对象的值。不幸的是,我没有找到已经执行此操作的简单转换示例。

有人知道执行此转换的代码示例吗?尽管这很复杂,但如果已经有解决方案,我不想自己做这件事。


由于 Windows Phone 8 已经发布,而且我还没有手机可供测试,所以我再次遇到了同样的问题。

很像答案使用模拟器时WP7 Mock Microsoft.Devices.Sensors.Compass,我已经创建了与 Motion API 具有相同方法的包装类。当实际的 Motion API 支持时,就会使用它。否则,在调试模式下,将返回改变横滚、俯仰和偏航的模拟数据。


运动包装器

    /// <summary>
    /// Provides Windows Phone applications information about the device’s orientation and motion.
    /// </summary>
    public class MotionWrapper //: SensorBase<MotionReading> // No public constructors, nice one.
    {
        private Motion motion;

        public event EventHandler<SensorReadingEventArgs<MockMotionReading>> CurrentValueChanged;

        #region Properties
        /// <summary>
        /// Gets or sets the preferred time between Microsoft.Devices.Sensors.SensorBase<TSensorReading>.CurrentValueChanged events.
        /// </summary>
        public virtual TimeSpan TimeBetweenUpdates
        {
            get
            {
                return motion.TimeBetweenUpdates;
            }
            set
            {
                motion.TimeBetweenUpdates = value;
            }
        }

        /// <summary>
        /// Gets or sets whether the device on which the application is running supports the sensors required by the Microsoft.Devices.Sensors.Motion class.
        /// </summary>
        public static bool IsSupported
        {
            get
            {
#if(DEBUG)
                return true;
#else
                return Motion.IsSupported;
#endif

            }
        }
        #endregion

        #region Constructors
        protected MotionWrapper()
        {
        }

        protected MotionWrapper(Motion motion)
        {
            this.motion = motion;
            this.motion.CurrentValueChanged += motion_CurrentValueChanged;
        }
        #endregion

        /// <summary>
        /// Get an instance of the MotionWrappper that supports the Motion API
        /// </summary>
        /// <returns></returns>
        public static MotionWrapper Instance()
        {
#if(DEBUG)
            if (!Motion.IsSupported)
            {
                return new MockMotionWrapper();
            }
#endif
            return new MotionWrapper(new Motion());
        }

        /// <summary>
        /// The value from the underlying Motion API has changed. Relay it on within a MockMotionReading.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void motion_CurrentValueChanged(object sender, SensorReadingEventArgs<MotionReading> e)
        {
            var f = new SensorReadingEventArgs<MockMotionReading>();
            f.SensorReading = new MockMotionReading(e.SensorReading);
   RaiseValueChangedEvent(sender, f);
        }

        protected void RaiseValueChangedEvent(object sender, SensorReadingEventArgs<MockMotionReading> e)
        {
            if (CurrentValueChanged != null)
            {
                CurrentValueChanged(this, e);
            }
        }

        /// <summary>
        /// Starts acquisition of data from the sensor.
        /// </summary>
        public virtual void Start()
        {
            motion.Start();
        }

        /// <summary>
        /// Stops acquisition of data from the sensor.
        /// </summary>
        public virtual void Stop()
        {
            motion.Stop();
        }
    }

模拟运动包装器

    /// <summary>
    /// Provides Windows Phone applications mock information about the device’s orientation and motion.
    /// </summary>
    public class MockMotionWrapper : MotionWrapper
    {
        /// <summary>
        /// Use a timer to trigger simulated data updates.
        /// </summary>
        private DispatcherTimer timer;

        private MockMotionReading lastCompassReading = new MockMotionReading(true);

        #region Properties
        /// <summary>
        /// Gets or sets the preferred time between Microsoft.Devices.Sensors.SensorBase<TSensorReading>.CurrentValueChanged events.
        /// </summary>
        public override TimeSpan TimeBetweenUpdates
        {
            get
            {
                return timer.Interval;
            }
            set
            {
                timer.Interval = value;
            }
        }
        #endregion

        #region Constructors
        public MockMotionWrapper()
        {
            timer = new DispatcherTimer();
            timer.Interval = TimeSpan.FromMilliseconds(30);
            timer.Tick += new EventHandler(timer_Tick);
        }
        #endregion

        void timer_Tick(object sender, EventArgs e)
        {
            var reading = new Microsoft.Devices.Sensors.SensorReadingEventArgs<MockMotionReading>();
            lastCompassReading = new MockMotionReading(lastCompassReading);
            reading.SensorReading = lastCompassReading;

            //if (lastCompassReading.HeadingAccuracy > 20)
            //{
            //    RaiseValueChangedEvent(this, new CalibrationEventArgs());
            //}

            RaiseValueChangedEvent(this, reading);
        }

        /// <summary>
        /// Starts acquisition of data from the sensor.
        /// </summary>
        public override void Start()
        {
            timer.Start();
        }

        /// <summary>
        /// Stops acquisition of data from the sensor.
        /// </summary>
        public override void Stop()
        {
            timer.Stop();
        }

    }

模拟动作阅读

//Microsoft.Devices.Sensors.MotionReading
/// <summary>
/// Contains information about the orientation and movement of the device.
/// </summary>
public struct MockMotionReading : Microsoft.Devices.Sensors.ISensorReading
{
    public static bool RequiresCalibration = false;

    #region Properties
    /// <summary>
    /// Gets the attitude (yaw, pitch, and roll) of the device, in radians.
    /// </summary>
    public MockAttitudeReading Attitude { get; internal set; }

    /// <summary>
    ///  Gets the linear acceleration of the device, in gravitational units.
    /// </summary>
    public Vector3 DeviceAcceleration { get; internal set; }

    /// <summary>
    /// Gets the rotational velocity of the device, in radians per second.
    /// </summary>
    public Vector3 DeviceRotationRate { get; internal set; }

    /// <summary>
    /// Gets the gravity vector associated with the Microsoft.Devices.Sensors.MotionReading.
    /// </summary>
    public Vector3 Gravity { get; internal set; }

    /// <summary>
    /// Gets a timestamp indicating the time at which the accelerometer reading was
    ///     taken. This can be used to correlate readings across sensors and provide
    ///     additional input to algorithms that process raw sensor data.
    /// </summary>
    public DateTimeOffset Timestamp { get; internal set; }
    #endregion

    #region Constructors

    /// <summary>
    /// Initialize an instance from an actual MotionReading
    /// </summary>
    /// <param name="cr"></param>
    public MockMotionReading(MotionReading cr)
        : this()
    {
        this.Attitude = new MockAttitudeReading(cr.Attitude);
        this.DeviceAcceleration = cr.DeviceAcceleration;
        this.DeviceRotationRate = cr.DeviceRotationRate;
        this.Gravity = cr.Gravity;
        this.Timestamp = cr.Timestamp;
    }

    /// <summary>
    /// Create an instance initialized with testing data
    /// </summary>
    /// <param name="test"></param>
    public MockMotionReading(bool test) 
        : this()
    {
        float pitch = 0.01f;
        float roll = 0.02f;
        float yaw = 0.03f;

        this.Attitude = new MockAttitudeReading()
        {
            Pitch = pitch,
            Roll = roll,
            Yaw = yaw,
            RotationMatrix = Matrix.CreateFromYawPitchRoll(yaw, pitch, roll),  
            Quaternion = Quaternion.CreateFromYawPitchRoll(yaw, pitch, roll), 

            Timestamp = DateTimeOffset.Now
        };

        // TODO: pull data from the Accelerometer
        this.Gravity = new Vector3(0, 0, 1f);
    }

    /// <summary>
    /// Create a new mock instance based on the previous mock instance
    /// </summary>
    /// <param name="lastCompassReading"></param>
    public MockMotionReading(MockMotionReading lastCompassReading)
        : this()
    {
        // Adjust the pitch, roll, and yaw as required.

        // -90 to 90 deg
        float pitchDegrees = MathHelper.ToDegrees(lastCompassReading.Attitude.Pitch) - 0.5f;
        //pitchDegrees = ((pitchDegrees + 90) % 180) - 90;

        // -90 to 90 deg
        float rollDegrees = MathHelper.ToDegrees(lastCompassReading.Attitude.Roll);
        //rollDegrees = ((rollDegrees + 90) % 180) - 90;

        // 0 to 360 deg
        float yawDegrees = MathHelper.ToDegrees(lastCompassReading.Attitude.Yaw) + 0.5f;
        //yawDegrees = yawDegrees % 360;

        float pitch = MathHelper.ToRadians(pitchDegrees);
        float roll = MathHelper.ToRadians(rollDegrees);
        float yaw = MathHelper.ToRadians(yawDegrees);

        this.Attitude = new MockAttitudeReading()
        {
            Pitch = pitch,
            Roll = roll,
            Yaw = yaw,
            RotationMatrix = Matrix.CreateFromYawPitchRoll(yaw, pitch, roll),
            Quaternion = Quaternion.CreateFromYawPitchRoll(yaw, pitch, roll),

            Timestamp = DateTimeOffset.Now
        };

        this.DeviceAcceleration = lastCompassReading.DeviceAcceleration;
        this.DeviceRotationRate = lastCompassReading.DeviceRotationRate;
        this.Gravity = lastCompassReading.Gravity;
        Timestamp = DateTime.Now;

    }
    #endregion



}

模拟态度阅读

public struct MockAttitudeReading : ISensorReading
{
    public MockAttitudeReading(AttitudeReading attitudeReading) : this()
    {
        Pitch = attitudeReading.Pitch;
        Quaternion = attitudeReading.Quaternion;
        Roll = attitudeReading.Roll;
        RotationMatrix = attitudeReading.RotationMatrix;
        Timestamp = attitudeReading.Timestamp;
        Yaw = attitudeReading.Yaw;
    }

    /// <summary>
    /// Gets the pitch of the attitude reading in radians.
    /// </summary>
    public float Pitch { get; set; }

    /// <summary>
    /// Gets the quaternion representation of the attitude reading.
    /// </summary>
    public Quaternion Quaternion { get; set; }

    /// <summary>
    /// Gets the roll of the attitude reading in radians.
    /// </summary>
    public float Roll { get; set; }

    /// <summary>
    /// Gets the matrix representation of the attitude reading.
    /// </summary>
    public Matrix RotationMatrix { get; set; }

    /// <summary>
    /// Gets a timestamp indicating the time at which the accelerometer reading was
    ///     taken. This can be used to correlate readings across sensors and provide
    ///     additional input to algorithms that process raw sensor data.
    /// </summary>
    public DateTimeOffset Timestamp { get; set; }

    /// <summary>
    /// Gets the yaw of the attitude reading in radians.
    /// </summary>
    public float Yaw { get; set; }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

WP7:将加速度计和指南针数据转换为模拟运动 API 的相关文章

  • Windows Phone 7 和 Windows Phone 8 支持什么框架?

    Windows Phone 7 和 Windows Phone 8 支持什么框架 我在网上找不到太多关于此的信息 但我听说WP7不支持完整的框架 如果是的话 WP7 和 WP8 的框架有哪些限制 奖金问题 WP7和WP8的编程仅限于C 吗
  • 过滤 ObservableCollection?

    当我将 ListBox 直接绑定到 ObservableCollection 时 我会在 ListBox 中显示实时更新 但是一旦我在混合中添加其他 LINQ 方法 我的 ListBox 就不再收到 ObservableCollection
  • 如何使用 .Net(在 Windows Phone 上)阅读公共 Twitter 源

    我正在尝试读取用户的公共 Twitter 状态 以便可以在我的 Windows Phone 应用程序中显示它 我用 Scott Gu 的例子 http weblogs asp net scottgu archive 2010 03 18 b
  • 反序列化 json 数组以列出 wp7

    我需要从 json 内的子数组获取数据 但它没有转换成列表 下面是我的 json 字符串 responseCode 0 responseObject TotalRecords 25 TotalDisplayRecords 25 aaData
  • 在WP7中调用普通的Web服务

    我有普通的 NET Web 服务 不是 WCF 服务 我已使用服务参考将此服务添加到我的 WP7 项目中 因为我们没有 通常我们会使用 添加服务引用 选项添加 WCF 服务 但这里我使用 添加服务引用 选项添加普通的 Web 服务 例如我有
  • Windows Phone 7 - 从十六进制动态设置按钮背景颜色? [复制]

    这个问题在这里已经有答案了 可能的重复 更改 Rectangle Fill 或 Grid Background 的自定义颜色 https stackoverflow com questions 10532056 change custom
  • 在哪里可以下载 Windows Phone 开发人员工具?

    我找到了 Web 下载程序 但它们似乎对我不起作用 我如何下载 ISO 格式的最新版本或完整安装程序 我无法使用网络下载器 谢谢 尝试这个 http go microsoft com fwlink LinkId 201927 http go
  • WP7 检查互联网是否可用

    我的应用程序 WP7 未被接受 因为如果互联网不可用 它无法加载 我寻找一种方法来检查它并找到了这个命令 NetworkInterface GetIsNetworkAvailable 但它无法在模拟器上运行 而且我没有任何设备来测试它 有人
  • 如何为 Windows Phone 7 应用程序的 UI 编写自动化测试?

    驱动一个普通的应用程序 它的 UI 已经足够困难了 但是当应用程序没有在您的开发平台上运行时 这就更难了 那么哪些工具可以帮助我测试 Windows Phone 7 应用程序的 UI 也可以看看 对于 Windows 7 Phone 代码的
  • 需要在Windows Phone 7屏幕上显示大量文字

    我想要在屏幕上显示大约 800 KB 的文本 有人可以让我知道这个问题的可能解决方案吗 由于文本块的 2048X2048 限制 我已经尝试将文本拆分为多个文本块 并且也尝试过http blogs msdn com b priozersk a
  • 添加新的 ApplicationBarMenuItem 图标时无法分配给属性

    我添加了一个新的ApplicationBarMenu带有图标的按钮到我的 wp7 项目中的页面 当尝试运行页面时我得到 无法分配给属性 Microsoft Phone Shell ApplicationBarIconButton Click
  • 不使用 PIN 的 Twitter 身份验证

    我正在尝试验证 Windows Phone 中的用户帐户 我找到了这个 C 库来完成这项工作 tweetsharp 他们的示例非常清楚 但他们使用 pin 码来验证用户身份 using TweetSharp Pass your creden
  • 用户控制可混合性 wp7

    嗨我想做一个简单的用户控件
  • 如何在 Windows Phone 7 中解析 XML(数据集)

    我有一个如下所示的 XML 但我无法解析它 请帮我解析下面的 XML
  • 如何隐藏数据透视项标题?

    我希望有一个具有 PivotItems 但没有横向数据透视项标题文本的数据透视控件 它是横向模式下的画廊 当恢复为纵向时 它应该再次显示 PivotItems 标题 解决办法就是制作文字PivotItem Header 不好 因为标题文本占
  • Windows Phone 7 中的 ASCII 编码

    有没有办法在 Windows Phone 7 中使用 ASCIIEncoding 除非我做错了什么Encoding ASCII不存在 我需要它来进行 C gt PHP 加密 因为 PHP 在 SHA1 加密中仅使用 ASCII 有什么建议么
  • 多个事件处理程序触发,为什么?

    我很难解决这个问题 我已经呆了三个小时了 但我仍然不明白为什么会这样 这是代码 private void Catagory SelectionChanged object sender SelectionChangedEventArgs e
  • 更改 Windows Phone 系统托盘颜色

    有没有办法将 Windows Phone 上的系统托盘颜色从黑色更改为白色 我的应用程序有白色背景 所以我希望系统托盘也是白色的 您可以在页面 XAML 中执行此操作
  • WP7:将参数传递到新页面?

    在 Windows Phone 7 Silverlight 应用程序中 我使用以下命令调用新页面 NavigationService Navigate new Uri View SecondPage xaml UriKind Relativ
  • 将事件绑定到 ItemsControl 中的按钮

    我有一个 Windows Phone 7 应用程序 其中包含一些 xaml 如下所示

随机推荐

  • RapidXML 给出空的 CDATA 节点

    我编写了下面的代码来获取 CDATA 节点值 我得到了节点的名称 但值是空白的 我将解析标志更改为 parse full 但它也不起作用 如果我从 XML 中手动删除 它会给出预期的值 但在解析之前删除它不是一个选项 代码 include
  • Promises - 在 Promise.all 中捕获所有拒绝[重复]

    这个问题在这里已经有答案了 我有这个虚拟代码 var Promise require bluebird function rej1 return new Promise reject new Error rej1 function rej2
  • Bootstrap 导航栏在滚动时折叠

    我在我的项目中使用引导灰度主题 它有一个在滚动时折叠的导航栏 或者如果我转到同一页面上的链接 download 等 问题是当我从其他页面转到锚链接时 导航栏在滚动之前不会折叠 我想解决方案是在java脚本中添加该行 但我真的不知道要添加什么
  • 如何解决OSError:[WinError 2]不可能找到指定文件:'c:\\ python39 \\ Scripts \\ chardetect.exe'

    正如标题中所述 每次我尝试通过 pip 安装某些内容时 在安装结束时都会出现错误 WARNING Failed to write executable trying to use deleteme logic Rolling back un
  • 如何获取JRadioButton的文本值

    我正在用java创建一个项目 我的程序有 80 个 JRadioButtons 我需要获取它们的文本值 现在这些单选按钮已添加到 ButtonGroup 每个单选按钮有 4 个单选按钮 我知道如何通过以下代码从单选按钮获取文本值 radio
  • 如何不屏蔽 GitHub Actions 中的输出?

    作为 GitHub Actions 中 PowerShell 脚本的一部分 我尝试输出一个 json 对象列表 以便稍后作为另一个作业的矩阵重新使用 使用以下命令我将编写输出 Write Host set output name value
  • XSLT 更改元素中的名称空间

    我正在尝试使用以下 xsl 代码更改元素属性的命名空间
  • ZendGdata框架路径设置错误

    你好 我正在使用 ZendGdata 1 12 5 框架在 youtube 上上传视频 我在我的 php 代码中使用了以下内容 path ZendGdata 1 12 5 library set include path get inclu
  • 如何使用 WS-UsernameToken 获取结果摘要?

    我有来自以下文档ONVIF 程序员指南 我目前正在尝试重现结果摘要使用指南中给出的相同条目 这是我的代码 private string GenerateHashedPassword string nonce string created s
  • Cassandra 无法在 Java 10 上启动

    我有一个全新的 Windows 10 家庭版安装 并全新安装了 JDK 10 0 1 这是我访问 JDK 下载站点时 Oracle 推荐的安装版本 我刚刚下载了 Cassandra 3 11 2 解压缩 d 它 并将 bin 目录放在我的类
  • firebase 云函数 API Google Cloud Storage 错误

    随着 Firebase Cloud Functions 的推出 我们正在考虑将当前的一些 Node js 服务器端代码迁移到云函数 我遇到的一个问题是从 GCS 存储桶下载文件到磁盘上的临时文件 然后将其作为附件通过电子邮件发送 使用 ma
  • 在初始 init“firebase”后添加/编辑 pod 文件

    因此 当我将 pod 文件安装到我的项目中时 我忘记添加到 FirebaseDatabase 中 现在我想将其添加到我该如何做 再次执行 pod init 过程会不会把事情弄乱 我的 Pod 文件照片 您应该只添加所需的新 Pod 然后运行
  • Vanilla JS 中具有 Momentum 的水平滚动

    我目前正在开发一个项目 我想要一个水平滑块 我使这个滑块可拖动 并找到了一些代码使其能够随动量滑动 我设法用鼠标滚轮进行水平滚动 但我不知道如何使其与动量效果一起工作 我能怎么做 Lorem ipsum dolor sat amet con
  • 带 Ifelse 条件的 Cbind/Rbind

    这是我正在使用的代码 x lt c Yes No No Yes Maybe y lt t 1 10 z lt t 11 20 rbind data frame ifelse x Yes y z 这会产生 X1L X12L X13L X4L
  • 提升.Interprocess notification() 性能

    我有两个进程 A 和 B 它们应该在 Windows 10 上使用 Boost Interprocess 通过共享内存快速交换数据 我的问题 之间的时间notify all 和wait 似乎很慢 通常为 15 毫秒 我最终编写了一个简单的应
  • 文字分隔符( \Q \E 块内的分隔符)

    我一直在尝试制作一些基于的功能RegEx他们中的大多数人都使用 Q and E作为一些RegEx pattern是用户输入 所以 假设我们正在使用delimiter 并想将其与 该函数将在以下行中构造一些东西 Q E 我不知道为什么 Q E
  • 阻止对 docker 容器的外部访问[关闭]

    Closed 这个问题不符合堆栈溢出指南 目前不接受答案 我想阻止从外部直接访问 docker 容器 我使用 haproxy 希望只允许访问端口 80 443 我在 iptables 中添加了以下规则 但我仍然可以通过不同的端口访问 doc
  • 在类中使用 boost::numeric::odeint

    对于模拟 我使用 boost numeric odeint 但遇到问题 我在我的一个类的方法中使用集成函数 但出现 没有匹配的函数可用于调用集成 的错误 为了更清楚 这是我的代码的压缩版本 include MotionGeneration
  • C 编译器错误:未定义的函数引用

    执行 exe 后 我收到此错误 对 StudentScan 的未定义引用错误 ld 返回 1 退出状态 注意 我的编码能力很差 而且是新手 所以请不要介意我的糟糕编码 注2 我只是在搞乱随机函数 include
  • WP7:将加速度计和指南针数据转换为模拟运动 API

    我正在为 Windows Phone 7 1 Mango 编写一个小型示例应用程序 并希望使用组合运动 API 来显示设备的运动 我需要编写模拟类 以便在使用不支持设备所有传感器的模拟器时能够测试我的应用程序 我已经编写了一个简单的模拟类来