Skip to content

Commit 43baaa9

Browse files
committed
New test component added to coderbot triggerable via front-end
1 parent 32ab74d commit 43baaa9

File tree

6 files changed

+134
-9
lines changed

6 files changed

+134
-9
lines changed

api.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from coderbot import CoderBot
1414
from program import ProgramEngine, Program
1515
from config import Config
16+
from coderbotTestUnit import run_test as runCoderbotTestUnit
1617
import pigpio
1718

1819
BUTTON_PIN = 16
@@ -241,3 +242,7 @@ def reset():
241242
"status": "ok"
242243
}
243244

245+
## Test
246+
def testCoderbot(data):
247+
tests_state = runCoderbotTestUnit(data)
248+
return tests_state

coderbot.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,14 @@ def stop(self):
159159
def is_moving(self):
160160
return self._is_moving
161161

162+
# Distance travelled getter
163+
def distance(self):
164+
return self._twin_motors_enc.distance()
165+
166+
# CoderBot velocity getter
167+
def speed(self):
168+
return self._twin_motors_enc.speed()
169+
162170
def set_callback(self, gpio, callback, elapse):
163171
self._cb_elapse[gpio] = elapse * 1000
164172
self._cb[gpio] = callback

coderbotTestUnit.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
"""
2+
This file defines base tests for CoderBot
3+
in order to test its functionality
4+
5+
The function run_test(varargin) lanuches tests
6+
according to required test from the front-end.
7+
e.g. varagrin = ["motor_test", "sonar_test"]
8+
__test_encoder() and __test_sonar() will be launched.
9+
10+
If something goes wrong a -1 is returned for the correspondent failed test.
11+
If a test passes for correspondent component, a 1 is returned.
12+
If no test was executed on that component, 0 is preserved.
13+
"""
14+
# Single components tests
15+
16+
# encoder motors
17+
def __test_encoder():
18+
return 1
19+
20+
# sonar
21+
def __testSonar():
22+
return 1
23+
24+
# speaker
25+
def __test_speaker():
26+
return 1
27+
28+
# OCR
29+
def __test_OCR():
30+
return 1
31+
32+
# add more tests here
33+
34+
""" Main test function
35+
it launches tests to test single components individually.
36+
A dictionary is returned monitoring the state of the
37+
tests for each component.
38+
Varargin is a list of strings that indicates which test
39+
to run. """
40+
def run_test(varargin):
41+
# state for each executed test
42+
# 0 = component not tested
43+
# 1 = test passed
44+
# -1 = test failed
45+
tests_state = {
46+
"motors": 0,
47+
"sonar": 0,
48+
"speaker": 0,
49+
"OCR": 0
50+
# add more tests state here
51+
}
52+
53+
# running chosen tests
54+
for test in varargin:
55+
if(test == 'motors'):
56+
tests_state[test] = __test_encoder()
57+
elif(test == 'sonar'):
58+
tests_state[test] = __testSonar()
59+
elif (test == 'speaker'):
60+
tests_state[test] = __test_speaker()
61+
elif(test == 'OCR'):
62+
tests_state[test] = __test_OCR()
63+
#add more test cases here
64+
65+
return tests_state

rotary_encoder/motorencoder.py

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ def __init__(self, pi, enable_pin, forward_pin, backward_pin, feedback_pin_A, fe
3434

3535
# other
3636
self._motor_lock = threading.RLock()
37+
self._start_timer = 0
3738
self._rotary_decoder = RotaryDecoder(pi, feedback_pin_A, feedback_pin_B, self.rotary_callback)
3839

3940
# GETTERS
@@ -103,17 +104,23 @@ def stop(self):
103104
self._pi.write(self._backward_pin, 0)
104105
self._pi.write(self._forward_pin, 0)
105106

107+
# resetting wheel state
108+
self.reset_state()
109+
110+
# releasing lock
111+
self._motor_lock.release()
112+
113+
# stop auxiliary function, resets wheel state
114+
def reset_state(self):
106115
# returning state variables to consistent state
107-
self._distance = 0 # resetting distance travelled
108-
self._ticks = 0 # resetting ticks
109-
self._power = 0 # resetting PWM power
116+
self._distance = 0 # resetting distance travelled
117+
self._ticks = 0 # resetting ticks
118+
self._power = 0 # resetting PWM power
110119
self._encoder_speed = 0 # resetting encoder speed
111-
self._direction = 0 # resetting direction
120+
self._direction = 0 # resetting direction
121+
self._start_timer = 0
112122
self._is_moving = False # resetting moving flag
113123

114-
# releasing lock
115-
self._motor_lock.release()
116-
117124
# CALLBACK
118125
""" The callback function rotary_callback is called on FALLING_EDGE by the
119126
rotary_decoder with a parameter value of 1 (1 new tick)
@@ -132,7 +139,12 @@ def rotary_callback(self, tick):
132139
self._motor_lock.acquire()
133140
self._ticks += tick # updating ticks
134141
self._distance = self._ticks * 0.0981 # (mm) travelled
135-
#self._encoder_speed = (mm/s)
142+
143+
# velocity calculation
144+
elapse = time() - self._start_timer
145+
if(elapse != 0):
146+
self._encoder_speed = (self._distance / 1000) / elapse #(mm/s)
147+
136148
self._motor_lock.release()
137149

138150
# callback cancelling

rotary_encoder/wheelsaxel.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,24 +91,36 @@ def control_distance(self, power_left=100, power_right=100, target_distance=0):
9191
self._right_motor.control(power_right)
9292

9393
# moving for certaing amount of distance
94+
# threshold value avoid to stop it after
9495
while(abs(self.distance()) < target_distance):
9596
pass # busy waiting
9697

9798
# robot arrived
9899
self.stop()
99100

100101
""" Motor speed control to travel given distance
101-
in given time adjusting power on motors """
102+
in given time adjusting power on motors
103+
NOT very intuitive, idea has been postponed"""
102104
def control_velocity(self, time_elapse=0, target_distance=0):
103105
pass
104106

105107
""" The stop function calls the two stop functions of the two
106108
correspondent motors.
107109
Locks are automatically obtained """
108110
def stop(self):
111+
# stopping left and right motors
109112
self._left_motor.stop()
110113
self._right_motor.stop()
114+
115+
# trying to fix distance different than zero after
116+
# wheels has stopped by re-resetting state after 0.5s
117+
sleep(0.5)
118+
self._left_motor.reset_state()
119+
self._right_motor.reset_state()
120+
121+
# updating state
111122
self._is_moving = False
123+
# restoring callback
112124
self._wheelsAxle_lock.release()
113125

114126
# CALLBACK

v2.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,29 @@ paths:
179179
responses:
180180
200:
181181
description: "resets coderbot entirely"
182+
/testCoderbot:
183+
post:
184+
summary: Tests CoderBot components.
185+
operationId: "api.testCoderbot"
186+
tags:
187+
- CoderBot Test
188+
parameters:
189+
- in: body
190+
name: data
191+
description: Components names to be tested
192+
schema:
193+
type: object
194+
default: {'varargin': ['motors', 'sonar', 'speaker']}
195+
required:
196+
- varargin
197+
properties:
198+
speed:
199+
type: list
200+
description: Components names to be tested
201+
202+
responses:
203+
200:
204+
description: Test ended.
182205
/info:
183206
get:
184207
operationId: "api.info"

0 commit comments

Comments
 (0)