Beispiel #1
0
    def start(self):
        if self.recording:
            return
        print_debug("Start recording bike sensor")

        self.recording = True

        if not self.debug_sensor:
            if not self.previous_handled_time:
                self.previous_handled_time = current_millis()

            while self.recording:
                if not self._handle_new_state(GPIO.input(self.pin)):
                    if current_millis(
                    ) - self.previous_handled_time > self.idle_time:
                        if self.on_idle and not self.is_idle:
                            print_debug('idle')
                            self.is_idle = True
                            self.initialise()
                            self.on_idle(True)
                else:
                    self.previous_handled_time = current_millis()
        else:
            i = 0
            while self.recording:
                if self.is_idle:
                    self.is_idle = False
                    self.on_idle(False)
                if i % 2 != 0 and i != 0:
                    self._handle_new_state(1)
                else:
                    self._handle_new_state(0)
                i += 1
                sleep(0.5)
        print_debug("End recording bike sensor")
Beispiel #2
0
    def _handle_new_state(self, new_state):
        if self.paused:
            return

        handled_state = True

        increase_test_millis_by(750)

        if new_state != self.current_state and new_state == 1:
            if self.off_time == -1:
                if self.is_idle:
                    self.on_idle(False)
                    self.is_idle = False
                elif self.on_first_rev:
                    self.on_first_rev(-1)
                self.off_time = current_millis()
            else:
                time_in_seconds = (current_millis() - self.off_time) / 1000
                if self.on_revolution:
                    self.on_revolution(time_in_seconds)
                self.off_time = current_millis()
            sleep(0.2)
        else:
            handled_state = False
            sleep(0.1)
        self.current_state = new_state

        return handled_state
Beispiel #3
0
    def _calculate_values_from_rev(self, time_in_seconds):
        self.total_time += time_in_seconds
        distance_in_metres = properties.bike.revolution_distance
        metres_per_seconds = distance_in_metres / time_in_seconds
        metres_per_seconds_history.append(metres_per_seconds)
        average_metres_per_seconds = mean(metres_per_seconds_history)

        total_average_speed = (self.revolutions *
                               distance_in_metres) / self.total_time

        x = {
            "last_rev_time": time_in_seconds,
            "total_revs": self.revolutions,
            "distance": self.revolutions * distance_in_metres / 1000,
            "rpm": 60 / time_in_seconds,
            "current_speed": average_metres_per_seconds * 3.6,
            "total_average_speed": total_average_speed * 3.6,
            "gear": self.gear,
            "duration": self.duration,
            "time": current_millis() * 1000000
        }

        if self.heart_rate_updated and self.heart_rate > 0:
            x['heartrate'] = self.heart_rate
            self.heart_rate = 0

        print_debug(x)

        self.data_points.append(x)

        return x
Beispiel #4
0
 def on_revolution(self, t: float):
     if not self.paused:
         if t == -1 or self.start_time == -1:
             if self.start_time == -1:
                 self.start_timer()
                 self.start_time = current_millis()
         else:
             self.revolutions += 1
             if self.on_update:
                 self.on_update(self._calculate_values_from_rev(t))
Beispiel #5
0
 def publish_heartrate(self, heartrate):
     print_debug(f'Heartrate retrieved: {heartrate}')
     self.heart_rate = heartrate
     self.heart_rate_updated = current_millis()
Beispiel #6
0
 def on_bike_ride_started(self):
     self.start_time = current_millis()