This End Up: Using Device Orientation

本文将探讨如何使用HTML5的设备定位API来获取设备的倾斜角度和方向,并将其应用于图像旋转,以实现更丰富的交互体验。通过检查设备支持情况、创建标记和事件监听器,可以实时调整图像的角度,使网页应用更加生动有趣。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

英文原文:http://www.html5rocks.com/en/tutorials/device/orientation/?redirect_from_locale=zh

Pete LePage

Published: April 29th, 2011

Comments: 34

Introduction

Many new computers, mobile phones and other devices are now shipping standard with accelerometers, gyroscopes, compasses and other hardware designed to determine capture motion and orientation data. At the same time, web browsers are providing increasingly more access to that new hardware. One example of that are the new HTML5 DeviceOrientation and DeviceMotion events. These events provide developers with information about the orientation, motion and acceleration of the device.

Today, all modern mobile browsers support device orientation, and most support device motion, with Chrome for Android and Opera being the holdouts. Thankfully they're working on it and we should see it soon. Note, if you need to support older versions of Firefox, it has an implementation using a slightly different event model.

These events have a lot of fun uses, such as using orientation to control the direction of character in a game, or motion to determine how high a character should jump. Additionally, you can use these events beyond gaming. When used with GeoLocation, you can create a more accurate turn-by-turn navigation system.

In this article, we’ll take a look at the two different events, and use CSS to rotate an image based on the orientation of the device.

Which end is up?

There are a couple of things that we need to make sure we understand before we can start using these events, which end is up and what are the axes we’re going to use?

Device Orientation Data

To answer this question, we need to align our device with the local earth frame. To do that, take your device, and put it on flat, level surface with the top of the device pointing directly north. To do this with a phone, turn around until you are facing north, and place the phone on a flat table, with the bottom of the phone closest to you and the top pointed north. For a laptop, the situation is similar, except the screen is open at a 90° angle, facing you and the keyboard in-line with the surface.


Phone is in its normal position, on a flat on a surface, with the screen facing up, the top of the phone pointed directly north and the bottom of the phone is closest to you. The X, Y and Z values increase as you move to the right, forward or up respectively.

Device Motion Data

Acceleration data is returned as a coordinate frame with three axes, x, y and z. The x-axis runs side-to-side across the mobile phone screen, or the laptop keyboard and is positive towards the right side. The y-axis runs front-to-back across the mobile phone screen or the laptop keyboard and is positive towards as it moves away from you. The z-axis comes straight up out of the mobile phone screen or the laptop keyboard and is positive as it moves up.


The phone is rotated on the Y-Axis by gamma degrees.

The rotation data uses Euler angles to represent the difference between the device in it’s normal position and it’s current position. With the HTML5 device orientation events, the data is returned as the number of degrees different from normal. An easier way to think about it is how much the device is tilted front-to-back, is referred to as beta. How much its tilted side-to-side, is known as gamma. And finally how much has it been rotated around the z-axis, is known as alpha.

One thing to keep in mind is that most people don’t use their phones when they’re in this flat position, and instead have rotated them around the x-axis so the screen is facing them, this will affect the acceleration data that is returned from the device motion events.

Differences in Browsers

As mentioned earlier, alpha, beta and gamma are determined based on the position the device is with the local earth frame. For most browsers, alpha returns the compass heading, so when the device is pointed north, alpha is zero. With Mobile Safari, alpha is based on the direction the device was pointing when device orientation was first requested. The compass heading is available in the webkitCompassHeading parameter.

The events

Device orientation

The device orientation event returns only the rotation data, which includes how much the device is leaning front-to-back (beta), side-to-side (gamma) and if the phone or laptop has a compass, the direction the device is facing (alpha).

Let’s see a couple examples:


You are looking down at a phone that is lying on a flat, horizontal surface, with north at the top of the frame.
{evt.alpha: 0, evt.beta: 0, evt.gamma: 0}  
You are looking down at a phone that is lying on a flat, horizontal surface, with north at the top of the frame. The phone has been rotated by 180° so it is pointing south.
{evt.alpha: 180, evt.beta: 0, evt.gamma: 0}  
You are looking down at a device that does not have a compass built in, it is pointed west and tilted up by 45° with the left side of the phone higer than the right.
{evt.alpha: null, evt.beta: 0, evt.gamma: 45}  
You are looking down at a phone that is pointed north and tilted up by 45° with the top of the phone higer than the bottom.
{evt.alpha: 0, evt.beta: 45, evt.gamma: 0}

Device motion

The device motion event is a superset of the device orientation event; it returns data about the rotation information and also acceleration information about the device. The acceleration data is returned in three axes: x, y and z. They are measured in meters per second squared (m/s^2). Because some devices might not have the hardware to exclude the effect of gravity, the event returns two properties, accelerationIncludingGravity and acceleration, which excludes the effects of gravity, (when this is the case, the acceleration data will be null).

A laptop in its normal position, with the screen facing up, the data returned would be:

  Not accelerating Accelerating up Accelerating forward Accelerating right Accelerating up & to the right
acceleration {0, 0, 0} {0, 0, 5} {0, 2, 0} {3, 0, 0} {5, 0, 5}
accelerationIncludingGravity {0, 0, 9.81} {0, 0, 14.81} {0, 2, 9.81} {3, 0, 9.81} {5, 0, 14.81}

A mobile phone rotated along the x-axis so the screen is perpendicular to it’s normal position would return:

  Not accelerating Accelerating up Accelerating forward Accelerating right Accelerating up & to the right
acceleration {0, 0, 0} {0, 5, 0} {0, 0, 2} {3, 0, 0} {5, 5, 0}
accelerationIncludingGravity {0, 9.81, 0} {0, 14.81, 0} {0, 9.81, 2} {3, 9.81, 0} {5, 14.81, 0}

Using the DeviceOrientation events

1. Check for compatibility

Check to see if DeviceOrientationEvent supported.

if (window.DeviceOrientationEvent) {
 console.log("DeviceOrientation is supported");
}

One thing to note, some devices (for example, the original iPad) say that they support the event when in fact they don’t. In these cases, the event is fired only once, and all of the properties are null.

2. Declare the markup

For this example, we’re going to display the orientation data and then apply that information as a CSS3 transform to an image.

