got it so that reading usb packets reliably works "sometimes"
diff --git a/aos/common/sensors/sensor_receiver-tmpl.h b/aos/common/sensors/sensor_receiver-tmpl.h
index dc9f17d..e3840c4 100644
--- a/aos/common/sensors/sensor_receiver-tmpl.h
+++ b/aos/common/sensors/sensor_receiver-tmpl.h
@@ -6,7 +6,7 @@
template<class Values>
const time::Time SensorReceiver<Values>::kJitterDelay =
- time::Time::InSeconds(0.0035);
+ time::Time::InSeconds(0.00225);
// Not a multiple of kSensorSendFrequency to unwedge ourself if we hit some bug
// where it needs to get off of that frequency to work.
template<class Values>
@@ -185,6 +185,7 @@
}
}
+ Synchronized(goal_time + kLoopFrequency * kTestCycles);
return true;
}
@@ -197,13 +198,6 @@
int old_count = data_.count;
DoReceiveData();
- data_.checksum = ntoh(data_.checksum);
- if (!data_.CheckChecksum()) {
- LOG(WARNING, "got a bad packet\n");
- return ReceiveData();
- }
-
- data_.NetworkToHost();
if (data_.count < 0) {
LOG(FATAL, "data count overflowed. currently %"PRId32"\n", data_.count);
}
@@ -251,6 +245,13 @@
while (true) {
if (socket_.Receive(this->data(), sizeof(*this->data())) ==
sizeof(*this->data())) {
+ this->data()->checksum = ntoh(this->data()->checksum);
+ if (!this->data()->CheckChecksum()) {
+ LOG(WARNING, "got a bad packet\n");
+ continue;
+ }
+
+ this->data()->NetworkToHost();
return;
}
LOG(WARNING, "received incorrect amount of data\n");
diff --git a/aos/common/sensors/sensor_receiver.h b/aos/common/sensors/sensor_receiver.h
index c829d53..9cd24e1 100644
--- a/aos/common/sensors/sensor_receiver.h
+++ b/aos/common/sensors/sensor_receiver.h
@@ -53,6 +53,8 @@
// Subclasses need to implement this to read 1 set of data (blocking until it
// is available) into data().
+ // It needs to have the correct byte order etc and not be corrupted
+ // (subclasses can check the checksum if they want).
virtual void DoReceiveData() = 0;
// Optional: if subclasses can do anything to reinitialize after there are
@@ -60,6 +62,10 @@
// This will be called right before calling DoReceiveData() the first time.
virtual void Reset() {}
+ // Optional: if subclasses want to be notified when this is first convinced
+ // that it has a good packet, they should do whatever here.
+ virtual void Synchronized(time::Time /*last_packet_ideal_time*/) {}
+
// Returns whether the current packet looks like a good one to use.
bool GoodPacket();
diff --git a/aos/common/time.cc b/aos/common/time.cc
index 995011a..75ea196 100644
--- a/aos/common/time.cc
+++ b/aos/common/time.cc
@@ -120,6 +120,9 @@
const Time Time::operator/(int32_t rhs) const {
return Time(*this) /= rhs;
}
+double Time::operator/(const Time &rhs) const {
+ return ToSeconds() / rhs.ToSeconds();
+}
Time &Time::operator%=(int32_t rhs) {
nsec_ = ToNSec() % rhs;
const int wraps = nsec_ / kNSecInSec;
diff --git a/aos/common/time.h b/aos/common/time.h
index ea703f7..276cf1c 100644
--- a/aos/common/time.h
+++ b/aos/common/time.h
@@ -160,6 +160,8 @@
const Time operator-(const Time &rhs) const;
const Time operator*(int32_t rhs) const;
const Time operator/(int32_t rhs) const;
+ // TODO(brians) test this
+ double operator/(const Time &rhs) const;
const Time operator%(int32_t rhs) const;
bool operator==(const Time &rhs) const;