Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396

397

398

399

400

401

402

403

404

405

406

407

408

409

# -*- coding: utf-8 -*- 

"""The Roomba Janitoo helper 

It handle all communications to the Roomba vacuum 

 

 

 

""" 

 

__license__ = """ 

    This file is part of Janitoo. 

 

    Janitoo is free software: you can redistribute it and/or modify 

    it under the terms of the GNU General Public License as published by 

    the Free Software Foundation, either version 3 of the License, or 

    (at your option) any later version. 

 

    Janitoo is distributed in the hope that it will be useful, 

    but WITHOUT ANY WARRANTY; without even the implied warranty of 

    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 

    GNU General Public License for more details. 

 

    You should have received a copy of the GNU General Public License 

    along with Janitoo. If not, see <http://www.gnu.org/licenses/>. 

 

""" 

__author__ = 'Sébastien GALLET aka bibi21000' 

__email__ = 'bibi21000@gmail.com' 

__copyright__ = "Copyright © 2013-2014-2015 Sébastien GALLET aka bibi21000" 

 

# Set default logging handler to avoid "No handler found" warnings. 

import logging 

logger = logging.getLogger('janitoo.roomba') 

import os, sys 

import threading 

import requests 

from datetime import datetime, timedelta 

from janitoo.thread import JNTBusThread 

from janitoo.options import get_option_autostart 

from janitoo.utils import HADD, HADD_SEP, json_dumps, json_loads 

from janitoo.node import JNTNode 

from janitoo.value import JNTValue, value_config_poll 

from janitoo.bus import JNTBus 

from janitoo.component import JNTComponent 

 

############################################################## 

#Check that we are in sync with the official command classes 

#Must be implemented for non-regression 

from janitoo.classes import COMMAND_DESC 

 

COMMAND_METER = 0x0032 

COMMAND_ROOMBA_VACUUM = 0x2000 

 

assert(COMMAND_DESC[COMMAND_METER] == 'COMMAND_METER') 

assert(COMMAND_DESC[COMMAND_ROOMBA_VACUUM] == 'COMMAND_ROOMBA_VACUUM') 

############################################################## 

 

def make_thread(options): 

    if get_option_autostart(options, 'roomba') == True: 

        return RoombaThread(options) 

    else: 

        return None 

 

def make_vacuum(**kwargs): 

    return RoombaVacuum(**kwargs) 

 

commands = { 

    "clean":135, 

    "dock":143, 

    "full":132, 

    "max":136, 

    "play":141, 

    "spot":143, 

    "start":128, 

    "spot":134, 

    "LED_COMMAND":139, 

    "SENSOR_COMMAND":142, 

    "SENSOR_PACKET_0":0, 

    "SENSOR_PACKET_1":1, 

    "SENSOR_PACKET_2":2, 

    "SENSOR_PACKET_3":3, 

    "NUM_BYTES_PACKET_0":26, 

    } 

 

sensors = { 

    "Bumps Wheeldrops":0, 

    "Wall":0, 

    "Cliff Left":0, 

    "Cliff Front Left":0, 

    "Cliff Front Right":0, 

    "Cliff Right":0, 

    "Virtual Wall":0, 

    "Motor Overcurrents":0, 

    "Dirt Detector - Left":0, 

    "Dirt Detector - Right":0, 

    "Remote Opcode":0, 

    "Buttons":0, 

    "Distance":0, 

    "Angle":0, 

    "State":0, 

    "Voltage":0, 

    "Current":0, 

    "Temperature":0, 

    "Charge":0, 

    "Capacity":0, 

    "Battery-level":0, 

    } 

 

states = { 

    0:"Not charging", 

    1:"Charging Recovery", 

    2:"Charging", 

    3:"Trickle charging", 

    4:"Waiting", 

    5:"Charging Error", 

    6:"Cleaning", 

    7:"Docking", 

    8:"Maximun", 

    } 

 

