Have any of these attempts to get GPS time into a mavlink been successful? - dronekit-python

I'm trying to get GPS time out of a pixhawk. I've found a bunch of discussions about such but none that appear to have been resolved. Is there any update?
This guy and this guy were both told to just use system time (which is not UTC)
I also tried to get GLOBAL_POSITION_INT_COV but found it was not available.
Here is a long dev discussion about such that was never resolved (circa 2013)
Another dev discussion that references a bunch of pull requests for such - but doesn't look like any of them ever made it in or am I wrong?
Much thanks!

As noted in your second link, ArduPilot sends unix time in the SYSTEM_TIME message. You didn't mention what language you are using, but in Python, unix time can easily be converted to UTC using the datetime module.
#vehicle.on_message('SYSTEM_TIME')
def listener(self, name, message):
unix_time = (int) (message.time_unix_usec/1000000)
print(datetime.datetime.fromtimestamp( unix_time ))

GPS times reported in MAVLink messages are in two parts: The GPS week number, and the number of milliseconds since the start of that week. Also, need to know when GPS weeks started (Jan 6, 1980).
Here's a bit of javascript code that seems to work well for me (could easily be ported to other languages):
// no. of milliseconds in a date since midnight of
// January 1, 1970, according to UTC time;
// months are zero-based, but days are 1-based
var GPS_EPOCH_MILLIS = (new Date(1980, 0, 6, 0, 0, 0, 0)).getTime();
// all the leap seconds that have been defined so far;
// must keep this list current!
var leapSecondsMillis = [
(new Date(1981, 6, 1, 0, 0, 0, 0)).getTime(),
(new Date(1982, 6, 1, 0, 0, 0, 0)).getTime(),
(new Date(1983, 6, 1, 0, 0, 0, 0)).getTime(),
(new Date(1985, 6, 1, 0, 0, 0, 0)).getTime(),
(new Date(1988, 0, 1, 0, 0, 0, 0)).getTime(),
(new Date(1990, 0, 1, 0, 0, 0, 0)).getTime(),
(new Date(1991, 0, 1, 0, 0, 0, 0)).getTime(),
(new Date(1992, 6, 1, 0, 0, 0, 0)).getTime(),
(new Date(1993, 6, 1, 0, 0, 0, 0)).getTime(),
(new Date(1994, 6, 1, 0, 0, 0, 0)).getTime(),
(new Date(1996, 0, 1, 0, 0, 0, 0)).getTime(),
(new Date(1997, 6, 1, 0, 0, 0, 0)).getTime(),
(new Date(1999, 0, 1, 0, 0, 0, 0)).getTime(),
(new Date(2006, 0, 1, 0, 0, 0, 0)).getTime(),
(new Date(2009, 0, 1, 0, 0, 0, 0)).getTime(),
(new Date(2012, 6, 1, 0, 0, 0, 0)).getTime(),
(new Date(2015, 6, 1, 0, 0, 0, 0)).getTime(),
(new Date(2017, 0, 1, 0, 0, 0, 0)).getTime()
];
// convert GPS time to a date & time object
var gpsTimeToDate = function(gpsWeek, millisIntoGPSWeek) {
return new Date(gpsTimeToMillis(gpsWeek, millisIntoGPSWeek));
};
// 604,800 seconds in a week (60 x 60 x 24 x 7)
var gpsTimeToMillis = function(gpsWeek, millisIntoGPSWeek) {
var millis = GPS_EPOCH_MILLIS + (gpsWeek * 604800000) + millisIntoGPSWeek;
// add leap seconds as appropriate
for (var leap in leapSecondsMillis)
if (millis >= leap)
millis -= 1000;
return millis;
};

Related

resize array while keeping mask

I'm trying to figure out how to effectively resize an 1-d array while keeping the mask it represents. Using this array i do draw simple sprites while one value in the array represents a specific color.
Anyway my goal is as follows, having the following "small" array with values:
0, 1, 2, 3,
0, 1, 2, 2,
0, 1, 1, 1,
0, 0, 1, 1,
0, 0, 0, 0
This obviously is going to be a sprite of size 4x5.
Now i want to resize it keeping the values so getting the same sprite/shape but in higher resolution.
Now by saying "scale-by-2" i would get a 8x10 sized sprite, the 1-d array then should look as follows:
0, 0, 1, 1, 2, 2, 3, 3,
0, 0, 1, 1, 2, 2, 3, 3,
0, 0, 1, 1, 2, 2, 2, 2,
0, 0, 1, 1, 2, 2, 2, 2,
0, 0, 1, 1, 1, 1, 1, 1,
0, 0, 1, 1, 1, 1, 1, 1,
0, 0, 0, 0, 1, 1, 1, 1,
0, 0, 0, 0, 1, 1, 1, 1,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0
My idea is to group the numbers row by row, take the scale factor (2) and add as many of the digits (from one group) as we have to scale (2) in one row. Then duplicate each row by the scale factor as well. But still i am not sure if this covers all cases.
Any other (more effective) way to handle this?

