|
| 1 | +import datetime |
| 2 | + |
| 3 | +import crc |
| 4 | +from utils import * |
| 5 | + |
| 6 | +START_OF_PACKET = 0xcc |
| 7 | +WIFI_MSG = 0x1a |
| 8 | +VIDEO_RATE_QUERY = 40 |
| 9 | +LIGHT_MSG = 53 |
| 10 | +FLIGHT_MSG = 0x56 |
| 11 | +LOG_MSG = 0x1050 |
| 12 | + |
| 13 | +VIDEO_ENCODER_RATE_CMD = 0x20 |
| 14 | +VIDEO_START_CMD = 0x25 |
| 15 | +EXPOSURE_CMD = 0x34 |
| 16 | +TIME_CMD = 70 |
| 17 | +STICK_CMD = 80 |
| 18 | +TAKEOFF_CMD = 0x0054 |
| 19 | +LAND_CMD = 0x0055 |
| 20 | +FLIP_CMD = 0x005c |
| 21 | + |
| 22 | + |
| 23 | +class Packet(object): |
| 24 | + def __init__(self, cmd, pkt_type=0x68): |
| 25 | + if isinstance(cmd, (bytearray, str)): |
| 26 | + self.buf = bytearray() |
| 27 | + self.buf[:] = cmd |
| 28 | + else: |
| 29 | + self.buf = bytearray([ |
| 30 | + chr(START_OF_PACKET), |
| 31 | + 0, 0, |
| 32 | + 0, |
| 33 | + chr(pkt_type), |
| 34 | + chr(cmd & 0xff), chr((cmd >> 8) & 0xff), |
| 35 | + 0, 0]) |
| 36 | + |
| 37 | + def fixup(self, seq_num=0): |
| 38 | + buf = self.get_buffer() |
| 39 | + if buf[0] == START_OF_PACKET: |
| 40 | + buf[1], buf[2] = le16(len(buf)+2) |
| 41 | + buf[1] = (buf[1] << 3) |
| 42 | + buf[3] = crc.crc8(buf[0:3]) |
| 43 | + buf[7], buf[8] = le16(seq_num) |
| 44 | + self.add_int16(crc.crc16(buf)) |
| 45 | + |
| 46 | + def get_buffer(self): |
| 47 | + return self.buf |
| 48 | + |
| 49 | + def get_data(self): |
| 50 | + return self.buf[9:len(self.buf)-2] |
| 51 | + |
| 52 | + def add_byte(self, val): |
| 53 | + self.buf.append(val & 0xff) |
| 54 | + |
| 55 | + def add_int16(self, val): |
| 56 | + self.add_byte(val) |
| 57 | + self.add_byte(val >> 8) |
| 58 | + |
| 59 | + def add_time(self, time=datetime.datetime.now()): |
| 60 | + self.add_int16(time.hour) |
| 61 | + self.add_int16(time.minute) |
| 62 | + self.add_int16(time.second) |
| 63 | + self.add_int16((time.microsecond/1000) & 0xff) |
| 64 | + self.add_int16(((time.microsecond/1000) >> 8) & 0xff) |
| 65 | + |
| 66 | + def get_time(self, buf=None): |
| 67 | + if buf is None: |
| 68 | + buf = self.get_data()[1:] |
| 69 | + hour = int16(buf[0], buf[1]) |
| 70 | + min = int16(buf[2], buf[3]) |
| 71 | + sec = int16(buf[4], buf[5]) |
| 72 | + millisec = int16(buf[6], buf[8]) |
| 73 | + now = datetime.datetime.now() |
| 74 | + return datetime.datetime(now.year, now.month, now.day, hour, min, sec, millisec) |
| 75 | + |
| 76 | + |
| 77 | +class FlightData(object): |
| 78 | + def __init__(self, data): |
| 79 | + self.battery_low = 0 |
| 80 | + self.battery_lower = 0 |
| 81 | + self.battery_percentage = 0 |
| 82 | + self.battery_state = 0 |
| 83 | + self.camera_state = 0 |
| 84 | + self.down_visual_state = 0 |
| 85 | + self.drone_battery_left = 0 |
| 86 | + self.drone_fly_time_left = 0 |
| 87 | + self.drone_hover = 0 |
| 88 | + self.em_open = 0 |
| 89 | + self.em_sky = 0 |
| 90 | + self.em_ground = 0 |
| 91 | + self.east_speed = 0 |
| 92 | + self.electrical_machinery_state = 0 |
| 93 | + self.factory_mode = 0 |
| 94 | + self.fly_mode = 0 |
| 95 | + self.fly_speed = 0 |
| 96 | + self.fly_time = 0 |
| 97 | + self.front_in = 0 |
| 98 | + self.front_lsc = 0 |
| 99 | + self.front_out = 0 |
| 100 | + self.gravity_state = 0 |
| 101 | + self.ground_speed = 0 |
| 102 | + self.height = 0 |
| 103 | + self.imu_calibration_state = 0 |
| 104 | + self.imu_state = 0 |
| 105 | + self.light_strength = 0 |
| 106 | + self.north_speed = 0 |
| 107 | + self.outage_recording = 0 |
| 108 | + self.power_state = 0 |
| 109 | + self.pressure_state = 0 |
| 110 | + self.smart_video_exit_mode = 0 |
| 111 | + self.temperature_height = 0 |
| 112 | + self.throw_fly_timer = 0 |
| 113 | + self.wifi_disturb = 0 |
| 114 | + self.wifi_strength = 0 |
| 115 | + self.wind_state = 0 |
| 116 | + |
| 117 | + if len(data) < 24: |
| 118 | + return |
| 119 | + |
| 120 | + self.height = int16(data[0], data[1]) |
| 121 | + self.north_speed = int16(data[2], data[3]) |
| 122 | + self.east_speed = int16(data[4], data[5]) |
| 123 | + self.ground_speed = int16(data[6], data[7]) |
| 124 | + self.fly_time = int16(data[8], data[9]) |
| 125 | + |
| 126 | + self.imu_state = ((data[10] >> 0) & 0x1) |
| 127 | + self.pressure_state = ((data[10] >> 1) & 0x1) |
| 128 | + self.down_visual_state = ((data[10] >> 2) & 0x1) |
| 129 | + self.power_state = ((data[10] >> 3) & 0x1) |
| 130 | + self.battery_state = ((data[10] >> 4) & 0x1) |
| 131 | + self.gravity_state = ((data[10] >> 5) & 0x1) |
| 132 | + self.wind_state = ((data[10] >> 7) & 0x1) |
| 133 | + |
| 134 | + self.imu_calibration_state = data[11] |
| 135 | + self.battery_percentage = data[12] |
| 136 | + self.drone_battery_left = int16(data[13], data[14]) |
| 137 | + self.drone_fly_time_left = int16(data[15], data[16]) |
| 138 | + |
| 139 | + self.em_sky = ((data[17] >> 0) & 0x1) |
| 140 | + self.em_ground = ((data[17] >> 1) & 0x1) |
| 141 | + self.em_open = ((data[17] >> 2) & 0x1) |
| 142 | + self.drone_hover = ((data[17] >> 3) & 0x1) |
| 143 | + self.outage_recording = ((data[17] >> 4) & 0x1) |
| 144 | + self.battery_low = ((data[17] >> 5) & 0x1) |
| 145 | + self.battery_lower = ((data[17] >> 6) & 0x1) |
| 146 | + self.factory_mode = ((data[17] >> 7) & 0x1) |
| 147 | + |
| 148 | + self.fly_mode = data[18] |
| 149 | + self.throw_fly_timer = data[19] |
| 150 | + self.camera_state = data[20] |
| 151 | + self.electrical_machinery_state = data[21] |
| 152 | + |
| 153 | + self.front_in = ((data[22] >> 0) & 0x1) |
| 154 | + self.front_out = ((data[22] >> 1) & 0x1) |
| 155 | + self.front_lsc = ((data[22] >> 2) & 0x1) |
| 156 | + |
| 157 | + self.temperature_height = ((data[23] >> 0) & 0x1) |
| 158 | + |
| 159 | + def __str__(self): |
| 160 | + return ( |
| 161 | + ("height=%2d" % self.height) + |
| 162 | + (", fly_mode=0x%02x" % self.fly_mode) + |
| 163 | + (", battery_percentage=%2d" % self.battery_percentage) + |
| 164 | + (", drone_battery_left=0x%04x" % self.drone_battery_left) + |
| 165 | + "") |
0 commit comments