<div class="main">
  <h2>Device Orientation</h2>
  <table>
    <tr>
      <td>Event Supported</td>
      <td id="doEvent"></td>
    </tr>
    <tr>
      <td>Tilt Left/Right [gamma]</td>
      <td id="doTiltLR"></td>
    </tr>
    <tr>
      <td>Tilt Front/Back [beta]</td>
      <td id="doTiltFB"></td>
    </tr>
    <tr>
      <td>Direction [alpha]</td>
      <td id="doDirection"></td>
    </tr>
   </table>
</div>

<div class="container" style="perspective: 300;">
  <img src="html5_logo.png" id="imgLogo" class="logo">
</div>

3. Add an event listener

Now that we know what events are supported, add the appropriate event listeners:

if (window.DeviceOrientationEvent) {
  // Listen for the event and handle DeviceOrientationEvent object
  window.addEventListener('deviceorientation', devOrientHandler, false);
}

4. Handle the event

The HTML5 DeviceOrientation event returns three pieces of data:

  • alpha the direction the device is facing according to the compass
  • beta the angle in degrees the device is tilted front-to-back
  • gamma the angle in degrees the device is tilted left-to-right.
The angles values increase as you tilt the device to the right or towards you.

if (window.DeviceOrientationEvent) {
  document.getElementById("doEvent").innerHTML = "DeviceOrientation";
  // Listen for the deviceorientation event and handle the raw data
  window.addEventListener('deviceorientation', function(eventData) {
    // gamma is the left-to-right tilt in degrees, where right is positive
    var tiltLR = eventData.gamma;

    // beta is the front-to-back tilt in degrees, where front is positive
    var tiltFB = eventData.beta;

    // alpha is the compass direction the device is facing in degrees
    var dir = eventData.alpha

    // call our orientation event handler
    deviceOrientationHandler(tiltLR, tiltFB, dir);
  }, false);
} else {
  document.getElementById("doEvent").innerHTML = "Not supported."
}

5. Respond to the data

We’ll display it in the table we created in step 2, and also add rotate the image using a CSS3 transform.

document.getElementById("doTiltLR").innerHTML = Math.round(tiltLR);
document.getElementById("doTiltFB").innerHTML = Math.round(tiltFB);
document.getElementById("doDirection").innerHTML = Math.round(dir);

// Apply the transform to the image
var logo = document.getElementById("imgLogo");
logo.style.webkitTransform =
  "rotate("+ tiltLR +"deg) rotate3d(1,0,0, "+ (tiltFB*-1)+"deg)";
logo.style.MozTransform = "rotate("+ tiltLR +"deg)";
logo.style.transform =
  "rotate("+ tiltLR +"deg) rotate3d(1,0,0, "+ (tiltFB*-1)+"deg)";

The final product

Try it out, and view the source to see it in action.

Using the DeviceMotion events

The DeviceMotionEvent provides the acceleration and rotation data of the device. In our example, we’re going to use accelerationIncludingGravity to determine the orientation of the device and get a similar result to the sample we built withDeviceOrientation.

1. Check for compatibility

First thing we want to do is determine if the DeviceMotionEvent is supported using feature detection.

if (window.DeviceMotionEvent) {
  console.log("DeviceMotionEvent supported");
} 

Like DeviceOrientationEvent, if a browser cannot provide motion information, the event will be fired once and all properties will be null.

2. Add an event listener

Now that we know that the browser and device supports the DeviceMotionEvent, we can add an event listener.