Python 3 array return value

fellow python programmers.
I have been working on a small tool that will help automate some email distribution for a repeated task.
I'm writing a function that takes a list of items and I'm stripping out the usernames in the email, matching it with a CSV file and finding the email that correlates with that user.
I am successfully getting all of the information that I need, however I'm trying to return the data in an array that is a list with 3 total columns that should look like so
[reference#, user, email,
reference#, user, email]
Below is the code that I have tried, but it just returns an array full of zeroes.
def gu(tids):
data = [[0 for i in range(len(tids))] for j in range(1)]
#In each ticket, splice out the username
for tid in tids:
#print(tid.Subject)
su = tid.Body.find("from ") + 5
eu = tid.Body.find(" has")
u = tid.Body[su:eu]
with open('c:\\software\\users_and_emails.csv', "r") as f:
reader = csv.reader(f)
for k, row in reader:
if u.lower() == row[0].lower():
#print(row)
tidSubject = tid.Subject[30:-1]
data[k][0] = tidSubject
data[k][1] = row[0]
data[k][2] = row[1]
return data
For whatever reason this returns an empty array of the appropriate length
[[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
Could someone help me out in understanding why it's not returning the appropriate value?
The below code without storing values in the array prints out the appropriate values.
def gu(tids):
data = [[0 for i in range(len(tids))] for j in range(1)]
#In each ticket, splice out the username
for tid in tids:
#print(tid.Subject)
su = tid.Body.find("from ") + 5
eu = tid.Body.find(" has")
u = tid.Body[su:eu]
with open('c:\\software\\users_and_emails.csv', "r") as f:
reader = csv.reader(f)
for row in reader:
if u.lower() == row[0].lower():
#print(row)
tidSubject = tid.Subject[30:-1]
#data[i][0] = tidSubject
#data[i][1] = row[0]
#data[i][2] = row[1]
print(tidSubject)
print(row[0])
print(row[1])
#print(i)
#return data
And it returns data similar to this (have to obscure actual returns, sorry)
47299
username1
user1-emailaddress#foo.com
47303
username2
user2-emailaddress#foo.com
47307
username3
user3-emailaddress#foo.com
47312
username4
user4-emailaddress#foo.com
47325
username5
user5-emailaddress#foo.com
Try this.
def gu(tids):
data = []
#In each ticket, splice out the username
for tid in tids:
#print(tid.Subject)
su = tid.Body.find("from ") + 5
eu = tid.Body.find(" has")
u = tid.Body[su:eu]
with open('c:\\software\\users_and_emails.csv', "r") as f:
reader = csv.reader(f)
for row in reader:
if u.lower() == row[0].lower():
#print(row)
tidSubject = tid.Subject[30:-1]
subject = tidSubject
row0 = row[0]
row1 = row[1]
data.append([subject, row0, row1])
return data

python: vectorized cumulative counting

I have a numpy array and would like to count the number of occurences for each value, however, in a cumulative way
in = [0, 1, 0, 1, 2, 3, 0, 0, 2, 1, 1, 3, 3, 0, ...]
out = [0, 0, 1, 1, 0, 0, 2, 3, 1, 2, 3, 1, 2, 4, ...]
I'm wondering if it is best to create a (sparse) matrix with ones at col = i and row = in[i]
1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0
0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0
0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0
0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0
Then we could compute the cumsums along the rows and extract the numbers from the locations where the cumsums increment.
However, if we cumsum a sparse matrix, doesn't become dense? Is there an efficient way of doing it?
Here's one vectorized approach using sorting -
def cumcount(a):
# Store length of array
n = len(a)
# Get sorted indices (use later on too) and store the sorted array
sidx = a.argsort()
b = a[sidx]
# Mask of shifts/groups
m = b[1:] != b[:-1]
# Get indices of those shifts
idx = np.flatnonzero(m)
# ID array that will store the cumulative nature at the very end
id_arr = np.ones(n,dtype=int)
id_arr[idx[1:]+1] = -np.diff(idx)+1
id_arr[idx[0]+1] = -idx[0]
id_arr[0] = 0
c = id_arr.cumsum()
# Finally re-arrange those cumulative values back to original order
out = np.empty(n, dtype=int)
out[sidx] = c
return out
Sample run -
In [66]: a
Out[66]: array([0, 1, 0, 1, 2, 3, 0, 0, 2, 1, 1, 3, 3, 0])
In [67]: cumcount(a)
Out[67]: array([0, 0, 1, 1, 0, 0, 2, 3, 1, 2, 3, 1, 2, 4])

I want to use Bilinear interpolation to calculate the summation of vectors

I have individual vectors from my last stage of code which i implemented it
The next stage of the algorithm is to calculate the summation of these vectors
As mentioned in the paper
"The vectors from the previous stage were summed together spatially by bilinearly weighting"
I think The bilinear weighting means bilinear interpolation
can any one tell or give me an example how can i use bilinear interpolation
to calculate the Summation of this vectors
V1 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2]
V2 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 11, 11]
V3 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 23, 0, 0]
V4 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 19, 19, 0, 0]
V5 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 0, 0]
V6 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 8, 0, 0]
V7 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 18, 18, 0, 0]
V8 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 23, 23, 0, 0, 0]
V9= [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0]
V10 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 0, 0]
V11 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 8, 0, 0]
V12 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 11, 11, 0, 0, 0]
V13 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
V14 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0]
V15 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 0, 0, 0]
V16 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 0, 0, 0, 0]
I googled it but didn't understand the Equations
Regards and thanks in advance !
Sadly I'm having trouble understanding the paper as well. The idea, as you've said, is to weight the vectors based on their distance from the pooling centres, so that vectors farther from the pooling centres have less of an impact. The paper compares this to what is done in the famous SIFT feature, which you can read about in this tutorial.
Below is by best guess as to what the meaning is. Since this is related to machine learning, could also ask people over at cross-validated to get their opinion, or consider contacting the author of the paper.
If I understand correctly, this is amounts to a process similar to bilinear interpolation, except in reverse.
With bilinear interpolation, we are given a set of function values arranged in a grid, and we want to find a good guess for what the function values are between the gridpoints. We do this by taking a weighted average of the four surrounding function values, with the weights being the relative area of the opposite rectangle in the image below. (By "relative" I mean the area is normalized by the area of the whole grid rectangle, so the weights sum to 1.) Note how the point to be interpolated is the closest to the (x1,y2) gridpoint, so we weight it with the largest weight (the relative area of the yellow rectangle).
f(x,y) = w_11*f(x1,y1) + w_21*f(x2,y1) + w_12*f(x1,y2) + w_22*f(x2,y2)
w_ij = area of rectangle opposite (xi,yj) / total area of grid square
The "bilinear weighing" described in the paper seems to be doing the opposite: we have values (or vectors in this case) scattered throughout 2D space, and we want to "pool" their values at a set of gridpoints that we choose.
We do this by adding a fraction of each vector to the four surrounding pooling gridpoints. This fraction would again be the relative area of the opposite rectangle.
In the above image... pooling point (xi,yj) would get w_ij * f(x,y) summed along with the appropriate fraction of any other points we have in the region.
As the paper states, the spacing of the grid points is up to you. I assume it would need to be big enough to allow most polling points have at least one vector in its neighbourhood.
EDIT: Here is an example of what I mean.
(0,1) . _ _ _ _ _ . (1,1)
| |
| v |
| |
| |
(0,0) . _ _ _ _ _ . (1,0)
Let's say the vector v=[10,5] is at point (0.2,0.8)
point (0,0) gets weight 0.8*0.2=0.16, so we add 0.16*v = [1.6,0.8] to that pool
point (1,0) gets weight 0.2*0.2=0.04, so we add 0.04*v = [0.4,0.2] to that pool
point (0,1) gets weight 0.8*0.8=0.64, so we add 0.64*v = [6.4,3.2] to that pool
point (1,1) gets weight 0.2*0.8=0.16, so we add 0.16*v = [1.6,0.8] to that pool

