Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/pybot/functions.py
blob: 71dbf42199863eed283cfe81582b0ecdc4e4ff17 (plain)
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
#! /usr/bin/env python
# -*- coding: utf-8 -*-
#
# Abstract class with common functions
#
# Copyright (c) 2012-2013 Alan Aguiar alanjas@hotmail.com
# Copyright (c) 2012-2013 Butiá Team butia@fing.edu.uy 
# Butia is a free and open robotic platform
# www.fing.edu.uy/inco/proyectos/butia
# Facultad de Ingeniería - Universidad de la República - Uruguay
#
# This program 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 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA


class ButiaFunctions:

    def isPresent(self, module_name):
        """
        Check if module: module_name is present
        """
        module_list = self.get_modules_list()
        return (module_name in module_list)

    def loopBack(self, data, board='0'):
        """
        LoopBack command: send data to the board and get the result. If all is ok
        the return must be exactly of the data parameter
        """
        msg = [str(data)]
        return self.callModule('lback', str(board), '0', 'send', msg, ret_type=str)

    ############################## Movement calls ##############################

    def set2MotorSpeed(self, leftSense='0', leftSpeed='0', rightSense='0', rightSpeed='0', board='0'):
        """
        Set the speed of 2 motors. The sense is 0 or 1, and the speed is
        between 0 and 1023
        """
        msg = [str(leftSense), str(leftSpeed), str(rightSense), str(rightSpeed)]
        return self.callModule('motors', str(board), '0', 'setvel2mtr', msg)

    def setMotorSpeed(self, idMotor='0', sense='0', speed='0', board='0'):
        """
        Set the speed of one motor. idMotor = 0 for left motor and 1 for the
        right motor. The sense is 0 or 1, and the speed is between 0 and 1023
        """
        msg = [str(idMotor), str(sense), str(speed)]
        return self.callModule('motors', str(board), '0', 'setvelmtr', msg)

    def getMotorType(self, board='0'):
        """
        If AX-12 motors present returns 1. If there are a shield "cc" returns 2
        """
        return self.callModule('motors', str(board), '0', 'getType')

    ##################### Operations for ax.lua driver #########################

    def writeInfo(self, idMotor, regstart, value, board='0'):
        """
        Writes the motor: idMotor in the registry: regstart with value: value
        """
        msg = [str(idMotor), str(regstart), str(value)]
        return self.callModule('ax', str(board), '0', 'writeInfo', msg)

    def readInfo(self, idMotor, regstart, length='1', board='0'):
        """
        Reads the motor: idMotor in the registry: regstart
        """
        msg = [str(idMotor), str(regstart), str(length)]
        return self.callModule('ax', str(board), '0', 'writeInfo', msg)

    def sendPacket(self, msg, board='0'):
        """
        Send a raw packet to ax module
        """
        msg_s = [str(i) for i in msg]
        return self.callModule('ax', str(board), '0', 'sendPacket', msg_s, ret_type=str)

    def wheelMode(self, idMotor='0', board='0'):
        """
        Sets the motor: idMotor in wheel mode (continuos rotation)
        """
        msg = [str(idMotor)]
        return self.callModule('ax', str(board), '0', 'wheelMode', msg)
     
    def jointMode(self, idMotor='0', _min='0', _max='1023', board='0'):
        """
        Sets the motor: idMotor in servo mode
        """
        msg = [str(idMotor), str(_min), str(_max)]
        return self.callModule('ax', str(board), '0', 'jointMode', msg)

    def setPosition(self, idMotor='0', pos='0', board='0'):
        """
        Sets the position: pos of the motor: idMotor
        """
        msg = [str(idMotor), str(pos)]
        return self.callModule('ax', str(board), '0', 'setPosition', msg)

    def getPosition(self, idMotor='0', board='0'):
        """
        Gets the position of motor: idMotor
        """
        msg = [str(idMotor)]
        return self.callModule('ax', str(board), '0', 'getPosition', msg)

    def setSpeed(self, idMotor='0', speed='0', board='0'):
        """
        Set the speed: speed to the motor: idMotor
        """
        msg = [str(idMotor), str(speed)]
        return self.callModule('ax', str(board), '0', 'setSpeed', msg)

    ############################### General calls ##############################
     
    def getBatteryCharge(self, board='0'):
        """
        Gets the battery level charge
        """
        return self.callModule('butia', str(board), '0', 'getVolt', ret_type=float)

    def getVersion(self, board='0'):
        """
        Gets the version of Butiá module. 22 for new version
        """
        return self.callModule('butia', str(board), '0', 'getVersion')

    def getFirmwareVersion(self, board='0'):
        """
        Gets the version of the Firmware
        """
        return self.callModule('admin', str(board), '0', 'getVersion')

    ############################### Sensors calls ###############################

    def getButton(self, port, board='0'):
        """
        Gets the value of the button connected in port
        """
        return self.callModule('button', str(board), str(port), 'getValue')
    
    def getLight(self, port, board='0'):
        """
        Gets the value of the light sensor connected in port
        """
        return self.callModule('light', str(board), str(port), 'getValue')

    def getDistance(self, port, board='0'):
        """
        Gets the value of the distance sensor connected in port
        """
        return self.callModule('distanc', str(board), str(port), 'getValue')

    def getGray(self, port, board='0'):
        """
        Gets the value of the gray sensor connected in port
        """
        return self.callModule('grey', str(board), str(port), 'getValue')

    def getResistance(self, port, board='0'):
        """
        Gets the value of the resistance sensor connected in port
        """
        return self.callModule('res', str(board), str(port), 'getValue', ret_type=float)

    def getVoltage(self, port, board='0'):
        """
        Gets the value of the voltage sensor connected in port
        """
        return self.callModule('volt', str(board), str(port), 'getValue', ret_type=float)

    def getTemperature(self, port, board='0'):
        """
        Gets the value of the temperature sensor connected in port
        """
        return self.callModule('temp', str(board), str(port), 'getValue', ret_type=float)

    def setLed(self, port, on_off, board='0'):
        """
        Sets on or off the LED connected in port (0 is off, 1 is on)
        """
        return self.callModule('led', str(board), str(port), 'turn', [str(on_off)])

    ################################ Extras ################################

    def modeHack(self, pin, mode, board='0'):
        """
        Sets the mode of hack pin. If mode 0 = input, mode 1 = output
        """
        msg = [str(pin), str(mode)]
        return self.callModule('hackp', str(board), '0', 'setMode', msg)

    def setHack(self, pin, value, board='0'):
        """
        Sets the value of hack pin configured as output. Value is 0 or 1
        """
        msg = [str(pin), str(value)]
        return self.callModule('hackp', str(board), '0', 'write', msg)

    def getHack(self, pin, board='0'):
        """
        Gets the value of hack pin configured as input. Returns 0 or 1
        """
        return self.callModule('hackp', str(board), '0', 'read', [str(pin)])

    ############################# Generic modules #############################

    def getModuleA(self, port, board='0'):
        """
        Gets the value of the generic sensor ModuleA connected in port
        """
        return self.callModule('moduleA', str(board), str(port), 'getValue')

    def getModuleB(self, port, board='0'):
        """
        Gets the value of the generic sensor ModuleB connected in port
        """
        return self.callModule('moduleB', str(board), str(port), 'getValue')

    def getModuleC(self, port, board='0'):
        """
        Gets the value of the generic sensor ModuleC connected in port
        """
        return self.callModule('moduleC', str(board), str(port), 'getValue')

    def setModuleA(self, port, on_off, board='0'):
        """
        Sets on or off the generic module A
        """
        return self.callModule('modSenA', str(board), str(port), 'turn', [str(on_off)])

    def setModuleB(self, port, on_off, board='0'):
        """
        Sets on or off the generic module B
        """
        return self.callModule('modSenB', str(board), str(port), 'turn', [str(on_off)])

    def setModuleC(self, port, on_off, board='0'):
        """
        Sets on or off the generic module C
        """
        return self.callModule('modSenC', str(board), str(port), 'turn', [str(on_off)])