Provide Simple Method to Get Current Speed (Implementing Speedometer)

provide simple method to get current speed (implementing speedometer)

The basic steps that you need to go through look something like this:

  1. Create a CLLocationmanager instance
  2. Assign a delegate with the
    appropriate callback method
  3. Check to see if the "speed" is set on the CLLocation in the callback, if it is - there's your speed
  4. (Optionally) if the "speed" isn't
    set, try calculating it from the distance between the last update
    and the current, divided by the difference in timestamps

    import CoreLocation

    class locationDelegate: NSObject, CLLocationManagerDelegate {
    var last:CLLocation?
    override init() {
    super.init()
    }
    func processLocation(_ current:CLLocation) {
    guard last != nil else {
    last = current
    return
    }
    var speed = current.speed
    if (speed > 0) {
    print(speed) // or whatever
    } else {
    speed = last!.distance(from: current) / (current.timestamp.timeIntervalSince(last!.timestamp))
    print(speed)
    }
    last = current
    }
    func locationManager(_ manager: CLLocationManager,
    didUpdateLocations locations: [CLLocation]) {
    for location in locations {
    processLocation(location)
    }
    }
    }

    var del = locationDelegate()
    var lm = CLLocationManager();
    lm.delegate = del
    lm.startUpdatingLocation()

Displaying the speed the device is travelling at using CLLocationSpeed

I found this here on SO by @Leo

Swift: Exception while trying to print CLLocationSpeed "unexpectedly found nil while unwrapping an Optional value"
:

import UIKit
import CoreLocation
class ViewController: UIViewController, CLLocationManagerDelegate {
let locationManager = CLLocationManager()

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
locationManager.delegate = self
if NSString(string:UIDevice.currentDevice().systemVersion).doubleValue > 8 {
locationManager.requestAlwaysAuthorization()
}
locationManager.desiredAccuracy=kCLLocationAccuracyBest
}

func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
var speed: CLLocationSpeed = CLLocationSpeed()
speed = locationManager.location.speed
println(speed);
}

func locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
if status != CLAuthorizationStatus.Denied{
locationManager.startUpdatingLocation()
}
}
}

then in your viewDidLoad, or wherever, you can make the label:

      myLabel = UILabel()
myLabel.text = "MySpeed: \(speed)"
self.addChild(myLabel)

Just make sure the 'speed' variable is in scope wherever you are trying to use it (I didn't demonstrate).

It compiled for me. Hope this helps. I haven't used it before, but with the Search function I'm confident I can learn most anything here :D

how do i find the high speed from a speedometer application?

Here:

cpeeda=arr[0];
borat= Math.max(0,arr[0]);

That is non-sensical. I guess you meant to assign

arr[0] = cpeeda;

But even that doesn't make much sense. The idea of arrays is that they provide multiple data points. When you only assign a value into the first slot, all the other array slots stay at their initial value (which would be 0 if done right). Btw: your code does not create an array, so arr is null first of all.

In the end, the real answer is two-fold:

  • you are absolutely overburdening yourself here. Your code implies that you lack a lot of knowledge about basic java. You should spend some days, weeks, to learn Java first, before going for the even more complicated "Java Android"
  • you don't need an array to just find a max speed!

Simply do something like:

double maxSpeed = 0; // some field of your class, similar to your array

... wherever you determine the current speed:

if (maxSpeed > currentSpeed) {
maxSpeed = currentSpeed;

Yet, when you want to store multiple datapoints, you either should create an array of a fixed size (where you start overwriting older values once you got to that fixed size), or you could use an ArrayList. That one grows dynamically, thus you could just keep adding values to it. (of course, at some point you better stop doing so, otherwise you will run out of memory sooner or later)

Is there a way to find the speed from analog speedometer?

Assuming the needle will have a different colour to the rest of the speedometer OR its size is distinctly larger than the rest of the elements on the speedometer (which is often the case), I'll do something like below.

  1. Convert the image to grayscale.
  2. Apply colour thresholding (or size-based thresholding) to detect the pixel area representing the needle.
  3. Use HoughLines() or HoughLinesP() functions in OpenCV to fit a line to the shape you detected in Step 2.
  4. Now it's a matter of measuring the angle of the line you generated in Step 3 (example provided here: How can I determine the angle a line found by HoughLines function using OpenCV?)
  5. You can then map the angle of the line to the speed through a simple equation (Will need to see an image of the speedometer to generate this).

let me know how it went.

How to get speed in android app using Location or accelerometer or some other way

I had to deal with same problem, what you can do is to use Location Strategies code.

Then, on each update of location, you save the time of the current update. So, you will have the previous and current location, and time of update.

Then you calculate the distance in meters between those two locations (the old and new one)

private static long calculateDistance(double lat1, double lng1, double lat2, double lng2) {
double dLat = Math.toRadians(lat2 - lat1);
double dLon = Math.toRadians(lng2 - lng1);
double a = Math.sin(dLat / 2) * Math.sin(dLat / 2)
+ Math.cos(Math.toRadians(lat1))
* Math.cos(Math.toRadians(lat2)) * Math.sin(dLon / 2)
* Math.sin(dLon / 2);
double c = 2 * Math.asin(Math.sqrt(a));
long distanceInMeters = Math.round(6371000 * c);
return distanceInMeters;
}

So, then you have the distance and the time difference, I think it wouldn't be a big deal to get the speed.

Get the speed of a moving camera

Simple and dirty, you can set a camera or any other object that has a position as measure Object to monitor its speed:

var DebugSpeedometer = (function() {

var lastMeasurement = 0,
currentSpeed = 0;

var lastMeasurementPosition = new THREE.Vector3(),
distance = new THREE.Vector3();

return {

update: function( measureObject, timeStep ){

if( lastMeasurement + timeStep < clock.getElapsedTime() ) {

lastMeasurement = clock.getElapsedTime();
distance = measureObject.getWorldPosition().distanceTo( lastMeasurementPosition );
currentSpeed = distance / timeStep;
lastMeasurementPosition = measureObject.getWorldPosition();

}

return currentSpeed;
}

};

})();

// globals
var clock = new THREE.Clock();
var speedoMeter = new DebugSpeedometer();

In your update function:

console.log( speedoMeter.update( camera, 1 ) );

Raspberry Pi Speedometer

Hi feet_traveled it's only defined and living inside the increase_count() function. Consider returning it if you need it outside of the scope of the function.


EDIT

The point is that the feet_traveled variable is indeed defined as global and it will be available outside of the function's scope as well, but you are only updating the variable:

feet_traveled += 2.095

without having it initialized first. You are adding 2.095 to something that can even not be float, in other words.
So my advice is: initialize the variable with

feet_traveled = 2.095

and your error will go away.



Related Topics



Leave a reply



Submit