Leading zeros calculation with intrinsic function

I'm trying to optimize some code working in an embedded system (FLAC decoding, Windows CE, ARM 926 MCU).
The default implementation uses a macro and a lookup table:
/* counts the # of zero MSBs in a word */
#define COUNT_ZERO_MSBS(word) ( \
(word) <= 0xffff ? \
( (word) <= 0xff? byte_to_unary_table[word] + 24 : \
byte_to_unary_table[(word) >> 8] + 16 ) : \
( (word) <= 0xffffff? byte_to_unary_table[word >> 16] + 8 : \
byte_to_unary_table[(word) >> 24] ) \
)
static const unsigned char byte_to_unary_table[] = {
8, 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};
However most CPU already have a dedicated instruction, bsr on x86 and clz on ARM (http://www.devmaster.net/articles/fixed-point-optimizations/), that should be more efficient.
On Windows CE we have the intrinsic function _CountLeadingZeros, that should just call that value. However it is 4 times slower than the macro (measured on 10 million of iterations).
How is possible that an intrinsic function, that (should) rely on a dedicated ASM instruction, is 4 times slower?
Check the disassembly. Are you sure that the compiler inserted the instruction? In the Remarks section there is this text:
This function can be implemented by
calling a runtime function.
I suspect that's what's happening in your case.
Note that the CLZ instruction is only available in ARMv5 and later. You need to tell the compiler if you want ARMv5 code:
/QRarch5 ARM5 Architecture
/QRarch5T ARM5T Architecture
(Microsoft incorrectly uses "ARM5" instead of "ARMv5")

Resources