class RoombaThread(JNTBusThread): 

    """The Roomba thread 

    """ 

 

    def init_bus(self): 

        """Build the bus 

        """ 

        self.section = 'roomba' 

        self.bus = JNTBus(options=self.options, oid=self.section, product_name="Roomba controller") 

 

class RoombaVacuum(JNTComponent): 

    """This class abstracts a roowifi and gives attributes for telemetry data, 

    as well as methods to command the robot 

    """ 

    def __init__(self, bus=None, addr=None, **kwargs): 

        """ 

        """ 

        JNTComponent.__init__(self, 'roomba.vacuum', bus=bus, addr=addr, name="Roomba Vacuum series", 

                product_name="Roomba Vacuum", product_type="Roowifi", product_manufacturer="IRobot", **kwargs) 

        logger.debug("[%s] - __init__ node uuid:%s", self.__class__.__name__, self.uuid) 

        self._lock = threading.Lock() 

        self._current = -1.0 

        self._voltage = -1.0 

        self._charge = -1.0 

        self._capacity = -1.0 

        self._battery = -1.0 

        self._temperature = -1.0 

        self._state = -1.0 

        self.telemetry_ttl = 60 

        self._telemetry_last = False 

        self._telemetry_next_run = datetime.now() + timedelta(seconds=15) 

 

        uuid = "current" 

        self.values[uuid] = self.value_factory['sensor_current'](options=self.options, uuid=uuid, 

            node_uuid=self.uuid, 

            help='The dock current', 

            label='Current', 

            units='mA', 

            get_data_cb=self.get_current, 

        ) 

        poll_value = self.values[uuid].create_poll_value(default=60) 

        self.values[poll_value.uuid] = poll_value 

 

        uuid = "voltage" 

        self.values[uuid] = self.value_factory['sensor_voltage'](options=self.options, uuid=uuid, 

            node_uuid=self.uuid, 

            help='The dock voltage', 

            label='Voltage', 

            units='V', 

            get_data_cb=self.get_voltage, 

        ) 

        poll_value = self.values[uuid].create_poll_value(default=60) 

        self.values[poll_value.uuid] = poll_value 

 

        uuid = "battery_current" 

        self.values[uuid] = self.value_factory['sensor_current'](options=self.options, uuid=uuid, 

            node_uuid=self.uuid, 

            help='The current of the battery', 

            label='Charge', 

            units='mA', 

            get_data_cb=self.get_battery_charge, 

        ) 

        poll_value = self.values[uuid].create_poll_value(default=60) 

        self.values[poll_value.uuid] = poll_value 

 

        uuid = "battery_capacity" 

        self.values[uuid] = self.value_factory['sensor_current'](options=self.options, uuid=uuid, 

            node_uuid=self.uuid, 

            help='The capacity of the battery', 

            label='Capacity', 

            units='mA', 

            get_data_cb=self.get_battery_capacity, 

        ) 

        poll_value = self.values[uuid].create_poll_value(default=60) 

        self.values[poll_value.uuid] = poll_value 

 

        uuid = "battery_charge" 

        self.values[uuid] = self.value_factory['sensor_percent'](options=self.options, uuid=uuid, 

            node_uuid=self.uuid, 

            help='The charge of the battery', 

            label='Percent', 

            get_data_cb=self.get_battery_percent, 

        ) 

        poll_value = self.values[uuid].create_poll_value(default=60) 

        self.values[poll_value.uuid] = poll_value 

 

        uuid = "ip_ping" 

        self.values[uuid] = self.value_factory['ip_ping'](options=self.options, uuid=uuid, 

            node_uuid=self.uuid, 

            help='Ping the vacuum', 

            label='Ping', 

        ) 

        config_value = self.values[uuid].create_config_value(help='The IP of the vacuum', label='IP',) 

        self.values[config_value.uuid] = config_value 

        poll_value = self.values[uuid].create_poll_value(default=60) 

        self.values[poll_value.uuid] = poll_value 

 

        uuid = "username" 

        self.values[uuid] = self.value_factory['config_string'](options=self.options, uuid=uuid, 

            node_uuid=self.uuid, 

            help='Username to connect the roomba', 

            label='Username', 

        ) 

 

        uuid = "password" 

        self.values[uuid] = self.value_factory['config_password'](options=self.options, uuid=uuid, 

            node_uuid=self.uuid, 

            help='Password to connect the roomba', 

            label='Password', 

        ) 

 

        uuid = "temperature" 

        self.values[uuid] = self.value_factory['sensor_temperature'](options=self.options, uuid=uuid, 

            node_uuid=self.uuid, 

            help='The temperature of the roomba', 

            label='Temperature', 

            get_data_cb=self.get_temperature, 

        ) 

        poll_value = self.values[uuid].create_poll_value(default=60) 

        self.values[poll_value.uuid] = poll_value 

 

        uuid = "dock" 

        self.values[uuid] = self.value_factory['sensor_string'](options=self.options, uuid=uuid, 

            node_uuid=self.uuid, 

            help='The state of the roomba dock', 

            label='Dock', 

            get_data_cb=self.get_state, 

        ) 

        poll_value = self.values[uuid].create_poll_value(default=60) 

        self.values[poll_value.uuid] = poll_value 

 

        uuid = "buttons" 

        self.values[uuid] = self.value_factory['action_list'](options=self.options, uuid=uuid, 

            node_uuid=self.uuid, 

            help='The buttons on the roomba', 

            label='Buttons', 

            list_items=['clean', 'spot', 'dock', 'idle'], 

            set_data_cb=self.set_button, 

            is_writeonly = True, 

            cmd_class = COMMAND_ROOMBA_VACUUM, 

        ) 

 

    def check_heartbeat(self): 

        """Check that the component is 'available' 

 

        """ 

        #~ print "it's me %s : %s" % (self.values['upsname'].data, self._ups_stats_last) 

        return self._telemetry_last 

 

    def get_telemetry(self): 

        """Roomba method which fetches telemetry data about the robot. 

 

        Other values that can be implemented: 

 

        - sensors['Bumps Wheeldrops'] = self.j['response']['r0']['value'] 

        - sensors['Wall'] = self.j['response']['r1']['value'] 

        - sensors['Cliff Left'] = self.j['response']['r2']['value'] 

        - sensors['Cliff Front Left'] = self.j['response']['r3']['value'] 

        - sensors['Cliff Front Right'] = self.j['response']['r4']['value'] 

        - sensors['Cliff Right'] = self.j['response']['r5']['value'] 

        - sensors['Virtual Wall'] = self.j['response']['r6']['value'] 

        - sensors['Motor Overcurrents'] = self.j['response']['r7']['value'] 

        - sensors['Dirt Detector - Left'] = self.j['response']['r8']['value'] 

        - sensors['Dirt Detector - Right'] = self.j['response']['r9']['value'] 

        - sensors['Remote Opcode'] = self.j['response']['r10']['value'] 

        - sensors['Buttons'] = self.j['response']['r11']['value'] 

        - sensors['Distance'] = self.j['response']['r12']['value'] 

        - sensors['Angle'] = self.j['response']['r13']['value'] 

        - sensors['State'] = State[int(self.j['response']['r14']['value'])] 

        - sensors['Voltage'] = self.j['response']['r15']['value'] 

        - sensors['Current'] = self.j['response']['r16']['value'] 

        - sensors['Temperature'] = self.j['response']['r17']['value'] 

        - sensors['Charge'] = self.j['response']['r18']['value'] 

        - sensors['Capacity'] = self.j['response']['r19']['value'] 

        - sensors['battery-level'] =  int(self.j['response']['r18']['value'])*100 / int (self.j['response']['r19']['value']) 

        """ 

        if self._telemetry_next_run < datetime.now(): 

            locked = self._lock.acquire(False) 

            if locked == True: 

                try: 

                    auth = (self.values['username'].data, self.values['password'].data) 

                    r = requests.get('http://' + self.values['ip_ping_config'].data + '/roomba.json', auth=auth) 

                    self._telemetry = json_loads(r.text) 

                    logger.debug("[%s] - retrieve telemetry : %s", self.__class__.__name__, self._telemetry) 

                    self._temperature = float(self._telemetry['response']['r17']['value']) 

                    self._charge = float(self._telemetry['response']['r18']['value']) 

                    self._capacity = float(self._telemetry['response']['r19']['value']) 

                    self._state = int(self._telemetry['response']['r14']['value']) 

                    self._voltage = round(float(self._telemetry['response']['r15']['value'])/1000,2) 

                    self._current = round(float(self._telemetry['response']['r16']['value']),2) 

                    try: 

                        self._battery = round(100.0 * self._charge / self._capacity, 2) 

                    except ZeroDivisionError: 

                        self._battery = -1.0 

                    self._telemetry_last = True 

                except: 

                    logger.exception("[%s] - Exception in get_telemetry", self.__class__.__name__) 

                    self._telemetry_last = False 

                finally: 

                    self._lock.release() 

                    logger.debug("And finally release the lock !!!") 

                if self.values['ip_ping_poll'].data>0: 

                    self._telemetry_next_run = datetime.now() + timedelta(seconds=self.values['ip_ping_poll'].data) 

 

    def get_battery_charge(self, node_uuid, index): 

        """Return the battery 

        """ 

        self.get_telemetry() 

        return self._charge 

 

    def get_battery_capacity(self, node_uuid, index): 

        """Return the battery 

        """ 

        self.get_telemetry() 

        return self._capacity 

 

    def get_battery_percent(self, node_uuid, index): 

        """Return the battery charge 

        """ 

        self.get_telemetry() 

        return self._battery 

 

    def get_temperature(self, node_uuid, index): 

        """Return the temperture 

        """ 

        self.get_telemetry() 

        return self._temperature 

 

    def get_current(self, node_uuid, index): 

        """Return the current 

        """ 

        self.get_telemetry() 

        return self._current 

 

    def get_voltage(self, node_uuid, index): 

        """Return the voltage 

        """ 

        self.get_telemetry() 

        return self._voltage 

 

    def get_state(self, node_uuid, index): 

        """Return the dock state 

        """ 

        self.get_telemetry() 

        if self._state in states: 

            return states[self._state] 

        return "Unknown" 

 

    def set_button(self, node_uuid, index, data): 

        """Return the dock state 

        """ 

        auth = (self.values['username'].data, self.values['password'].data) 

        params = {} 

        if data == "clean": 

            params['exec'] = 4 

        elif data == "spot": 

            params['exec'] = 5 

        elif data == "dock": 

            params['exec'] = 6 

        elif data == "idle": 

            params['exec'] = 1 

        r = requests.get('http://' + self.values['ip_ping_config'].data + '/rwr.cgi', params = params, auth=auth) 

 

    def command(self, ip, port, device, command): 

        """Other way to acces the roowifi using a simple tcp socket. 

        A simple copy paste ... does not work 

        """ 

        self._log.info("Start processing clean Command on %s  " % (device)) 

        try: 

            self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 

            time.sleep(1) 

            self.s.connect((ip , port)) 

            time.sleep(1) 

            self.s.send(chr(132)) 

            time.sleep(1) 

            #print("Le code de la fonction est %s  " % table[command]) 

            self.s.send(chr(table[command])) 

            time.sleep(1) 

            self.s.close() 

            self._log.info("%s command Success on %s" % (command,device)) 

            if str(command) == "clean": 

                sensors['State'] = State[int(6)] 

            if str(command) == "dock" : 

                sensors['State'] =  State[int(7)] 

            if str(command) == "max" : 

                sensors['State'] =  State[int(8)] 

            return True 

        except: 

            self._log.error(" %s Command Failed on %s" % (command,device)) 

            return False