def isr(self, pin):
# debounce
if time.ticks_diff(time.ticks_ms(), self.last_isr) < 10:
return
print('! reset gyro request')
self.flag_reset_gyro = True
self.last_isr = time.ticks_ms()
python类ticks_ms()的实例源码
def serve(self):
print('starting mpu server on port {}'.format(self.port))
lastgc = lastsent = lastread = time.ticks_ms()
while True:
now = time.ticks_ms()
write_dt = time.ticks_diff(now, lastsent)
read_dt = time.ticks_diff(now, lastread)
gc_dt = time.ticks_diff(now, lastgc)
time.sleep_ms(max(0, 1-read_dt))
if self.flag_reset_gyro:
self.mpu.filter.reset_gyro()
self.flag_reset_gyro = False
values = self.mpu.read_position()
lastread = now
if write_dt >= self.write_interval:
lastsent = time.ticks_ms()
self.sock.sendto(tojson(values), ('192.168.4.2', 8000))
if gc_dt >= self.gc_interval:
gc.collect()
def input(self, vals):
now = time.ticks_ms()
# unpack sensor readings
accel_data = vals[0:3]
gyro_data = vals[4:7]
# convert accelerometer reading to degrees
self.accel_pos = self.calculate_accel_pos(*accel_data)
# if this is our first chunk of data, simply accept
# the accelerometer reads and move on.
if self.last == 0:
self.filter_pos = self.gyro_pos = self.accel_pos
self.last = now
return
# calculate the elapsed time (in seconds) since last data.
# we need this because the gyroscope measures movement in
# degrees/second.
dt = time.ticks_diff(now, self.last)/1000
self.last = now
# calculate change in position from gyroscope readings
gyro_delta = [i * dt for i in gyro_data]
self.gyro_pos = [i + j for i, j in zip(self.gyro_pos, gyro_delta)]
# pitch
self.filter_pos[0] = (
self.gyro_weight * (self.filter_pos[0] + gyro_delta[0])
+ (1-self.gyro_weight) * self.accel_pos[0])
# roll
self.filter_pos[1] = (
self.gyro_weight * (self.filter_pos[1] + gyro_delta[1])
+ (1-self.gyro_weight) * self.accel_pos[1])
def makegauge(self):
'''
Generator refreshing the raw measurments.
'''
delays = (5, 8, 14, 25)
while True:
self._bmp_i2c.writeto_mem(self._bmp_addr, 0xF4, bytearray([0x2E]))
t_start = time.ticks_ms()
while (time.ticks_ms() - t_start) <= 5: # 5mS delay
yield None
try:
self.UT_raw = self._bmp_i2c.readfrom_mem(self._bmp_addr, 0xF6, 2)
except:
yield None
self._bmp_i2c.writeto_mem(self._bmp_addr, 0xF4, bytearray([0x34+(self.oversample_setting << 6)]))
t_pressure_ready = delays[self.oversample_setting]
t_start = time.ticks_ms()
while (time.ticks_ms() - t_start) <= t_pressure_ready:
yield None
try:
self.MSB_raw = self._bmp_i2c.readfrom_mem(self._bmp_addr, 0xF6, 1)
self.LSB_raw = self._bmp_i2c.readfrom_mem(self._bmp_addr, 0xF7, 1)
self.XLSB_raw = self._bmp_i2c.readfrom_mem(self._bmp_addr, 0xF8, 1)
except:
yield None
yield True
def waiting(self):
acum = time.ticks_ms()
delta = time.ticks_diff(time.ticks_ms(),acum)
while delta < 5000:
delta = time.ticks_diff(time.ticks_ms(), acum)
time.sleep_ms(5)
self.DO_SLEEP = True
print("Ordem para dormir")
def do_get(clisock, uri, content_length):
clisock.write(b'HTTP/1.0 200 OK\r\n'
b'Content-type: text/html; charset=utf-8\r\n'
b'\r\n')
clisock.write(b'<!DOCTYPE html><html><head><title>Current time</title></head>')
clisock.write(b'<body>The current time is: ')
timestr ='{}-{:02d}-{:02d} {:02d}:{:02d}:{:02d}'.format(*time.localtime())
clisock.write(timestr.encode('ascii'))
del timestr
uptime_s = int(time.ticks_ms() / 1000)
uptime_h = int(uptime_s / 3600)
uptime_m = int(uptime_s / 60)
uptime_m = uptime_m % 60
uptime_s = uptime_s % 60
clisock.write(b'<p>Uptime: {:02d}h {:02d}:{:02d}'.format(
uptime_h, uptime_m, uptime_s))
clisock.write(b'<p>Flash ID: {:x}'.format(esp.flash_id()))
clisock.write(b'<p>Flash size: {:d}'.format(esp.flash_size()))
clisock.write(b'<p>Python: {:s} on {:s} '.format(str(sys.implementation), sys.platform))
clisock.write(b'<p>Unique ID: ')
for b in machine.unique_id():
clisock.write(b'{:02x}'.format(b))
clisock.write(b'\n<h2>Network interfaces</h2>\n')
clisock.write(b'\n<table><tr><th>mac<th>active</th><th>connected</th><th>IP</th><th>Gateway</th>')
for i in network.STA_IF, network.AP_IF:
wlan = network.WLAN(i)
# Show MAC address.
clisock.write(b'<tr>')
clisock.write(b'<td>')
for b in wlan.config('mac'):
clisock.write(b'{:02X}'.format(b))
clisock.write(b'<td>{:}</td>'.format(wlan.active()))
clisock.write(b'<td>{:}</td>'.format(wlan.isconnected()))
ifconfig = wlan.ifconfig() #ip, netmask, gateway, dns
ifconfig = (ifconfig[0], ifconfig[2]) # ip, gw
for item in ifconfig:
clisock.write(b'<td>{:}</td>'.format(item))
clisock.write(b'\n</table>\n')