if ((window.DeviceMotionEvent) {
  window.addEventListener('devicemotion', deviceMotionHandler, false);
} else {
  document.getElementById("dmEvent").innerHTML = "Not supported."
}

3. Declare the markup

For this example, we’re just going to display data that we receive.

<div class="main">
  <h2>Device Motion</h2>
  <table>
    <tr>
      <td>Event Supported</td>
      <td id="dmEvent"></td>
    </tr>
    <tr>
      <td>acceleration</td>
      <td id="moAccel"></td>
    </tr>
    <tr>
      <td>accelerationIncludingGravity</td>
      <td id="moAccelGrav"></td>
    </tr>
    <tr>
      <td>rotationRate</td>
      <td id="moRotation"></td>
    </tr>
    <tr>
      <td>interval</td>
      <td id="moInterval"></td>
    </tr>
  </table>
</div>

4. Create an event handler

We want to display the raw data that we get back.

function deviceMotionHandler(eventData) {
  var info, xyz = "[X, Y, Z]";

  // Grab the acceleration from the results
  var acceleration = eventData.acceleration;
  info = xyz.replace("X", acceleration.x);
  info = info.replace("Y", acceleration.y);
  info = info.replace("Z", acceleration.z);
  document.getElementById("moAccel").innerHTML = info;

  // Grab the acceleration including gravity from the results
  acceleration = eventData.accelerationIncludingGravity;
  info = xyz.replace("X", acceleration.x);
  info = info.replace("Y", acceleration.y);
  info = info.replace("Z", acceleration.z);
  document.getElementById("moAccelGrav").innerHTML = info;

  // Grab the rotation rate from the results
  var rotation = eventData.rotationRate;
  info = xyz.replace("X", rotation.alpha);
  info = info.replace("Y", rotation.beta);
  info = info.replace("Z", rotation.gamma);
  document.getElementById("moRotation").innerHTML = info;

  // // Grab the refresh interval from the results
  info = eventData.interval;
  document.getElementById("moInterval").innerHTML = info;       
}

The final product

When you load the page and move the device around, you should see the values change as the device moves through space and is affected by acceleration.

Try it out, and view the source to see it in action.

Additional resources

Here are a few other resources and demos you can check out that use the HTML5 device orientation or device motion events.

Physics

The Specs

Neat Demos

Pong  An experiment to try out device orientation in gaming.
01-18 16:50:07.926 222 222 I AudioFlinger: Using default 3000 mSec as standby time. 01-18 16:50:07.942 222 222 D AudioHardwareTiny: ALSA Audio Version: V1.1.0 01-18 16:50:07.942 222 222 D alsa_route: Can not get config table for sound card0 dummysound, so get default config table. 01-18 16:50:07.942 222 222 D AudioHardwareTiny: read_snd_card_info buf0 = dummysound 01-18 16:50:07.942 222 222 D AudioHardwareTiny: read_snd_card_info buf1 = ROCKCHIPSPDIF 01-18 16:50:07.942 222 222 D AudioHardwareTiny: read_snd_card_info buf2 = rkhdmispdif 01-18 16:50:07.942 222 222 D AudioHardwareTiny: now is 2 snd card mode 01-18 16:50:07.942 222 222 I AudioFlinger: loadHwModule() Loaded primary audio interface from Manta audio HW HAL (audio) handle 10 01-18 16:50:07.942 222 222 I AudioFlinger: openOutput(), module 10 Device 2, SamplingRate 48000, Format 0x000001, Channels 3, flags 2 01-18 16:50:07.942 222 222 D AudioHardwareTiny: audio hal adev_open_output_stream devices = 0x2, flags = 2, samplerate = 48000 01-18 16:50:07.942 222 222 D AudioHardwareTiny: out->config.rate = 48000, out->config.channels = 2 out->config.format = 0,out->config.flag = 0 01-18 16:50:07.943 222 222 I AudioFlinger: HAL output buffer size 512 frames, normal sink buffer size 1024 frames 01-18 16:50:07.978 222 222 I AudioFlinger: Using module 10 has the primary audio interface 01-18 16:50:07.978 222 222 D AudioHardwareTiny: adev_set_mode: set_mode = 0 01-18 16:50:07.986 222 252 I AudioFlinger: AudioFlinger&#39;s thread 0xb2103f80 ready to run 01-18 16:50:07.986 222 252 D AudioHardwareTiny: out_set_parameters: kvpairs = routing=2 01-18 16:50:07.988 222 252 W AudioFlinger: no wake lock to update, system not ready yet 01-18 16:50:07.991 222 254 I AudioFlinger: AudioFlinger&#39;s thread 0xb1d83b00 ready to run 01-18 16:50:07.996 222 222 I AudioFlinger: loadHwModule() Loaded a2dp audio interface from A2DP Audio HW HAL (audio) handle 18 01-18 16:50:08.007 222 222 I AudioFlinger: loadHwModule() Loaded usb audio interface from USB audio HW HAL (audio) handle 26 01-18 16:50:08.009 222 222 I r_submix: adev_open(name=audio_hw_if) 01-18 16:50:08.009 222 222 I AudioFlinger: loadHwModule() Loaded r_submix audio interface from Wifi Display audio HAL (audio) handle 34 01-18 16:50:08.012 222 256 I AudioFlinger: AudioFlinger&#39;s thread 0xb1c83bc0 ready to run 01-18 16:50:10.057 222 252 W AudioFlinger: no wake lock to update, system not ready yet 01-18 16:50:10.057 222 252 D AudioHardwareTiny: start_output_stream 01-18 16:50:10.066 222 252 D alsa_route: route_info->sound_card 0, route_info->devices 0 01-18 16:50:10.066 222 252 D alsa_route: route_set_controls() set route 0 01-18 16:50:17.994 222 252 D AudioHardwareTiny: out_set_parameters: kvpairs = routing=2 01-18 16:50:18.015 222 252 D AudioHardwareTiny: out_set_parameters: kvpairs = routing=2 01-18 16:50:18.036 222 252 D AudioHardwareTiny: out_set_parameters: kvpairs = routing=2 01-18 16:50:18.049 222 258 D AudioHardwareTiny: adev_set_parameters: kvpairs = orientation=undefined 01-18 16:50:18.615 222 258 D AudioHardwareTiny: adev_set_parameters: kvpairs = slice-weakly 01-18 16:50:18.694 222 258 D AudioHardwareTiny: adev_set_parameters: kvpairs = A2dpSuspended=false 01-18 16:50:18.725 222 258 I AudioFlinger: systemReady 01-18 16:50:19.041 222 252 I AudioFlinger: BUFFER TIMEOUT: remove(4096) from active list on thread 0xb2103f80 01-18 16:50:19.042 222 252 E AudioFlinger: no wake lock to update, but system ready! 01-18 16:50:20.188 222 252 E AudioFlinger: no wake lock to update, but system ready! 01-18 16:50:20.660 222 252 D AudioHardwareTiny: out_set_parameters: kvpairs = routing=2 01-18 16:50:20.693 222 252 D AudioHardwareTiny: out_set_parameters: kvpairs = routing=2 01-18 16:50:20.713 222 252 D AudioHardwareTiny: out_set_parameters: kvpairs = routing=2 01-18 16:50:20.725 222 252 D AudioHardwareTiny: out_set_parameters: kvpairs = routing=2 01-18 16:50:20.730 222 222 I AudioFlinger: openOutput(), module 10 Device 400, SamplingRate 44100, Format 0x000001, Channels 3, flags 1 01-18 16:50:20.730 222 222 D AudioHardwareTiny: audio hal adev_open_output_stream devices = 0x400, flags = 1, samplerate = 44100 01-18 16:50:20.730 222 222 D AudioHardwareTiny: Not any bitstream mode! 01-18 16:50:20.730 222 222 D AudioHardwareTiny: out->config.rate = 0, out->config.channels = 0 out->config.format = 0,out->config.flag = 0 01-18 16:50:20.731 222 247 D AudioHardwareTiny: adev_set_parameters: kvpairs = connect=1024 01-18 16:50:20.960 222 252 D AudioHardwareTiny: out_set_parameters: kvpairs = routing=1024 01-18 16:50:20.961 222 252 D alsa_route: route_set_controls() set route 24 01-18 16:50:20.961 222 252 D AudioHardwareTiny: close device 01-18 16:50:20.961 222 251 D AudioHardwareTiny: start_output_stream 01-18 16:50:20.962 222 251 D alsa_route: route_info->sound_card 1, route_info->devices 0 01-18 16:50:21.546 222 252 E AudioFlinger: no wake lock to update, but system ready! 01-18 16:50:21.699 222 222 D AudioHardwareTiny: adev_set_parameters: kvpairs = A2dpSuspended=false 01-18 16:50:24.442 222 252 D alsa_route: route_set_controls() set route 24 01-18 16:50:24.442 222 252 D AudioHardwareTiny: close device 08-11 18:17:32.263 222 252 D AudioHardwareTiny: start_output_stream 08-11 18:17:32.268 222 252 D alsa_route: route_info->sound_card 1, route_info->devices 0 08-11 18:17:36.463 222 252 D alsa_route: route_set_controls() set route 24 08-11 18:17:36.463 222 252 D AudioHardwareTiny: close device 08-11 18:17:39.677 222 1368 I AudioFlinger: AudioFlinger&#39;s thread 0xb1603a80 ready to run 08-11 18:17:39.692 222 1367 D AudioHardwareTiny: Device : 0x0 08-11 18:17:39.692 222 1367 D AudioHardwareTiny: SampleRate : 0 08-11 18:17:39.692 222 1367 D AudioHardwareTiny: Channels : 4 08-11 18:17:39.692 222 1367 D AudioHardwareTiny: Formate : 0 08-11 18:17:39.692 222 1367 D AudioHardwareTiny: PreiodSize : 0 08-11 18:17:39.692 222 1367 E AudioHardwareTiny: getInputRouteFromDevice:device:80000004 08-11 18:17:39.692 222 1367 D alsa_route: route_info->sound_card 0, route_info->devices 0 08-11 18:17:39.692 222 1367 D alsa_route: route_set_controls() set route 21 08-11 18:17:42.028 222 252 D AudioHardwareTiny: start_output_stream 08-11 18:17:42.029 222 252 D alsa_route: route_info->sound_card 1, route_info->devices 0 08-11 18:20:27.951 222 252 D AudioFlinger: mixer(0xb2103f80) throttle end: throttle time(6) 08-11 18:25:51.990 222 252 I AudioFlinger: BUFFER TIMEOUT: remove(4097) from active list on thread 0xb2103f80 08-11 18:27:16.399 222 252 D AudioFlinger: mixer(0xb2103f80) throttle end: throttle time(7) 08-11 18:28:45.387 222 252 I AudioFlinger: BUFFER TIMEOUT: remove(4097) from active list on thread 0xb2103f80 08-11 18:31:38.721 222 252 I AudioFlinger: BUFFER TIMEOUT: remove(4097) from active list on thread 0xb2103f80 08-11 18:34:32.031 222 252 I AudioFlinger: BUFFER TIMEOUT: remove(4097) from active list on thread 0xb2103f80 08-11 18:35:31.548 222 252 D AudioFlinger: mixer(0xb2103f80) throttle end: throttle time(1) 08-11 18:37:25.300 222 252 I AudioFlinger: BUFFER TIMEOUT: remove(4097) from active list on thread 0xb2103f80 08-11 18:38:53.382 222 252 D AudioFlinger: mixer(0xb2103f80) throttle end: throttle time(1) 08-11 18:40:18.677 222 252 I AudioFlinger: BUFFER TIMEOUT: remove(4097) from active list on thread 0xb2103f80 08-11 18:43:12.054 222 252 I AudioFlinger: BUFFER TIMEOUT: remove(4097) from active list on thread 0xb2103f80 08-11 18:44:34.010 222 252 D AudioFlinger: mixer(0xb2103f80) throttle end: throttle time(10) 08-11 18:44:34.266 222 252 D AudioFlinger: mixer(0xb2103f80) throttle end: throttle time(4) 08-11 18:46:05.388 222 252 I AudioFlinger: BUFFER TIMEOUT: remove(4097) from active list on thread 0xb2103f80 08-11 18:48:58.851 222 252 I AudioFlinger: BUFFER TIMEOUT: remove(4097) from active list on thread 0xb2103f80 08-11 18:51:46.265 222 252 D AudioFlinger: mixer(0xb2103f80) throttle end: throttle time(1) 08-11 18:51:52.159 222 252 I AudioFlinger: BUFFER TIMEOUT: remove(4097) from active list on thread 0xb2103f80 08-11 18:54:27.738 222 252 D AudioFlinger: mixer(0xb2103f80) throttle end: throttle time(5) 08-11 18:54:45.471 222 252 I AudioFlinger: BUFFER TIMEOUT: remove(4097) from active list on thread 0xb2103f80 08-11 18:55:08.676 222 252 D AudioFlinger: mixer(0xb2103f80) throttle end: throttle time(2) 08-11 18:57:38.954 222 252 I AudioFlinger: BUFFER TIMEOUT: remove(4097) from active list on thread 0xb2103f80 08-11 18:57:40.018 222 252 D AudioFlinger: mixer(0xb2103f80) throttle end: throttle time(13) 08-11 18:57:40.231 222 252 D AudioFlinger: mixer(0xb2103f80) throttle end: throttle time(2) 08-11 19:00:32.395 222 252 I AudioFlinger: BUFFER TIMEOUT: remove(4097) from active list on thread 0xb2103f80 08-11 19:03:25.814 222 252 I AudioFlinger: BUFFER TIMEOUT: remove(4097) from active list on thread 0xb2103f80 08-11 19:06:19.424 222 252 I AudioFlinger: BUFFER TIMEOUT: remove(4097) from active list on thread 0xb2103f80 08-11 19:13:06.054 222 252 D AudioFlinger: mixer(0xb2103f80) throttle end: throttle time(2) 08-11 19:15:50.475 222 252 I AudioFlinger: BUFFER TIMEOUT: remove(4097) from active list on thread 0xb2103f80 08-11 19:16:10.587 222 252 D AudioFlinger: mixer(0xb2103f80) throttle end: throttle time(3) 08-11 19:18:43.745 222 252 I AudioFlinger: BUFFER TIMEOUT: remove(4097) from active list on thread 0xb2103f80 08-11 19:24:32.987 222 252 D AudioFlinger: mixer(0xb2103f80) throttle end: throttle time(4) 08-11 19:29:59.697 222 252 D alsa_route: route_set_controls() set route 24 08-11 19:29:59.698 222 252 D AudioHardwareTiny: close device 08-11 19:34:14.303 222 252 D AudioHardwareTiny: start_output_stream 08-11 19:34:14.303 222 252 D alsa_route: route_info->sound_card 1, route_info->devices 0 08-11 19:34:30.498 222 1368 E AudioFlinger: read failed: framesRead=-2147483631 08-11 19:34:30.501 222 1368 D alsa_route: route_set_controls() set route 25 08-11 19:34:30.514 222 1367 D AudioHardwareTiny: Device : 0x0 08-11 19:34:30.514 222 1368 E AudioFlinger: read failed: framesRead=-2147483631 08-11 19:34:30.514 222 1367 D AudioHardwareTiny: SampleRate : 851968 08-11 19:34:30.514 222 1367 D AudioHardwareTiny: Channels : 4 08-11 19:34:30.514 222 1367 D AudioHardwareTiny: Formate : 0 08-11 19:34:30.514 222 1367 D AudioHardwareTiny: PreiodSize : 4800 08-11 19:34:30.514 222 1367 E AudioHardwareTiny: getInputRouteFromDevice:device:80000004 08-11 19:34:30.514 222 1367 D alsa_route: route_info->sound_card 0, route_info->devices 0 08-11 19:34:30.514 222 1367 D alsa_route: route_set_controls() set route 21 08-11 19:34:30.522 222 1368 D alsa_route: route_set_controls() set route 25 08-11 19:34:30.533 222 1367 D AudioHardwareTiny: Device : 0x0 08-11 19:34:30.533 222 1368 E AudioFlinger: read failed: framesRead=-2147483631 08-11 19:34:30.533 222 1367 D AudioHardwareTiny: SampleRate : 851968 08-11 19:34:30.533 222 1367 D AudioHardwareTiny: Channels : 4 08-11 19:34:30.533 222 1367 D AudioHardwareTiny: Formate : 0 08-11 19:34:30.533 222 1367 D AudioHardwareTiny: PreiodSize : 4800 分析下这段日志,错误原因
08-12
EPRobot@EPRobot:~/robot_ws/src/eprobot_start/script/one_car_pkg$ roscd usb_cam EPRobot@EPRobot:/opt/ros/melodic/share/usb_cam$ source /opt/ros/melodic/setup.bash EPRobot@EPRobot:/opt/ros/melodic/share/usb_cam$ cd ~/robot_ws EPRobot@EPRobot:~/robot_ws$ source devel/setup.bash EPRobot@EPRobot:~/robot_ws$ roslaunch eprobot_start one_car_start.launch ... logging to /home/EPRobot/.ros/log/9641654e-6234-11f0-a6b3-e45f0131f240/roslaunch-EPRobot-2443.log Checking log directory for disk usage. This may take a while. Press Ctrl-C to interrupt Done checking log file disk usage. Usage is <1GB. started roslaunch server http://EPRobot:45445/ SUMMARY ======== CLEAR PARAMETERS * /ekf_se/ PARAMETERS * /amcl/base_frame_id: base_footprint * /amcl/global_frame_id: map * /amcl/gui_publish_rate: 10.0 * /amcl/initial_pose_a: 0.0 * /amcl/initial_pose_x: 0.0 * /amcl/initial_pose_y: 0.0 * /amcl/kld_err: 0.05 * /amcl/kld_z: 0.99 * /amcl/laser_lambda_short: 0.1 * /amcl/laser_likelihood_max_dist: 5.0 * /amcl/laser_max_beams: 60 * /amcl/laser_max_range: 12.0 * /amcl/laser_model_type: likelihood_field * /amcl/laser_sigma_hit: 0.2 * /amcl/laser_z_hit: 0.7 * /amcl/laser_z_max: 0.1 * /amcl/laser_z_rand: 0.3 * /amcl/laser_z_short: 0.1 * /amcl/max_particles: 2000 * /amcl/min_particles: 600 * /amcl/odom_alpha1: 0.2 * /amcl/odom_alpha2: 0.2 * /amcl/odom_alpha3: 0.2 * /amcl/odom_alpha4: 0.2 * /amcl/odom_alpha5: 0.1 * /amcl/odom_frame_id: odom * /amcl/odom_model_type: diff * /amcl/recovery_alpha_fast: 0.2 * /amcl/recovery_alpha_slow: 0.005 * /amcl/resample_interval: 0.4 * /amcl/transform_tolerance: 0.5 * /amcl/update_min_a: 0.05 * /amcl/update_min_d: 0.05 * /amcl/use_map_topic: True * /base_control/base_kd: 0.0 * /base_control/base_ki: 100.0 * /base_control/base_kp: 1000.0 * /base_control/base_kv: 1.0 * /base_control/is_send_anger: false * /camera_uri: http://192.168.3.... * /ekf_se/base_link_frame: /base_footprint * /ekf_se/debug: False * /ekf_se/debug_out_file: /path/to/debug/fi... * /ekf_se/frequency: 30 * /ekf_se/imu0: /imu_data * /ekf_se/imu0_config: [False, False, Fa... * /ekf_se/imu0_differential: False * /ekf_se/imu0_nodelay: True * /ekf_se/imu0_pose_rejection_threshold: 0.8 * /ekf_se/imu0_queue_size: 8 * /ekf_se/imu0_relative: True * /ekf_se/imu0_remove_gravitational_acceleration: True * /ekf_se/initial_estimate_covariance: [&#39;1e-9&#39;, 0, 0, 0,... * /ekf_se/map_frame: /map * /ekf_se/odom0: /odom * /ekf_se/odom0_config: [False, False, Fa... * /ekf_se/odom0_differential: False * /ekf_se/odom0_nodelay: True * /ekf_se/odom0_queue_size: 5 * /ekf_se/odom0_relative: True * /ekf_se/odom_frame: /odom * /ekf_se/print_diagnostics: True * /ekf_se/process_noise_covariance: [0.01, 0, 0, 0, 0... * /ekf_se/publish_acceleration: True * /ekf_se/publish_tf: True * /ekf_se/sensor_timeout: 0.025 * /ekf_se/transform_time_offset: 0.0001 * /ekf_se/transform_timeout: 0.025 * /ekf_se/two_d_mode: True * /ekf_se/world_frame: /odom * /image_view/autosize: True * /laser_filter/scan_filter_chain: [{&#39;type&#39;: &#39;laser_... * /lslidar_driver_node/angle_disable_max: 0 * /lslidar_driver_node/angle_disable_min: 0 * /lslidar_driver_node/compensation: False * /lslidar_driver_node/frame_id: base_laser_link * /lslidar_driver_node/high_reflection: False * /lslidar_driver_node/interface_selection: serial * /lslidar_driver_node/lidar_name: M10 * /lslidar_driver_node/max_range: 100.0 * /lslidar_driver_node/min_range: 0.1 * /lslidar_driver_node/pubPointCloud2: False * /lslidar_driver_node/pubScan: True * /lslidar_driver_node/scan_topic: scan * /lslidar_driver_node/serial_port: /dev/EPRobot_laser * /lslidar_driver_node/use_gps_ts: False * /map_server/frame_id: map * /move_base/TebLocalPlannerROS/acc_lim_theta: 0.3 * /move_base/TebLocalPlannerROS/acc_lim_x: 0.2 * /move_base/TebLocalPlannerROS/allow_init_with_backwards_motion: True * /move_base/TebLocalPlannerROS/cmd_angle_instead_rotvel: False * /move_base/TebLocalPlannerROS/costmap_converter_plugin: * /move_base/TebLocalPlannerROS/costmap_converter_rate: 5 * /move_base/TebLocalPlannerROS/costmap_converter_spin_thread: True * /move_base/TebLocalPlannerROS/costmap_obstacles_behind_robot_dist: 1.0 * /move_base/TebLocalPlannerROS/dt_hysteresis: 0.1 * /move_base/TebLocalPlannerROS/dt_ref: 0.3 * /move_base/TebLocalPlannerROS/dynamic_obstacle_inflation_dist: 0.2 * /move_base/TebLocalPlannerROS/enable_homotopy_class_planning: False * /move_base/TebLocalPlannerROS/enable_multithreading: True * /move_base/TebLocalPlannerROS/exact_arc_length: False * /move_base/TebLocalPlannerROS/feasibility_check_no_poses: 3 * /move_base/TebLocalPlannerROS/footprint_model/line_end: [0.1, 0.0] * /move_base/TebLocalPlannerROS/footprint_model/line_start: [-0.1, 0.0] * /move_base/TebLocalPlannerROS/footprint_model/type: line * /move_base/TebLocalPlannerROS/force_reinit_new_goal_dist: 0.5 * /move_base/TebLocalPlannerROS/free_goal_vel: False * /move_base/TebLocalPlannerROS/global_plan_overwrite_orientation: True * /move_base/TebLocalPlannerROS/global_plan_viapoint_sep: 3.0 * /move_base/TebLocalPlannerROS/h_signature_prescaler: 0.5 * /move_base/TebLocalPlannerROS/h_signature_threshold: 0.1 * /move_base/TebLocalPlannerROS/include_costmap_obstacles: True * /move_base/TebLocalPlannerROS/include_dynamic_obstacles: True * /move_base/TebLocalPlannerROS/inflation_dist: 0.35 * /move_base/TebLocalPlannerROS/is_footprint_dynamic: False * /move_base/TebLocalPlannerROS/legacy_obstacle_association: False * /move_base/TebLocalPlannerROS/max_global_plan_lookahead_dist: 10.0 * /move_base/TebLocalPlannerROS/max_number_classes: 2 * /move_base/TebLocalPlannerROS/max_vel_theta: 1.0 * /move_base/TebLocalPlannerROS/max_vel_x: 0.6 * /move_base/TebLocalPlannerROS/max_vel_x_backwards: 0.3 * /move_base/TebLocalPlannerROS/max_vel_y: 0.0 * /move_base/TebLocalPlannerROS/min_obstacle_dist: 0.55 * /move_base/TebLocalPlannerROS/min_turning_radius: 0.6 * /move_base/TebLocalPlannerROS/no_inner_iterations: 5 * /move_base/TebLocalPlannerROS/no_outer_iterations: 4 * /move_base/TebLocalPlannerROS/obstacle_association_cutoff_factor: 5.0 * /move_base/TebLocalPlannerROS/obstacle_association_force_inclusion_factor: 1.5 * /move_base/TebLocalPlannerROS/obstacle_cost_exponent: 5.5 * /move_base/TebLocalPlannerROS/obstacle_heading_threshold: 0.45 * /move_base/TebLocalPlannerROS/obstacle_keypoint_offset: 0.1 * /move_base/TebLocalPlannerROS/obstacle_poses_affected: 15 * /move_base/TebLocalPlannerROS/odom_topic: odom * /move_base/TebLocalPlannerROS/optimization_activate: True * /move_base/TebLocalPlannerROS/optimization_verbose: False * /move_base/TebLocalPlannerROS/oscillation_recovery: True * /move_base/TebLocalPlannerROS/penalty_epsilon: 0.08 * /move_base/TebLocalPlannerROS/roadmap_graph_area_length_scale: 1.0 * /move_base/TebLocalPlannerROS/roadmap_graph_area_width: 5 * /move_base/TebLocalPlannerROS/roadmap_graph_no_samples: 15 * /move_base/TebLocalPlannerROS/selection_alternative_time_cost: False * /move_base/TebLocalPlannerROS/selection_cost_hysteresis: 1.0 * /move_base/TebLocalPlannerROS/selection_obst_cost_scale: 1.0 * /move_base/TebLocalPlannerROS/selection_viapoint_cost_scale: 1.0 * /move_base/TebLocalPlannerROS/shrink_horizon_backup: True * /move_base/TebLocalPlannerROS/simple_exploration: False * /move_base/TebLocalPlannerROS/teb_autosize: True * /move_base/TebLocalPlannerROS/via_points_ordered: False * /move_base/TebLocalPlannerROS/visualize_hc_graph: False * /move_base/TebLocalPlannerROS/weight_acc_lim_theta: 1 * /move_base/TebLocalPlannerROS/weight_acc_lim_x: 1 * /move_base/TebLocalPlannerROS/weight_adapt_factor: 1.0 * /move_base/TebLocalPlannerROS/weight_dynamic_obstacle: 100 * /move_base/TebLocalPlannerROS/weight_dynamic_obstacle_inflation: 0.6 * /move_base/TebLocalPlannerROS/weight_inflation: 1.0 * /move_base/TebLocalPlannerROS/weight_kinematics_forward_drive: 800 * /move_base/TebLocalPlannerROS/weight_kinematics_nh: 1000 * /move_base/TebLocalPlannerROS/weight_kinematics_turning_radius: 6.0 * /move_base/TebLocalPlannerROS/weight_max_vel_theta: 1 * /move_base/TebLocalPlannerROS/weight_max_vel_x: 2 * /move_base/TebLocalPlannerROS/weight_obstacle: 190 * /move_base/TebLocalPlannerROS/weight_optimaltime: 15 * /move_base/TebLocalPlannerROS/weight_shortest_path: 5.0 * /move_base/TebLocalPlannerROS/weight_viapoint: 10.0 * /move_base/TebLocalPlannerROS/wheelbase: 0.3 * /move_base/TebLocalPlannerROS/xy_goal_tolerance: 0.1 * /move_base/TebLocalPlannerROS/yaw_goal_tolerance: 0.1 * /move_base/base_local_planner: teb_local_planner... * /move_base/clearing_rotation_allowed: False * /move_base/conservative_reset_dist: 0.2 * /move_base/controller_frequency: 5.0 * /move_base/controller_patience: 15.0 * /move_base/global_costmap/footprint: [[-0.18, -0.18], ... * /move_base/global_costmap/global_frame: map * /move_base/global_costmap/inflation_layer/cost_scaling_factor: 10.0 * /move_base/global_costmap/inflation_layer/enabled: True * /move_base/global_costmap/inflation_layer/inflation_radius: 0.3 * /move_base/global_costmap/inflation_radius: 0.4 * /move_base/global_costmap/laser_scan_sensor/clearing: True * /move_base/global_costmap/laser_scan_sensor/data_type: LaserScan * /move_base/global_costmap/laser_scan_sensor/marking: True * /move_base/global_costmap/laser_scan_sensor/topic: scan * /move_base/global_costmap/map_type: costmap * /move_base/global_costmap/observation_sources: laser_scan_sensor * /move_base/global_costmap/obstacle_layer/combination_method: 1 * /move_base/global_costmap/obstacle_layer/enabled: True * /move_base/global_costmap/obstacle_layer/inflation_radius: 0.2 * /move_base/global_costmap/obstacle_layer/obstacle_range: 6.5 * /move_base/global_costmap/obstacle_layer/raytrace_range: 6.0 * /move_base/global_costmap/obstacle_layer/track_unknown_space: False * /move_base/global_costmap/plugins: [{&#39;type&#39;: &#39;costma... * /move_base/global_costmap/publish_frequency: 5.0 * /move_base/global_costmap/robot_base_frame: base_footprint * /move_base/global_costmap/static_layer/enabled: True * /move_base/global_costmap/static_layer/map_topic: /map * /move_base/global_costmap/static_map: True * /move_base/global_costmap/transform_tolerance: 0.5 * /move_base/global_costmap/update_frequency: 5.0 * /move_base/local_costmap/cost_scaling_factor: 10 * /move_base/local_costmap/footprint: [[-0.18, -0.18], ... * /move_base/local_costmap/global_frame: map * /move_base/local_costmap/height: 3 * /move_base/local_costmap/inflation_layer/cost_scaling_factor: 10.0 * /move_base/local_costmap/inflation_layer/enabled: True * /move_base/local_costmap/inflation_layer/inflation_radius: 0.3 * /move_base/local_costmap/inflation_radius: 0.16 * /move_base/local_costmap/laser_scan_sensor/clearing: True * /move_base/local_costmap/laser_scan_sensor/data_type: LaserScan * /move_base/local_costmap/laser_scan_sensor/marking: True * /move_base/local_costmap/laser_scan_sensor/topic: scan * /move_base/local_costmap/map_type: costmap * /move_base/local_costmap/observation_sources: laser_scan_sensor * /move_base/local_costmap/obstacle_layer/combination_method: 1 * /move_base/local_costmap/obstacle_layer/enabled: True * /move_base/local_costmap/obstacle_layer/inflation_radius: 0.2 * /move_base/local_costmap/obstacle_layer/obstacle_range: 6.5 * /move_base/local_costmap/obstacle_layer/raytrace_range: 6.0 * /move_base/local_costmap/obstacle_layer/track_unknown_space: False * /move_base/local_costmap/plugins: [{&#39;type&#39;: &#39;costma... * /move_base/local_costmap/publish_frequency: 5.0 * /move_base/local_costmap/resolution: 0.05 * /move_base/local_costmap/robot_base_frame: base_footprint * /move_base/local_costmap/rolling_window: True * /move_base/local_costmap/static_layer/enabled: True * /move_base/local_costmap/static_layer/map_topic: /map * /move_base/local_costmap/static_map: False * /move_base/local_costmap/transform_tolerance: 0.5 * /move_base/local_costmap/update_frequency: 5.0 * /move_base/local_costmap/width: 3 * /move_base/oscillation_distance: 0.2 * /move_base/oscillation_timeout: 10.0 * /move_base/planner_frequency: 1.0 * /move_base/planner_patience: 5.0 * /robot_description: <?xml version="1.... * /rosdistro: melodic * /rosversion: 1.14.13 * /time0: 5.0 * /time1: 15.0 * /time2: 2.0 * /time3: 10.0 * /time4: 2.0 * /time5: 5.0 * /usb_cam/camera_frame_id: usb_cam * /usb_cam/color_format: yuv422p * /usb_cam/image_height: 960 * /usb_cam/image_width: 1280 * /usb_cam/io_method: mmap * /usb_cam/pixel_format: yuyv * /usb_cam/video_device: /dev/video0 NODES / amcl (amcl/amcl) base_control (eprobot_start/art_racecar.py) base_to_camera (tf/static_transform_publisher) base_to_gyro (tf/static_transform_publisher) base_to_laser (tf/static_transform_publisher) base_to_link (tf/static_transform_publisher) ekf_se (robot_localization/ekf_localization_node) image_view (image_view/image_view) joint_state_publisher (joint_state_publisher/joint_state_publisher) laser_filter (laser_filters/scan_to_scan_filter_chain) lslidar_driver_node (lslidar_driver/lslidar_driver_node) map_server (map_server/map_server) move_base (move_base/move_base) one_car_start (eprobot_start/one_car_start.py) robot_state_publisher (robot_state_publisher/robot_state_publisher) usb_cam (usb_cam/usb_cam_node) auto-starting new master process[master]: started with pid [2469] ROS_MASTER_URI=http://EPRobot:11311 setting /run_id to 9641654e-6234-11f0-a6b3-e45f0131f240 process[rosout-1]: started with pid [2498] started core service [/rosout] process[usb_cam-2]: started with pid [2502] process[image_view-3]: started with pid [2519] [ INFO] [1752663855.296181382]: Initializing nodelet with 4 worker threads. [ INFO] [1752663855.480798436]: using default calibration URL [ INFO] [1752663855.484462140]: camera calibration URL: file:///home/EPRobot/.ros/camera_info/head_camera.yaml [ INFO] [1752663855.491997843]: Starting &#39;head_camera&#39; (/dev/video0) at 1280x960 via mmap (yuyv) at 30 FPS [ INFO] [1752663855.661129732]: Using transport "raw" Unable to init server: Could not connect: Connection refused (image_raw:2519): Gtk-WARNING **: 19:04:15.720: cannot open display: [ WARN] [1752663855.904948901]: unknown control &#39;focus_auto&#39; process[base_to_link-4]: started with pid [2537] [image_view-3] process has died [pid 2519, exit code 1, cmd /opt/ros/melodic/lib/image_view/image_view image:=/usb_cam/image_raw __name:=image_view __log:=/home/EPRobot/.ros/log/9641654e-6234-11f0-a6b3-e45f0131f240/image_view-3.log]. log file: /home/EPRobot/.ros/log/9641654e-6234-11f0-a6b3-e45f0131f240/image_view-3*.log process[base_to_gyro-5]: started with pid [2556] process[base_to_laser-6]: started with pid [2562] process[base_to_camera-7]: started with pid [2583] process[joint_state_publisher-8]: started with pid [2604] process[robot_state_publisher-9]: started with pid [2610] /opt/ros/melodic/lib/python2.7/dist-packages/roslib/packages.py:470: UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode - interpreting them as being unequal if resource_name in files: process[base_control-10]: started with pid [2618] process[lslidar_driver_node-11]: started with pid [2647] [ INFO] [1752663866.105583696]: Lidar is M10 [ INFO] [1752663866.111040782]: Opening PCAP file port = /dev/EPRobot_laser, baud_rate = 460800 open_port /dev/EPRobot_laser OK ! [ INFO] [1752663866.125467287]: Initialised lslidar without error process[laser_filter-12]: started with pid [2676] [ INFO] [1752663867.962878802]: clear_inside(!invert): true [INFO] [1752663869.073894]: base control start... process[map_server-13]: started with pid [2683] [INFO] [1752663869.127131]: LaserMode : Express [INFO] [1752663869.131959]: is_pub_odom_tf : false [INFO] [1752663869.138982]: is_send_anger : false [INFO] [1752663869.144625]: Opening Serial [INFO] [1752663869.150115]: Serial Open Succeed process[amcl-14]: started with pid [2718] [ INFO] [1752663871.037123474]: Subscribed to map topic. process[move_base-15]: started with pid [2774] process[ekf_se-16]: started with pid [2814] ERROR: cannot launch node of type [eprobot_start/one_car_start.py]: Cannot locate node of type [one_car_start.py] in package [eprobot_start]. Make sure file exists in package path and permission is set to executable (chmod +x) [ WARN] [1752663879.396290450]: Timed out waiting for transform from base_footprint to map to become available before running costmap, tf error: canTransform: target_frame map does not exist. canTransform: source_frame base_footprint does not exist.. canTransform returned after 0.100705 timeout was 0.1. [ INFO] [1752663880.901153484]: Received a 113 X 155 map @ 0.050 m/pix [ INFO] [1752663880.938459111]: Initializing likelihood field model; this can take some time on large maps... [ INFO] [1752663881.063300341]: Done initializing likelihood field model. [ WARN] [1752663883.460416017]: Transform from IMU_link to base_footprint was unavailable for the time requested. Using latest instead. [ WARN] [1752663884.436037594]: Timed out waiting for transform from base_footprint to map to become available before running costmap, tf error: canTransform: target_frame map does not exist.. canTransform returned after 0.100968 timeout was 0.1. [ WARN] [1752663886.469901598]: No laser scan received (and thus no pose updates have been published) for 1752663886.469788 seconds. Verify that data is being published on the /scan_filtered topic. [ WARN] [1752663887.516970206]: global_costmap: Pre-Hydro parameter "static_map" unused since "plugins" is provided [ WARN] [1752663887.518994345]: global_costmap: Pre-Hydro parameter "map_type" unused since "plugins" is provided [ INFO] [1752663887.523573417]: global_costmap: Using plugin "static_layer" [ INFO] [1752663887.657142563]: Requesting the map... [ INFO] [1752663889.166172394]: Resizing costmap to 113 X 155 at 0.050000 m/pix [ INFO] [1752663889.265445621]: Received a 113 X 155 map at 0.050000 m/pix [ INFO] [1752663889.280243055]: global_costmap: Using plugin "obstacle_layer" [ INFO] [1752663889.298549771]: Subscribed to Topics: [ INFO] [1752663889.349325212]: global_costmap: Using plugin "inflation_layer" [ WARN] [1752663889.545199085]: local_costmap: Pre-Hydro parameter "static_map" unused since "plugins" is provided [ WARN] [1752663889.547135725]: local_costmap: Pre-Hydro parameter "map_type" unused since "plugins" is provided [ INFO] [1752663889.551484224]: local_costmap: Using plugin "static_layer" [ INFO] [1752663889.571839522]: Requesting the map... [ INFO] [1752663889.577998384]: Resizing static layer to 113 X 155 at 0.050000 m/pix [ INFO] [1752663889.677619257]: Received a 113 X 155 map at 0.050000 m/pix [ INFO] [1752663889.693015967]: local_costmap: Using plugin "obstacle_layer" [ INFO] [1752663889.706179575]: Subscribed to Topics: [ INFO] [1752663889.742291675]: local_costmap: Using plugin "inflation_layer" [ INFO] [1752663889.976098453]: Created local_planner teb_local_planner/TebLocalPlannerROS [ INFO] [1752663890.170609667]: Footprint model &#39;line&#39; (line_start: [-0.1,0]m, line_end: [0.1,0]m) loaded for trajectory optimization. [ INFO] [1752663890.172065624]: Parallel planning in distinctive topologies disabled. [ INFO] [1752663890.172326715]: No costmap conversion plugin specified. All occupied costmap cells are treaten as point obstacles. [ INFO] [1752663891.088227612]: Recovery behavior will clear layer &#39;obstacles&#39; [ INFO] [1752663891.107617471]: Recovery behavior will clear layer &#39;obstacles&#39; [ INFO] [1752663891.666258229]: odom received!
07-17
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值