diff options
Diffstat (limited to 'robot/problems/introduction')
51 files changed, 2289 insertions, 43 deletions
diff --git a/robot/problems/introduction/circle20/common.py b/robot/problems/introduction/circle20/common.py new file mode 100644 index 0000000..661d43a --- /dev/null +++ b/robot/problems/introduction/circle20/common.py @@ -0,0 +1,62 @@ +# coding=utf-8 + +from python.util import has_token_sequence, string_almost_equal, \ + string_contains_number, get_tokens, get_numbers, get_exception_desc +from server.hints import Hint, HintSequence + +id = 205 +group = 'introduction' +number = 5 +visible = True + +solution = '''\ +from ev3dev import * +from mindstorms_widgets import mindstorms_widgets + +robot = mindstorms_widgets() +robot.connect_motor( 'left' ) +robot.connect_motor( 'right' ) + +fct = 9 # full circle time +rad = 20 # Direction to make a good radius for the circle +robot.move_steering( 'on_for_seconds', direction=rad, power=40, seconds=fct ) +''' + +hint_type = { + 'mW_init': Hint('mW_init'), + 'connectMotorLeft': Hint('connectMotorLeft'), + 'connectMotorRight': Hint('connectMotorRight'), + 'moveSteering': Hint('moveSteering'), + 'onForSeconds': Hint('onForSeconds'), + 'direction': Hint('direction'), + 'seconds': Hint('seconds'), +} + +def hint( code): + tokens = get_tokens(code) + + # if code does not include mindstorms_widgets(), a student gets a hint that the robot should be somehow represented in the program + if not has_token_sequence(tokens, ['mindstorms_widgets', '(',')']): + return [{'id': 'mW_init'}] + + # if code does not include connect_motor statement, a student needs to learn about how to connect the motors + if not has_token_sequence(tokens, ['connect_motor']) and not has_token_sequence(tokens, ['left']): + return [{'id': 'connectMotorLeft'}] + + # if code does not include connect_motor statement, a student needs to learn about how to connect the motors + if not has_token_sequence(tokens, ['connect_motor']) and not has_token_sequence(tokens, ['right']): + return [{'id': 'connectMotorRight'}] + + if not has_token_sequence(tokens, ['move_steering']): + return [{'id': 'moveSteering'}] + + if not 'on_for_seconds' in code: + return [{'id': 'onForSeconds'}] + + if not 'direction' in code: + return [{'id': 'direction'}] + + if not 'seconds' in code: + return [{'id': 'seconds'}] + + return None diff --git a/robot/problems/introduction/circle20/en.py b/robot/problems/introduction/circle20/en.py new file mode 100644 index 0000000..8210d71 --- /dev/null +++ b/robot/problems/introduction/circle20/en.py @@ -0,0 +1,14 @@ +# coding=utf-8 +import server +mod = server.problems.load_language('python', 'en') + +id = 205 +name = 'Circle 20 cm' +slug = 'Circle 20 cm' + +description = '''\ +''' + +hint = { + +} diff --git a/robot/problems/introduction/circle20/naloga05_circle20.py b/robot/problems/introduction/circle20/naloga05_circle20.py new file mode 100644 index 0000000..e95d4f4 --- /dev/null +++ b/robot/problems/introduction/circle20/naloga05_circle20.py @@ -0,0 +1,13 @@ +#!/usr/bin/python + +import time +from ev3dev import * +from mindstorms_widgets import mindstorms_widgets + +robot = mindstorms_widgets() +robot.connect_motor( 'left' ) +robot.connect_motor( 'right' ) + +fct = 9 # full circle time +rad = 20 # Direction to make a good radius for the circle +robot.move_steering( 'on_for_seconds', direction=rad, power=40, seconds=fct )
\ No newline at end of file diff --git a/robot/problems/introduction/circle20/sl.py b/robot/problems/introduction/circle20/sl.py new file mode 100644 index 0000000..219f9c7 --- /dev/null +++ b/robot/problems/introduction/circle20/sl.py @@ -0,0 +1,26 @@ +# coding=utf-8 + +id = 205 +name = 'Krog 20 cm' +slug = 'Krog 20 cm' + +description = '''\ +<p>Napiši program, s katerim bo robot enkrat prevozil krog s polmerom 20 cm in se nato ustavil.</p>''' + +hint = { + 'mW_init':['''<p>Robota v programu predstavimo z mindstorms_widgets(): <code>robot = mindstorms_widgets()</code>.</p>'''], + 'connectMotorLeft':['''<p>Robotu priključi levi motor</p>''', + '''<p><code>robot.connect_motor( 'left' )</code>.</p>'''], + 'connectMotorRight':['''<p>Robotu priključi desni motor</p>''', + '''<p><code>robot.connect_motor( 'right' )</code>.</p>'''], + 'moveSteering':['''<p>Sinhroniziraj motorja in ju zaženi za 3 sekunde.</p>''', + '''<p>Za sinhronizirano vožnjo je najbolj primerna metoda <code>robot.move_steering( ... )</code>.</p>'''], + 'onForSeconds':['''<p>Prvi argument metode <code>robot.move_steering</code> naj pove, da bo delovanje motorjev časovno omejeno.</p>''', + '''<p><code>robot.move_steering('on_for_seconds', ... )</code>.</p>'''], + 'direction':['''<p>Navedi smer premikanja motorjev; pozitivne vrednosti pomenijo zavoj v desno.</p>''', + '''<p>Določi vrednost parametra direction tako, da bo robot krožil po krogu s polmerom 20cm. Vrednost je odvisna od konstrukcije robota in podlage.</p>''', + '''<p><code>robot.move_steering( 'on_for_seconds', direction=20, ... )</code>.</p>'''], + 'seconds':['''<p>Napiši časovno omejitev v sekundah.</p>''', + '''<p>Za dani polmer izmeri čas, ki ga robot potrebuje za en obhod.</p>''', + '''<p><code>robot.move_steering( 'on_for_seconds', direction=0, seconds=3 )</code>.</p>'''], +} diff --git a/robot/problems/introduction/countlines/common.py b/robot/problems/introduction/countlines/common.py new file mode 100644 index 0000000..29f5383 --- /dev/null +++ b/robot/problems/introduction/countlines/common.py @@ -0,0 +1,79 @@ +# coding=utf-8 + +from python.util import has_token_sequence, string_almost_equal, \ + string_contains_number, get_tokens, get_numbers, get_exception_desc +from server.hints import Hint, HintSequence + +id = 206 +group = 'introduction' +number = 10 +visible = True + +solution = '''\ +import time +from ev3dev import * +from mindstorms_widgets import mindstorms_widgets + +color_table = ['none', 'black', 'blue', 'green', 'yellow', 'red', 'white', 'brown'] + +robot = mindstorms_widgets() +robot.connect_motor( 'left' ) +robot.connect_motor( 'right' ) +robot.connect_sensor( 'color' ) + +robot.move_steering( 'on' ) +start = time.time() +stevec = 0 +color = -1 +while time.time()-start < 2.1: + c = robot.color_sensor_measure( 'color' ) # values: 0-7 see the scale above + if c!=color: + if c!=6 and c!=0: + stevec += 1 + color = c +robot.move_steering( 'off' ) +print( "Stevilo crt:", stevec ) +''' + +hint_type = { + 'mW_init': Hint('mW_init'), + 'connectMotorLeft': Hint('connectMotorLeft'), + 'connectMotorRight': Hint('connectMotorRight'), + 'moveSteeringOn': Hint('moveSteeringOn'), + 'moveSteeringOff': Hint('moveSteeringOff'), + 'connectColorSensor': Hint('connectColorSensor'), + 'colorSensorMeasure': Hint('colorSensorMeasure'), + 'while': Hint('while') +} + +def hint( code ): + tokens = get_tokens(code) + + # if code does not include mindstorms_widgets(), a student gets a hint that the robot should be somehow represented in the program + if not has_token_sequence(tokens, ['mindstorms_widgets', '(',')']): + return [{'id': 'mW_init'}] + + # if code does not include connect_motor statement, a student needs to learn about how to connect the motors + if not has_token_sequence(tokens, ['connect_motor']) and not has_token_sequence(tokens, ['left']): + return [{'id': 'connectMotorLeft'}] + + # if code does not include connect_motor statement, a student needs to learn about how to connect the motors + if not has_token_sequence(tokens, ['connect_motor']) and not has_token_sequence(tokens, ['right']): + return [{'id': 'connectMotorRight'}] + + if not (has_token_sequence(tokens, ['connect_sensor', '(' ]) and 'color' in code): + return [{'id': 'connectColorSensor'}] + + if not (has_token_sequence(tokens, ['move_steering', '(']) and 'on' in code): + return [{'id': 'moveSteeringOn'}] + + if not (has_token_sequence(tokens, ['move_steering', '(']) and 'off' in code): + return [{'id': 'moveSteeringOff'}] + + if not (has_token_sequence(tokens, ['color_sensor_measure', '(']) and 'color' in code): + return [{'id': 'colorSensorMeasure'}] + + if not has_token_sequence(tokens, ['while']): + return [{'id': 'while'}] + + return None diff --git a/robot/problems/introduction/countlines/en.py b/robot/problems/introduction/countlines/en.py new file mode 100644 index 0000000..8b3b275 --- /dev/null +++ b/robot/problems/introduction/countlines/en.py @@ -0,0 +1,14 @@ +# coding=utf-8 +import server +mod = server.problems.load_language('python', 'en') + +id = 206 +name = 'Count lines' +slug = 'Count lines' + +description = '''\ +''' + +hint = { + +} diff --git a/robot/problems/introduction/countlines/naloga10_countLines.py b/robot/problems/introduction/countlines/naloga10_countLines.py new file mode 100644 index 0000000..d1a8326 --- /dev/null +++ b/robot/problems/introduction/countlines/naloga10_countLines.py @@ -0,0 +1,37 @@ +#!/usr/bin/python +# coding=utf-8 + +print """ +Naloga 10: + Robot naj se pelje naravnost, pravokotno na crte in naj jih presteje. +""" + +import time +from ev3dev import * +from mindstorms_widgets import mindstorms_widgets + +color_table = ['none', 'black', 'blue', 'green', 'yellow', 'red', 'white', 'brown'] + +cs = color_sensor() +assert cs.connected +cs.mode = 'COL-COLOR' # values: 0-7 see the scale above + +robot = mindstorms_widgets() +robot.connect_motor( 'left' ) +robot.connect_motor( 'right' ) + +power = 30 + +robot.move_tank( 'on', lr_power=[power,power]) +start = time.time() +stevec = 0 +color = -1 +while time.time()-start < 2.1: + c = cs.value() + if c!=color: + print c, color_table[c] + if c!=6 and c!=0: + stevec += 1 + color = c +robot.move_tank( 'off', brake_at_end=True ) +print "Stevilo crt:", stevec
\ No newline at end of file diff --git a/robot/problems/introduction/countlines/sl.py b/robot/problems/introduction/countlines/sl.py new file mode 100644 index 0000000..4e1c3d4 --- /dev/null +++ b/robot/problems/introduction/countlines/sl.py @@ -0,0 +1,26 @@ +# coding=utf-8 + +name = 'Preštej črte' +slug = 'Preštej črte' + +description = '''\ +<p>Robot naj pelje naravnost, pravokotno na črte in naj jih prešteje.</p>''' + +hint = { + 'mW_init':['''<p>Robota v programu predstavimo z mindstorms_widgets(): <code>robot = mindstorms_widgets()</code>.</p>'''], + 'connectMotorLeft':['''<p>Robotu priključi levi motor</p>''', + '''<p><code>robot.connect_motor( 'left' )</code>.</p>'''], + 'connectMotorRight':['''<p>Robotu priključi desni motor</p>''', + '''<p><code>robot.connect_motor( 'right' )</code>.</p>'''], + 'moveSteeringOn':['''<p>Sinhroniziraj motorja in ju zaženi.</p>''', + '''<p>Za sinhronizirano vožnjo je najbolj primerna metoda <code>robot.move_steering( 'on' )</code>.</p>'''], + 'moveSteeringOff':['''<p>Ustavi motorja.</p>''', + '''<p><code>robot.move_steering( 'off' )</code>.</p>'''], + 'connectColorSensor':['''<p>Robotu moramo priključiti barvni senzor.</p>''', + '''<p><code>robot.connect_sensor( 'color' )</code>.</p>'''], + 'colorSensorMeasure':['''<p>Medtem ko se robot pomika naprej, naj uporabi barvni senzor v načinu 'color', s katerim pove, katero barvo trenutno vidi.</p>''', + '''<p><code>robot.color_sensor_measure( 'color' )</code>.</p>'''], + 'while':['''<p>Uporabi zanko, znotraj katere robot odčitava barve in povečuje števec.</p>''', + '''<p>Zanka je lahko časovno omejena, npr. z uporabo metode <code>time.time()</code>.</p>''', + '''<p><code>while time.time()-start < 1.1:</code>.</p>'''], +} diff --git a/robot/problems/introduction/followline/common.py b/robot/problems/introduction/followline/common.py new file mode 100644 index 0000000..d61d4c1 --- /dev/null +++ b/robot/problems/introduction/followline/common.py @@ -0,0 +1,85 @@ +# coding=utf-8 + +from python.util import has_token_sequence, string_almost_equal, \ + string_contains_number, get_tokens, get_numbers, get_exception_desc +from server.hints import Hint, HintSequence + +id = 207 +group = 'introduction' +number = 12 +visible = True + +solution = '''\ +import time +from ev3dev import * +from mindstorms_widgets import mindstorms_widgets + +robot = mindstorms_widgets() +robot.connect_motor( 'left' ) +robot.connect_motor( 'right' ) +robot.connect_sensor( 'color' ) + +start = time.time() +while time.time()-start < 10: + if robot.color_sensor_measure('reflected_light_intensity') < 30: + L, R = 0, 20 + else: + L, R = 20, 0 + robot.move_tank( 'on', lr_power=[L,R]) +robot.move_tank( 'off' ) +''' + +hint_type = { + 'mW_init': Hint('mW_init'), + 'connectMotorLeft': Hint('connectMotorLeft'), + 'connectMotorRight': Hint('connectMotorRight'), + 'moveTankOn': Hint('moveTankOn'), + 'lrPower': Hint('lr_power'), + 'moveTankOff': Hint('moveTankOff'), + 'connectColorSensor': Hint('connectColorSensor'), + 'colorSensorMeasureRLI': Hint('colorSensorMeasureRLI'), + 'while': Hint('while'), + 'time': Hint('time'), + 'if': Hint('if') +} + +def hint( code): + tokens = get_tokens(code) + + # if code does not include mindstorms_widgets(), a student gets a hint that the robot should be somehow represented in the program + if not has_token_sequence(tokens, ['mindstorms_widgets', '(',')']): + return [{'id': 'mW_init'}] + + # if code does not include connect_motor statement, a student needs to learn about how to connect the motors + if not has_token_sequence(tokens, ['connect_motor']) and not has_token_sequence(tokens, ['left']): + return [{'id': 'connectMotorLeft'}] + + # if code does not include connect_motor statement, a student needs to learn about how to connect the motors + if not has_token_sequence(tokens, ['connect_motor']) and not has_token_sequence(tokens, ['right']): + return [{'id': 'connectMotorRight'}] + + if not (has_token_sequence(tokens, ['connect_sensor', '(' ]) and 'color' in code): + return [{'id': 'connectColorSensor'}] + + if not (has_token_sequence(tokens, ['move_steering', '(']) and 'on' in code): + return [{'id': 'moveTankOn'}] + + if not 'lr_power' in code: + return [{'id': 'lrPower'}] + + if not (has_token_sequence(tokens, ['move_steering', '(']) and 'off' in code): + return [{'id': 'moveTankOff'}] + + if not (has_token_sequence(tokens, ['color_sensor_measure', '(']) and 'reflected_light_intensity' in code): + return [{'id': 'colorSensorMeasureRLI'}] + + if not has_token_sequence(tokens, ['while']): + return [{'id': 'while'}] + + if not has_token_sequence(tokens, ['time']): + return [{'id': 'time'}] + + if not has_token_sequence(tokens, ['if']): + return [{'id': 'if'}] + + return None diff --git a/robot/problems/introduction/followline/en.py b/robot/problems/introduction/followline/en.py new file mode 100644 index 0000000..3f816fe --- /dev/null +++ b/robot/problems/introduction/followline/en.py @@ -0,0 +1,14 @@ +# coding=utf-8 +import server +mod = server.problems.load_language('python', 'en') + +id = 207 +name = 'Line following' +slug = 'Line following' + +description = '''\ +''' + +hint = { + +} diff --git a/robot/problems/introduction/followline/naloga12_followLine.py b/robot/problems/introduction/followline/naloga12_followLine.py new file mode 100644 index 0000000..347e89e --- /dev/null +++ b/robot/problems/introduction/followline/naloga12_followLine.py @@ -0,0 +1,31 @@ +#!/usr/bin/python +# coding=utf-8 + +print( """ +Naloga 12: + Robot naj 10 sekund sledi crni crti na beli podlagi; pri tem naj si pomaga z barvnim senzorjem. +""") +import sys +sys.path.append('/home/user/codeq') +import time +from ev3dev import * +from mindstorms_widgets import mindstorms_widgets + +robot = mindstorms_widgets() +robot.connect_motor( 'left' ) +robot.connect_motor( 'right' ) +robot.connect_sensor( 'color' ) + +start = time.time() +# CALIBRATION +# print 'black', robot.color_sensor_measure('reflected_light_intensity') +# print "move to white" +# time.sleep(5) +# print 'white', robot.color_sensor_measure('reflected_light_intensity') +while time.time()-start < 10: + if robot.color_sensor_measure('reflected_light_intensity') < 30: + L, R = 0, 20 + else: + L, R = 20, 0 + robot.move_tank( 'on', lr_power=[L,R]) +robot.move_tank( 'off' ) diff --git a/robot/problems/introduction/followline/sl.py b/robot/problems/introduction/followline/sl.py new file mode 100644 index 0000000..848fe2b --- /dev/null +++ b/robot/problems/introduction/followline/sl.py @@ -0,0 +1,34 @@ +# coding=utf-8 + +name = 'Sledenje črti' +slug = 'Sledenje črti' + +description = '''\ +<p>Robot naj 10 sekund sledi črni črti na beli podlagi; pri tem naj si pomaga z barvnim senzorjem.</p>''' + +hint = { + 'mW_init':['''<p>Robota v programu predstavimo z mindstorms_widgets(): <code>robot = mindstorms_widgets()</code>.</p>'''], + 'connectMotorLeft':['''<p>Robotu priključi levi motor</p>''', + '''<p><code>robot.connect_motor( 'left' )</code>.</p>'''], + 'connectMotorRight':['''<p>Robotu priključi desni motor</p>''', + '''<p><code>robot.connect_motor( 'right' )</code>.</p>'''], + + 'moveTankOn':['''<p>Uporabi metodo za ločen nadzor motorjev, s katero vsakemu od motorjev nastavljaš drugačno moč.</p>''', + '''<p>Za to je najbolj primerna metoda <code>robot.move_tank( ... )</code>.</p>''', + '''<p><code>robot.move_tank( 'on', ...)</code></p>'''], + 'lrPower': ['''<p>Nastavi parameter s katerim nastaviš moči levega in desnega motorja.</p>''', + '''<p><code>robot.move_tank( 'on', lr_power=[ ... ])</code></p>'''], + 'moveTankOff': ['''Ustavi robota.''', + '''<p><code>robot.move_tank( 'off' )</code></p>'''], + 'connectColorSensor':['''<p>Robotu moramo priključiti barvni senzor.</p>''', + '''<p><code>robot.connect_sensor( 'color' )</code>.</p>'''], + 'colorSensorMeasureRLI':['''<p>Medtem ko se robot pomika naprej, naj uporabi barvni senzor v načinu 'reflected_light_intensity', za zaznavanje jakosti odbite svetlobe.</p>''', + '''<p><code>robot.color_sensor_measure( 'reflected_light_intensity' )</code>.</p>'''], + 'while':['''<p>Uporabi zanko, znotraj katere robot sledi črti.</p>''', + '''<p>Zanka naj bo časovno omejena, npr. z uporabo metode <code>time.time()</code>.</p>''', + '''<p><code>start = time.time()\nwhile time.time()-start < 10:</code>.</p>'''], + 'time': ['''Uporabi metodo time() za merjenje časa.''', + '''<p><code>start = time.time()\nwhile time.time()-start < 10:</code></p>'''], + 'if': ['''<p>V zanki uporabi pogojni stavek...</p>''', + '''<p>Če robot vidi črto, naj zavije z nje; če vidi podlago, naj zavije proti črti.</p>'''] +} diff --git a/robot/problems/introduction/forward/common.py b/robot/problems/introduction/forward/common.py index c82b0eb..b3fa8a9 100644 --- a/robot/problems/introduction/forward/common.py +++ b/robot/problems/introduction/forward/common.py @@ -1,32 +1,59 @@ # coding=utf-8 -from server.hints import Hint +from python.util import has_token_sequence, string_almost_equal, \ + string_contains_number, get_tokens, get_numbers, get_exception_desc +from server.hints import Hint, HintSequence id = 202 number = 1 visible = True solution = '''\ -import time from ev3dev import * from mindstorms_widgets import mindstorms_widgets robot = mindstorms_widgets() -robot.connect_motor('left') -robot.connect_motor('right') +robot.connect_motor( 'left' ) +robot.connect_motor( 'right' ) -robot.move_steering('on', power=50) -time.sleep(3) -robot.move_steering('off', brake_at_end=True) +robot.move_steering( 'on_for_seconds', direction=0, seconds=3 ) ''' hint_type = { - 'ev3dev': Hint('ev3dev'), - 'mindWidgets': Hint('mindWidgets'), + 'mW_init': Hint('mW_init'), + 'connectMotorLeft': Hint('connectMotorLeft'), + 'connectMotorRight': Hint('connectMotorRight'), + 'moveSteering': Hint('moveSteering'), + 'onForSeconds': Hint('onForSeconds'), + 'direction': Hint('direction'), + 'seconds': Hint('seconds'), } -def test(program): - return False, [] +def hint( code): + tokens = get_tokens(code) -def hint(program): - return [] + # if code does not include mindstorms_widgets(), a student gets a hint that the robot should be somehow represented in the program + if not has_token_sequence(tokens, ['mindstorms_widgets', '(',')']): + return [{'id': 'mW_init'}] + + # if code does not include connect_motor statement, a student needs to learn about how to connect the motors + if not has_token_sequence(tokens, ['connect_motor']) and not has_token_sequence(tokens, ['left']): + return [{'id': 'connectMotorLeft'}] + + # if code does not include connect_motor statement, a student needs to learn about how to connect the motors + if not has_token_sequence(tokens, ['connect_motor']) and not has_token_sequence(tokens, ['right']): + return [{'id': 'connectMotorRight'}] + + if not has_token_sequence(tokens, ['move_steering']): + return [{'id': 'moveSteering'}] + + if not 'on_for_seconds' in code: + return [{'id': 'onForSeconds'}] + + if not 'direction' in code: + return [{'id': 'direction'}] + + if not 'seconds' in code: + return [{'id': 'seconds'}] + + return None diff --git a/robot/problems/introduction/forward/en.py b/robot/problems/introduction/forward/en.py index 7c229b0..be73921 100644 --- a/robot/problems/introduction/forward/en.py +++ b/robot/problems/introduction/forward/en.py @@ -1,23 +1,12 @@ # coding=utf-8 -name = 'Forward' -slug = 'Forward' +name = 'Forward 3s' +slug = 'Forward 3s' description = '''\ -<p>Robot should drive forward in a straight line and stop after three -seconds.</p> +<p>Write a program that would make the robot drive straight forward for 3 seconds and stop</p> ''' -plan = [] - hint = { - 'ev3dev': '''\ -<p>To work with the robot you should import the ev3dev module: -<code>from ev3dev import *</code>.</p> -''', - - 'mindWidgets': '''\ -<p>To work with the robot you should import the mindstorms_widgets module: -<code>from mindstorms_widgets import mindstorms_widgets</code>.</p> -''', + } diff --git a/robot/problems/introduction/forward/sl.py b/robot/problems/introduction/forward/sl.py index c7c7eee..a6f7e86 100644 --- a/robot/problems/introduction/forward/sl.py +++ b/robot/problems/introduction/forward/sl.py @@ -1,22 +1,23 @@ # coding=utf-8 -name = 'Naprej' -slug = 'Naprej' +name = 'Naprej 3s' +slug = 'Naprej 3s' description = '''\ -<p>Robot naj pelje naravnost naprej in se po treh sekundah ustavi.</p> -''' - -plan = [] +<p>Napiši program, da bo robot peljal naravnost naprej 3 sekunde in se nato ustavil.</p>''' hint = { - 'ev3dev': '''\ -<p>Za delo z robotom moramo uvoziti modul ev3dev: -<code>from ev3dev import *</code>.</p> -''', - - 'mindWidgets': '''\ -<p>Za delo z robotom moramo uvoziti modul mindstorms_widgets: -<code>from mindstorms_widgets import mindstorms_widgets</code>.</p> -''', + 'mW_init':['''<p>Robota v programu predstavimo z mindstorms_widgets(): <code>robot = mindstorms_widgets()</code>.</p>'''], + 'connectMotorLeft':['''<p>Robotu priključi levi motor</p>''', + '''<p><code>robot.connect_motor( 'left' )</code>.</p>'''], + 'connectMotorRight':['''<p>Robotu priključi desni motor</p>''', + '''<p><code>robot.connect_motor( 'right' )</code>.</p>'''], + 'moveSteering':['''<p>Sinhroniziraj motorja in ju zaženi za 3 sekunde.</p>''', + '''<p>Za sinhronizirano vožnjo je najbolj primerna metoda <code>robot.move_steering( ... )</code>.</p>'''], + 'onForSeconds':['''<p>Prvi argument metode <code>robot.move_steering</code> naj pove, da bo delovanje motorjev časovno omejeno.</p>''', + '''<p><code>robot.move_steering('on_for_seconds', ... )</code>.</p>'''], + 'direction':['''<p>Navedi smer premikanja motorjev, naravnost = 0.</p>''', + '''<p><code>robot.move_steering( 'on_for_seconds', direction=0, ... )</code>.</p>'''], + 'seconds':['''<p>Napiši časovno omejitev v sekundah.</p>''', + '''<p><code>robot.move_steering( 'on_for_seconds', direction=0, seconds=3 )</code>.</p>'''], } diff --git a/robot/problems/introduction/forward1m/common.py b/robot/problems/introduction/forward1m/common.py new file mode 100644 index 0000000..ea2f7a0 --- /dev/null +++ b/robot/problems/introduction/forward1m/common.py @@ -0,0 +1,60 @@ +# coding=utf-8 + +from python.util import has_token_sequence, string_almost_equal, \ + string_contains_number, get_tokens, get_numbers, get_exception_desc +from server.hints import Hint, HintSequence + +id = 208 +group = 'introduction' +number = 2 +visible = True + +solution = '''\ +from ev3dev import * +from mindstorms_widgets import mindstorms_widgets + +robot = mindstorms_widgets() +robot.connect_motor( 'left' ) +robot.connect_motor( 'right' ) + +robot.move_steering( 'on_for_rotations', direction=0, rotations=5.71 ) +''' + +hint_type = { + 'mW_init': Hint('mW_init'), + 'connectMotorLeft': Hint('connectMotorLeft'), + 'connectMotorRight': Hint('connectMotorRight'), + 'moveSteering': Hint('moveSteering'), + 'onForRotations': Hint('onForRotations'), + 'direction': Hint('direction'), + 'rotations': Hint('rotations'), +} + +def hint(code): + tokens = get_tokens(code) + + # if code does not include mindstorms_widgets(), a student gets a hint that the robot should be somehow represented in the program + if not has_token_sequence(tokens, ['mindstorms_widgets', '(',')']): + return [{'id': 'mW_init'}] + + # if code does not include connect_motor statement, a student needs to learn about how to connect the motors + if not has_token_sequence(tokens, ['connect_motor']) and not has_token_sequence(tokens, ['left']): + return [{'id': 'connectMotorLeft'}] + + # if code does not include connect_motor statement, a student needs to learn about how to connect the motors + if not has_token_sequence(tokens, ['connect_motor']) and not has_token_sequence(tokens, ['right']): + return [{'id': 'connectMotorRight'}] + + if not has_token_sequence(tokens, ['move_steering']): + return [{'id': 'moveSteering'}] + + if not 'on_for_rotations' in code: + return [{'id': 'onForRotations'}] + + if not 'direction' in code: + return [{'id': 'direction'}] + + if not 'rotations' in code: + return [{'id': 'rotations'}] + + return None diff --git a/robot/problems/introduction/forward1m/en.py b/robot/problems/introduction/forward1m/en.py new file mode 100644 index 0000000..bdf2a45 --- /dev/null +++ b/robot/problems/introduction/forward1m/en.py @@ -0,0 +1,15 @@ +# coding=utf-8 +import server +mod = server.problems.load_language('python', 'en') + +id = 208 +name = 'Forward 1m' +slug = 'Forward 1m' + +description = '''\ + +''' + +hint = { + +} diff --git a/robot/problems/introduction/forward1m/sl.py b/robot/problems/introduction/forward1m/sl.py new file mode 100644 index 0000000..8045d58 --- /dev/null +++ b/robot/problems/introduction/forward1m/sl.py @@ -0,0 +1,23 @@ +# coding=utf-8 + +name = 'Naprej 1m' +slug = 'Naprej 1m' + +description = '''\ +<p>Napiši program, da bo robot peljal naravnost naprej 3 sekunde in se nato ustavil.</p>''' + +hint = { + 'mW_init':['''<p>Robota v programu predstavimo z mindstorms_widgets(): <code>robot = mindstorms_widgets()</code>.</p>'''], + 'connectMotorLeft':['''<p>Robotu priključi levi motor</p>''', + '''<p><code>robot.connect_motor( 'left' )</code>.</p>'''], + 'connectMotorRight':['''<p>Robotu priključi desni motor</p>''', + '''<p><code>robot.connect_motor( 'right' )</code>.</p>'''], + 'moveSteering':['''<p>Sinhroniziraj motorja in ju zaženi za 3 sekunde.</p>''', + '''<p>Za sinhronizirano vožnjo je najbolj primerna metoda <code>robot.move_steering( ... )</code>.</p>'''], + 'onForRotations':['''<p>Prvi argument metode <code>robot.move_steering</code> naj pove, da bo delovanje motorjev določeno s številom obratov.</p>''', + '''<p><code>robot.move_steering('on_for_rotations', ... )</code>.</p>'''], + 'direction':['''<p>Navedi smer premikanja motorjev, naravnost = 0.</p>''', + '''<p><code>robot.move_steering( 'on_for_rotations', direction=0, ... )</code>.</p>'''], + 'rotations':['''<p>Določi število obratov; za koliko obratov naj se zavrtita motorja? Izmeri dolžino 1m, izpisuj obrate <code>print(robot.motor['left'].count_per_rot, robot.motor['right'].count_per_rot)</code> in tako določi ustrezno število obratov.</p>''', + '''<p><code>robot.move_steering( 'on_for_rotations', direction=0, rotations=5 )</code>.</p>'''], +} diff --git a/robot/problems/introduction/gyro90/common.py b/robot/problems/introduction/gyro90/common.py new file mode 100644 index 0000000..b44d15d --- /dev/null +++ b/robot/problems/introduction/gyro90/common.py @@ -0,0 +1,80 @@ +# coding=utf-8 + +from python.util import has_token_sequence, string_almost_equal, \ + string_contains_number, get_tokens, get_numbers, get_exception_desc +from server.hints import Hint, HintSequence + +id = 209 +group = 'introduction' +number = 7 +visible = True + +solution = '''\ +from ev3dev import * +from mindstorms_widgets import mindstorms_widgets + +robot = mindstorms_widgets() +robot.connect_motor( 'left' ) +robot.connect_motor( 'right' ) +robot.connect_sensor( 'gyro' ) + +robot.gyro_set_mode( 'angle' ) +robot.reset_gyro() + +power = 15 +robot.move_tank( 'on', lr_power=[power,-power] ) +while robot.gyro_sensor_measure() < 90: + pass +robot.move_tank( 'off' ) +''' + +hint_type = { + 'mW_init': Hint('mW_init'), + 'connectMotorLeft': Hint('connectMotorLeft'), + 'connectMotorRight': Hint('connectMotorRight'), + 'connectGyro': Hint('connectGyro'), + 'resetGyro': Hint('resetGyro'), + 'setGyroMode': Hint('setGyroMode'), + 'gyroMeasure': Hint('gyroMeasure'), + 'moveTank': Hint('moveTank'), + 'lrPower': Hint('lrPower'), + 'while': Hint('while') +} + +def hint( code): + tokens = get_tokens(code) + + # if code does not include mindstorms_widgets(), a student gets a hint that the robot should be somehow represented in the program + if not has_token_sequence(tokens, ['mindstorms_widgets', '(',')']): + return [{'id': 'mW_init'}] + + # if code does not include connect_motor statement, a student needs to learn about how to connect the motors + if not has_token_sequence(tokens, ['connect_motor']) and not has_token_sequence(tokens, ['left']): + return [{'id': 'connectMotorLeft'}] + + # if code does not include connect_motor statement, a student needs to learn about how to connect the motors + if not has_token_sequence(tokens, ['connect_motor']) and not has_token_sequence(tokens, ['right']): + return [{'id': 'connectMotorRight'}] + + if not (has_token_sequence(tokens, ['connect_gyro','(']) and 'gyro' in code): + return [{'id': 'connectGyro'}] + + if not has_token_sequence(tokens, ['reset_gyro()']): + return [{'id': 'resetGyro'}] + + if not has_token_sequence(tokens, ['gyro_set_mode']): + return [{'id': 'setGyroMode'}] + + if not has_token_sequence(tokens, ['gyro_sensor_measure()']): + return [{'id': 'gyroMeasure'}] + + if not has_token_sequence(tokens, ['move_tank']): + return [{'id': 'moveTank'}] + + if not 'lr_power' in code: + return [{'id': 'lrPower'}] + + if not has_token_sequence(tokens, ['while']): + return [{'id': 'while'}] + + return None diff --git a/robot/problems/introduction/gyro90/en.py b/robot/problems/introduction/gyro90/en.py new file mode 100644 index 0000000..12c782c --- /dev/null +++ b/robot/problems/introduction/gyro90/en.py @@ -0,0 +1,15 @@ +# coding=utf-8 +import server +mod = server.problems.load_language('python', 'en') + +id = 209 +name = 'Gyro 90' +slug = 'Gyro 90' + +description = '''\ + +''' + +hint = { + +} diff --git a/robot/problems/introduction/gyro90/naloga07_gyro90.py b/robot/problems/introduction/gyro90/naloga07_gyro90.py new file mode 100644 index 0000000..84d4ca1 --- /dev/null +++ b/robot/problems/introduction/gyro90/naloga07_gyro90.py @@ -0,0 +1,25 @@ +#!/usr/bin/python +# coding=utf-8 + +print (""" +Naloga 7: + Robot naj se na mestu obrne za 90 stopinj v desno; pri tem naj si pomaga z ziroskopom. +""") +import sys +sys.path.append('/home/user/codeq') +from ev3dev import * +from mindstorms_widgets import mindstorms_widgets + +robot = mindstorms_widgets() +robot.connect_motor( 'left' ) +robot.connect_motor( 'right' ) +robot.connect_sensor( 'gyro' ) + +robot.gyro_set_mode( 'angle' ) +robot.reset_gyro() + +power = 15 +robot.move_tank( 'on', lr_power=[power,-power] ) +while robot.gyro_sensor_measure() < 90: + pass +robot.move_tank( 'off' ) diff --git a/robot/problems/introduction/gyro90/sl.py b/robot/problems/introduction/gyro90/sl.py new file mode 100644 index 0000000..0a82c6d --- /dev/null +++ b/robot/problems/introduction/gyro90/sl.py @@ -0,0 +1,29 @@ +# coding=utf-8 + +name = 'Žiroskop 90' +slug = 'Žiroskop 90' + +description = '''\ +<p>Robot naj se na mestu obrne za 90 stopinj v desno; pri tem naj si pomaga z žiroskopom.</p>''' + +hint = { + 'mW_init':['''<p>Robota v programu predstavimo z mindstorms_widgets(): <code>robot = mindstorms_widgets()</code>.</p>'''], + 'connectMotorLeft':['''<p>Robotu priključi levi motor.</p>''', + '''<p><code>robot.connect_motor( 'left' )</code>.</p>'''], + 'connectMotorRight':['''<p>Robotu priključi desni motor.</p>''', + '''<p><code>robot.connect_motor( 'right' )</code>.</p>'''], + 'connectGyro':['''<p>Robotu priključi žiroskop.</p>''', + '''<p><code>robot.connect_sensor( 'gyro' )</code>.</p>'''], + 'resetGyro':['''<p>Žiroskop na začetku resetiraj.</p>''', + '''<p><code>robot.reset_gyro()</code>.</p>'''], + 'setGyroMode':['''<p>Žiroskopu nastavi način delovanja na merjenje kotov.</p>''', + '''<p><code>robot.gyro_set_mode('angle')</code>.</p>'''], + 'gyroMeasure':['''<p>Preberi vrednost žiroskopa.</p>''', + '''<p><code>robot.gyro_sensor_measure()</code>.</p>'''], + 'lrPower':['''<p>Za obrat na mestu se morata kolesi vrteti z enako močjo in v nasprotni smeri.</p>''', + '''<p><code>robot.move_tank( 'on', lr_power=[hitrost, -hitrost], ... )</code>.</p>'''], + 'moveTank':['''<p>Robot naj se obrne na mestu.</p>''', + '''<p>Najprimernejša metoda za to je <code>move_tank</code></p>'''], + 'while':['''<p>Program naj teče dokler je kot zasuka manjši od 90 stopinj.</p>''', + '''<p><code>robot.gyro_sensor_measure() < 90:</code></p>'''], +} diff --git a/robot/problems/introduction/gyrosquare/common.py b/robot/problems/introduction/gyrosquare/common.py new file mode 100644 index 0000000..4c06247 --- /dev/null +++ b/robot/problems/introduction/gyrosquare/common.py @@ -0,0 +1,103 @@ +# coding=utf-8 + +from python.util import has_token_sequence, string_almost_equal, \ + string_contains_number, get_tokens, get_numbers, get_exception_desc +from server.hints import Hint, HintSequence + +id = 210 +group = 'introduction' +number = 8 +visible = True + +solution = '''\ +from ev3dev import * +from mindstorms_widgets import mindstorms_widgets + +robot = mindstorms_widgets() +robot.connect_motor( 'left' ) +robot.connect_motor( 'right' ) +robot.connect_sensor( 'gyro' ) + +robot.gyro_set_mode( 'angle' ) +robot.reset_gyro() + +power = 20 +def forward_and_turnRight(power, angle): + robot.move_steering( 'on_for_seconds', direction=0, power=power, seconds=2 ) + robot.move_tank( 'on', lr_power=[power,-power] ) + while robot.gyro_sensor_measure( 'angle' ) < angle: + pass + robot.move_tank( 'off' ) + +forward_and_turnRight(power, 89) +forward_and_turnRight(power, 179) +forward_and_turnRight(power, 269) +forward_and_turnRight(power, 359) +''' + +hint_type = { + 'mW_init': Hint('mW_init'), + 'connectMotorLeft': Hint('connectMotorLeft'), + 'connectMotorRight': Hint('connectMotorRight'), + 'moveSteering': Hint('moveSteering'), + 'onForSeconds': Hint('onForSeconds'), + 'direction': Hint('direction'), + 'seconds': Hint('seconds'), + 'connectGyro': Hint('connectGyro'), + 'resetGyro': Hint('resetGyro'), + 'setGyroMode': Hint('setGyroMode'), + 'gyroMeasure': Hint('gyroMeasure'), + 'moveTank': Hint('moveTank'), + 'lrPower': Hint('lrPower'), + 'while': Hint('while') +} + +def hint( code): + tokens = get_tokens(code) + + # if code does not include mindstorms_widgets(), a student gets a hint that the robot should be somehow represented in the program + if not has_token_sequence(tokens, ['mindstorms_widgets', '(',')']): + return [{'id': 'mW_init'}] + + # if code does not include connect_motor statement, a student needs to learn about how to connect the motors + if not has_token_sequence(tokens, ['connect_motor']) and not has_token_sequence(tokens, ['left']): + return [{'id': 'connectMotorLeft'}] + + # if code does not include connect_motor statement, a student needs to learn about how to connect the motors + if not has_token_sequence(tokens, ['connect_motor']) and not has_token_sequence(tokens, ['right']): + return [{'id': 'connectMotorRight'}] + + if not (has_token_sequence(tokens, ['connect_gyro','(']) and 'gyro' in code): + return [{'id': 'connectGyro'}] + + if not has_token_sequence(tokens, ['reset_gyro()']): + return [{'id': 'resetGyro'}] + + if not has_token_sequence(tokens, ['gyro_set_mode']): + return [{'id': 'setGyroMode'}] + + if not has_token_sequence(tokens, ['gyro_sensor_measure()']): + return [{'id': 'gyroMeasure'}] + + if not has_token_sequence(tokens, ['move_tank']): + return [{'id': 'moveTank'}] + + if not has_token_sequence(tokens, ['move_steering']): + return [{'id': 'moveSteering'}] + + if not 'on_for_seconds' in code: + return [{'id': 'onForSeconds'}] + + if not 'direction' in code: + return [{'id': 'direction'}] + + if not 'seconds' in code: + return [{'id': 'seconds'}] + + if not 'lr_power' in code: + return [{'id': 'lrPower'}] + + if not has_token_sequence(tokens, ['while']): + return [{'id': 'while'}] + + return None diff --git a/robot/problems/introduction/gyrosquare/en.py b/robot/problems/introduction/gyrosquare/en.py new file mode 100644 index 0000000..bbd389c --- /dev/null +++ b/robot/problems/introduction/gyrosquare/en.py @@ -0,0 +1,14 @@ +# coding=utf-8 +import server +mod = server.problems.load_language('python', 'en') + +id = 210 +name = 'Gyro square' +slug = 'Gyro square' + +description = '''\ +''' + +hint = { + +} diff --git a/robot/problems/introduction/gyrosquare/naloga08_gyroSquare.py b/robot/problems/introduction/gyrosquare/naloga08_gyroSquare.py new file mode 100644 index 0000000..ec0dd2f --- /dev/null +++ b/robot/problems/introduction/gyrosquare/naloga08_gyroSquare.py @@ -0,0 +1,33 @@ +#!/usr/bin/python +# coding=utf-8 + +print (""" +Naloga 8: + Robot naj prevozi kvadrat s stranico 20 cm; pri tem naj si pomaga z ziroskopom. +""") +import sys +sys.path.append('/home/user/codeq') +import time +from ev3dev import * +from mindstorms_widgets import mindstorms_widgets + +robot = mindstorms_widgets() +robot.connect_motor( 'left' ) +robot.connect_motor( 'right' ) +robot.connect_sensor( 'gyro' ) + +robot.gyro_set_mode( 'angle' ) +robot.reset_gyro() + +power = 20 +def forward_and_turnRight(power, angle): + robot.move_steering( 'on_for_seconds', direction=0, power=power, seconds=2 ) + robot.move_tank( 'on', lr_power=[power,-power] ) + while robot.gyro_sensor_measure( 'angle' ) < angle: + pass + robot.move_tank( 'off' ) + +forward_and_turnRight(power, 89) +forward_and_turnRight(power, 179) +forward_and_turnRight(power, 269) +forward_and_turnRight(power, 359) diff --git a/robot/problems/introduction/gyrosquare/sl.py b/robot/problems/introduction/gyrosquare/sl.py new file mode 100644 index 0000000..16b2c94 --- /dev/null +++ b/robot/problems/introduction/gyrosquare/sl.py @@ -0,0 +1,38 @@ +# coding=utf-8 + +name = 'Kvadrat z žiroskopom' +slug = 'Kvadrat z žiroskopom' + +description = '''\ +<p>Robot naj prevozi kvadrat s stranico 20 cm; pri tem naj si pomaga z žiroskopom.</p>''' + +hint = { + 'mW_init':['''<p>Robota v programu predstavimo z mindstorms_widgets(): <code>robot = mindstorms_widgets()</code>.</p>'''], + 'connectMotorLeft':['''<p>Robotu priključi levi motor</p>''', + '''<p><code>robot.connect_motor( 'left' )</code>.</p>'''], + 'connectMotorRight':['''<p>Robotu priključi desni motor</p>''', + '''<p><code>robot.connect_motor( 'right' )</code>.</p>'''], + 'connectGyro':['''<p>Robotu priključi žiroskop.</p>''', + '''<p><code>robot.connect_sensor( 'gyro' )</code>.</p>'''], + 'resetGyro':['''<p>Žiroskop na začetku resetiraj.</p>''', + '''<p><code>robot.reset_gyro()</code>.</p>'''], + 'setGyroMode':['''<p>Žiroskopu nastavi način delovanja na merjenje kotov.</p>''', + '''<p><code>robot.gyro_set_mode('angle')</code>.</p>'''], + 'gyroMeasure':['''<p>Preberi vrednost žiroskopa.</p>''', + '''<p><code>robot.gyro_sensor_measure()</code>.</p>'''], + 'lrPower':['''<p>Za obrat na mestu se morata kolesi vrteti z enako močjo in v nasprotni smeri.</p>''', + '''<p><code>robot.move_tank( 'on', lr_power=[hitrost, -hitrost], ... )</code>.</p>'''], + 'moveTank':['''<p>Robot naj se obrne na mestu.</p>''', + '''<p>Najprimernejša metoda za to je <code>move_tank</code></p>'''], + 'while':['''<p>Program naj čaka dokler je kot zasuka manjši od 90 stopinj.</p>''', + '''<p><code>robot.gyro_sensor_measure() < 90:</code></p>'''], + + 'moveSteering':['''<p>Sinhroniziraj motorja in ju zaženi.</p>''', + '''<p>Za sinhronizirano vožnjo je najbolj primerna metoda <code>robot.move_steering( ... )</code>.</p>'''], + 'onForSeconds':['''<p>Prvi argument metode <code>robot.move_steering</code> naj pove, da bo delovanje motorjev časovno omejeno.</p>''', + '''<p><code>robot.move_steering('on_for_seconds', ... )</code>.</p>'''], + 'direction':['''<p>Navedi smer premikanja motorjev, naravnost = 0.</p>''', + '''<p><code>robot.move_steering( 'on_for_seconds', direction=0, ... )</code>.</p>'''], + 'seconds':['''<p>Napiši časovno omejitev v sekundah.</p>''', + '''<p><code>robot.move_steering( 'on_for_seconds', direction=0, seconds=3 )</code>.</p>'''], +} diff --git a/robot/problems/introduction/inline.py b/robot/problems/introduction/inline.py new file mode 100644 index 0000000..67a6f06 --- /dev/null +++ b/robot/problems/introduction/inline.py @@ -0,0 +1,19 @@ +solution = '''\ +from ev3dev import * +from mindstorms_widgets import mindstorms_widgets + +robot = mindstorms_widgets() +robot.connect_motor( 'left' ) +robot.connect_motor( 'right' ) +robot.connect_sensor( 'ultrasonic' ) +robot.move_steering( 'on', power=80 ) +while robot.ultrasonic_sensor_measure( 'distance-cm' ) > 500: + pass +robot.move_steering( 'on', power=20 ) +while robot.ultrasonic_sensor_measure( 'distance-cm' ) > 200: + pass +robot.move_steering( 'off' ) +''' + +print (any(('power' in s and '80' in s and '=' in s) for s in solution.split('\n'))) + diff --git a/robot/problems/introduction/mindstorms_widgets_OLD.py b/robot/problems/introduction/mindstorms_widgets_OLD.py new file mode 100644 index 0000000..4151133 --- /dev/null +++ b/robot/problems/introduction/mindstorms_widgets_OLD.py @@ -0,0 +1,398 @@ +#!/usr/bin/python + +import sys, time +from ev3dev import * +# The mindstorms_widgets class is a wrapper for the python-ev3dev +# language binding. It implements interfaces that correspond directly +# to the action and sensor programming blocks (widgets) from the +# mindstorms LabVIEW software. This allows leverage from the amazing +# documentation already provided by Lego. Some exceptions are: +# * It doesn't auto-detect the devices. You need to call +# connect_motor or connect_sensor first for each device you use +# * Only two large motors and one medium motor are supported. +# This simplifies the interfaces. The motors are referenced by +# the following names in the interfaces: +# 'medium' motor, default Port A +# 'right' large motor, default Port B +# 'left' large motor, default Port C +# * degrees and rotations inputs must always be positive +# use negative power to go backward +# * Rather than having a separate unregulated_motor widget, regulated is an +# an optional input parameter (default = True) +# * motor_rotation_measure, 'current_power' returns both the duty_cycle_sp +# (the actual power level being applied to the motor) and speed_sp +# * sound includes an option to give a sentence and have the robot speak + +# Known issues/limitations +# * The degrees, rotations, speed and invert values provided as inputs do +# not map directly to the values programmed to the sysfs attributes i.e. +# if you don't use mindstorm_widgets methods exclusively to control the +# motors you will need to explicitly initialize all the relevant sysfs +# attributes each time you program the motor from outside mindstorms_widgets +# * led_flash +# * no error handling/recovery +# * volume for tones doesn't work very well (seems to have only 2 levels) +# * when reading the heading and proximity in beacon mode, ir_sensor_measure +# returns good values at first but continuing to poll it seems to result +# in inconsistent values +# * ultrasonic and gyro sensors are not implemented + +# Mindstorms uses the following coefficients to compute speed_sp +# based on a power input in range 0-100 i.e. speed_sp = x * power +# medium motor: x = 16.4 +# large motor : x = 10.3 +# e.g. power of 10 sets medium motor speed_sp to 160 (deg/sec) + +MEDIUM_MOTOR_SPEED_COEFFICIENT = 16.4 +LARGE_MOTOR_SPEED_COEFFICIENT = 10.3 + +class mindstorms_widgets: + def __init__(self): + self.motor = { + 'medium':None, + 'left' :None, + 'right' :None + } + + def _set_motor_attribs( self, size, motors, powers, + invert, regulated, brake_at_end ): + # For regulated speed, speed_sp is tacho counts per + # second which for lego EV3 motors is also degrees per second. + # To provide a mindstorms 0-100 power value for regulated + # speed we need to convert it. + speed_x = LARGE_MOTOR_SPEED_COEFFICIENT + if size == 'medium': + speed_x = MEDIUM_MOTOR_SPEED_COEFFICIENT + + for m,p in zip ( motors, powers ): + # When using run_forever() the sign of speed_sp or duty_cycle_sp + # determines the direction the motor runs. When using + # run_to_rel_pos() the sign of position_sp determines the + # direction and the sign of speed_sp/duty_cycle_sp is ignored by + # the driver. + + # In either case the polarity reverses the direction. + + # When using move_steering, providing a negative power should + # result in reversing along the same path of a positive power + # in the same direction. If the steering is sharp the power of + # one motor is opposite sign of the other. + + # This all combines to make for overly complicated logic when the + # intent is to set motor attribs independent of knowing what + # methods are being used. To avoid this complexity I devised the + # following scheme: + # * Use the motor polarity as the common attribute to determine + # direction since it works the same for run_forever() and + # run_to_rel_pos() + # * Always program duty_cycle_sp and speed_sp to positive values + # * Always program position_sp to a positive value + pol = ['normal','inversed'] + brk = ['coast', 'brake'] + inv = invert + if p < 0: + inv = not invert + p = abs( p ) + if m: + m.polarity = pol[int( inv )] #e.g. True=1='inversed' + m.stop_command = brk[int( brake_at_end )] + #print ('speed: '+str(p)+' inv: '+str(inv)) + if regulated: + m.speed_regulation_enabled = 'on' + m.speed_sp = int( p*speed_x ) + else: + m.speed_regulation_enabled = 'off' + m.duty_cycle_sp = p + + def _move_wait( self ): + # wait for any running motors to finish their movement then stop + # all the motors. + left_is_off = False + right_is_off = False + if 'running' not in self.motor['left'].state: + left_is_off = True + if 'running' not in self.motor['right'].state: + right_is_off = True + while ((left_is_off or 'running' in self.motor['left'].state) and + (right_is_off or 'running' in self.motor['right'].state)): + time.sleep(0.1) + self.motor['left'].stop() + self.motor['right'].stop() + + def _move( self, mode, seconds, degrees, rotations ): + if mode == 'off': + self.motor['left'].stop() + self.motor['right'].stop() + elif mode == 'on': + self.motor['left'].run_forever() + self.motor['right'].run_forever() + elif mode == 'on_for_seconds': + self.motor['left'].run_forever() + self.motor['right'].run_forever() + time.sleep(seconds) + self.motor['left'].stop() + self.motor['right'].stop() + elif mode == 'on_for_degrees': + self.motor['left'].run_to_rel_pos(position_sp=degrees) + self.motor['right'].run_to_rel_pos(position_sp=degrees) + self._move_wait() + elif mode == 'on_for_rotations': + self.motor['left'].run_to_rel_pos( position_sp=int(rotations*360) ) + self.motor['right'].run_to_rel_pos( position_sp=int(rotations*360) ) + self._move_wait() + + def _validate_inputs( self, degrees, rotations ): + if (degrees < 0 or rotations < 0): + print( 'degrees and rotations should be positive. ' + 'Use negative power to go backward' ) + + def _run_motor( self, motor, mode, power, + seconds, degrees, rotations, + invert, regulated, brake_at_end ): + self._validate_inputs( degrees, rotations ) + self._set_motor_attribs( 'medium', [motor], [power], invert, regulated, + brake_at_end ) + if mode == 'off': + motor.stop() + elif mode == 'on': + motor.run_forever() + elif mode == 'on_for_seconds': + motor.run_forever() + time.sleep(seconds) + motor.stop() + elif mode == 'on_for_degrees': + motor.run_to_rel_pos(position_sp=degrees) + while 'running' in motor.state: + time.sleep(0.1) + elif motor and mode == 'on_for_rotations': + motor.run_to_rel_pos(position_sp=int(rotations*360)) + while 'running' in motor.state: + time.sleep(0.1) + + def _led_pulse( self, color ): + # Note that calling flash() multiple times on a LED causes an error + # Disable this routine for now + return + + if color == 'green': + led.green_left.flash( 200, 200 ) + led.green_right.flash( 200, 200 ) + elif color == 'orange': + led.green_left.flash( 200, 200 ) + led.red_left.flash( 200, 200 ) + led.green_right.flash( 200, 200 ) + led.red_right.flash( 200, 200 ) + elif color == 'red': + led.red_left.flash( 200, 200 ) + led.red_right.flash( 200, 200 ) + + + # Begin public methods + + def connect_motor( self, motor, port=None ): + default_port = { + 'medium':'A', + 'left' :'B', + 'right' :'C' + } + + port_enum = { + 'A' :OUTPUT_A, + 'B' :OUTPUT_B, + 'C' :OUTPUT_C, + 'D' :OUTPUT_D + } + if not port: + port = default_port[motor] + port = port.upper() + + if motor == 'medium': + self.motor['medium'] = medium_motor(port_enum[port]) + if not self.motor['medium'].connected: + print("Medium motor not connected to port " + port) + elif motor == 'right': + self.motor['right'] = large_motor(port_enum[port]) + if not self.motor['right'].connected: + print("Large right motor not connected to port " + port) + elif motor == 'left': + self.motor['left'] = large_motor(port_enum[port]) + if not self.motor['left'].connected: + print("Large left motor not connected to port " + port) + + def connect_sensor( self, sensor ): + if sensor == 'color': + self.cs = color_sensor() + if not self.cs.connected: + print("Color sensor not connected") + elif sensor == 'touch': + self.ts = touch_sensor() + if not self.ts.connected: + print("Touch sensor not connected") + elif sensor == 'infrared': + self.irs = infrared_sensor() + if not self.irs.connected: + print("Infrared sensor not connected") + elif sensor == 'gyro': + self.gs = gyro_sensor() + if not self.gs.connected: + print("Gyro sensor not connected") + elif sensor == 'ultrasonic': + self.us = ultrasonic_sensor() + if not self.us.connected: + print("Ultrasonic sensor not connected") + + + def large_motor( self, motor, mode, power=50, + seconds=0, degrees=0, rotations=0, + invert=False, regulated=True, brake_at_end=True ): + self._run_motor( self.motor[motor], mode, power, + seconds, degrees, rotations, + invert, regulated, brake_at_end ) + + def medium_motor( self, mode, power=50, + seconds=0, degrees=0, rotations=0, + invert=False, regulated=True, brake_at_end=True ): + self._run_motor( self.motor['medium'], mode, power, + seconds, degrees, rotations, + invert, regulated, brake_at_end ) + + def move_steering( self, mode, direction=0, power=50, + seconds=0, degrees=0, rotations=0, + invert=False, regulated=True, brake_at_end=True ): + ''' + direction [-100, 100]: + * -100 = pivot left i.e. right motor = power + * left motor = -power + * -50 = sharp left i.e. right motor = power + * left motor = 0 + * -25 = turn left i.e. right motor = power + * left motor = 1/2 power + * 0 = straight + * 25 = turn right + * 50 = sharp right + * 100 = pivot right + ''' + self._validate_inputs( degrees, rotations ) + powers = steering( direction, power ) + self._set_motor_attribs( 'large', + [self.motor['left'], self.motor['right']], + powers, invert, regulated, brake_at_end ) + self._move( mode, seconds, degrees, rotations ) + + def move_tank( self, mode, direction=0, lr_power=None, + seconds=0, degrees=0, rotations=0, + invert=False, regulated=True, brake_at_end=True ): + self._validate_inputs( degrees, rotations ) + if not lr_power: + lr_power=[50,50] + self._set_motor_attribs( 'large', + [self.motor['left'], self.motor['right']], + lr_power, invert, regulated, brake_at_end ) + self._move( mode, seconds, degrees, rotations ) + + def brick_status_light( self, mode, color='green', pulse=False ): + led.all_off() + if mode == 'on': + if color == 'green': + led.green_on() + elif color == 'orange': + led.all_on() + elif color == 'red': + led.red_on() + if mode == 'on' and pulse: + self._led_pulse( color ) + pass + # the sensor should be perfectly still while resetting the gyro + def resetGyro(self): + self.gs.mode = 'GYRO-RATE' + self.gs.mode = 'GYRO-ANG' + + def gyro_sensor_measure( self, mode ): + if mode == 'angle': + self.gs.mode = 'GYRO-ANG' # Angle (degrees), value: (-32768 to 32767) + elif mode == 'rate': + self.gs.mode = 'GYRO-RATE' + return self.gs.value() + + def ultrasonic_sensor_measure( self, mode ): + if mode == 'distance-cm': + self.gs.mode = 'US-DIST-CM' # Continuous measurement of distance (0-2550 mm) + return self.us.value() + + def color_sensor_measure( self, mode ): + if mode == 'color': + self.cs.mode = 'COL-COLOR' + elif mode == 'reflected_light_intensity': + self.cs.mode = 'COL-REFLECT' + elif mode == 'ambient_light_intensity': + self.cs.mode = 'COL-AMBIENT' + return self.cs.value() + + def ir_sensor_measure( self, mode, channel=1 ): + # value(n) returns the given value. See + # http://www.ev3dev.org/docs/sensors/lego-ev3-infrared-sensor/ + i = 2*(channel-1) + if mode == 'proximity': + self.irs.mode = 'IR-PROX' + return self.irs.value() + elif mode == 'beacon': + self.irs.mode = 'IR-SEEK' + # (heading, proximity, detected) + # heading in range (-25,25) + # proximity is -128 or in [0,100] + prox = self.irs.value(i+1) + return( self.irs.value(i), prox, prox != -128 ) + elif mode == 'remote': + self.irs.mode = 'IR-REMOTE' + ''' + 0 = No button (and Beacon Mode is off) + 1 = Button 1 + 2 = Button 2 + 3 = Button 3 + 4 = Button 4 + 5 = Both Button 1 and Button 3 + 6 = Both Button 1 and Button 4 + 7 = Both Button 2 and Button 3 + 8 = Both Button 2 and Button 4 + 9 = Beacon Mode is on + 10 = Both Button 1 and Button 2 + 11 = Both Button 3 and Button 4 + ''' + return self.irs.value( channel-1 ) + + + def motor_rotation_measure( self, motor, mode ): + motor = self.motor[motor] + if mode == 'reset': + motor.position = 0 + return None + if mode == 'current_power': + return (motor.speed, motor.duty_cycle) + degrees=motor.position + if mode == 'degrees': + return degrees + elif mode == 'rotations': + return degrees/360 + + def touch_sensor_measure( self ): + return self.ts.value() + + def sound( self, mode, volume=40, hz=523, path=None, + sentence=None, duration_ms=200 ): + # todo:Volume doesn't seem to work very well for tones. + # 0=just a click + # 1-49 same loud volume tone + # > 50 same volume really loud tone + sound.volume = volume + if mode == 'play_file': + # Sound.rsf files can be extracted from mindstorms by using the + # sound in a mindstorms program then going to mindstorms + # project->sounds tab and choosing "export" + + sound.play( path ) + elif mode == 'play_tone': + sound.tone( int(hz), duration_ms ) + elif mode == 'speak': + sound.speak( sentence, True ) + + diff --git a/robot/problems/introduction/printcolors/common.py b/robot/problems/introduction/printcolors/common.py new file mode 100644 index 0000000..54c3cda --- /dev/null +++ b/robot/problems/introduction/printcolors/common.py @@ -0,0 +1,80 @@ +# coding=utf-8 + +from python.util import has_token_sequence, string_almost_equal, \ + string_contains_number, get_tokens, get_numbers, get_exception_desc +from server.hints import Hint, HintSequence + +id = 211 +group = 'introduction' +number = 9 +visible = True + +solution = '''\ +import time +from ev3dev import * +from mindstorms_widgets import mindstorms_widgets + +color_table = ['none', 'black', 'blue', 'green', 'yellow', 'red', 'white', 'brown'] + +robot = mindstorms_widgets() +robot.connect_motor( 'left' ) +robot.connect_motor( 'right' ) +robot.connect_sensor( 'color' ) + +robot.move_steering( 'on') +start = time.time() +color = -1 +while time.time()-start < 2.1: + c = robot.color_sensor_measure('color') + if c!=color: + print( c, color_table[c]) + color = c +robot.move_steering( 'off' ) +''' + +hint_type = { + 'mW_init': Hint('mW_init'), + 'connectMotorLeft': Hint('connectMotorLeft'), + 'connectMotorRight': Hint('connectMotorRight'), + 'moveSteeringOn': Hint('moveSteeringOn'), + 'moveSteeringOff': Hint('moveSteeringOff'), + 'connectColorSensor': Hint('connectColorSensor'), + 'colorSensorMeasure': Hint('colorSensorMeasure'), + 'while': Hint('while'), + 'print': Hint('print') +} + +def hint( code): + tokens = get_tokens(code) + + # if code does not include mindstorms_widgets(), a student gets a hint that the robot should be somehow represented in the program + if not has_token_sequence(tokens, ['mindstorms_widgets', '(',')']): + return [{'id': 'mW_init'}] + + # if code does not include connect_motor statement, a student needs to learn about how to connect the motors + if not has_token_sequence(tokens, ['connect_motor']) and not has_token_sequence(tokens, ['left']): + return [{'id': 'connectMotorLeft'}] + + # if code does not include connect_motor statement, a student needs to learn about how to connect the motors + if not has_token_sequence(tokens, ['connect_motor']) and not has_token_sequence(tokens, ['right']): + return [{'id': 'connectMotorRight'}] + + if not (has_token_sequence(tokens, ['connect_sensor', '(' ]) and 'color' in code): + return [{'id': 'connectColorSensor'}] + + if not (has_token_sequence(tokens, ['move_steering', '(']) and 'on' in code): + return [{'id': 'moveSteeringOn'}] + + if not (has_token_sequence(tokens, ['move_steering', '(']) and 'off' in code): + return [{'id': 'moveSteeringOff'}] + + if not (has_token_sequence(tokens, ['color_sensor_measure', '(']) and 'color' in code): + return [{'id': 'colorSensorMeasure'}] + + if not has_token_sequence(tokens, ['while']): + return [{'id': 'while'}] + + if not has_token_sequence(tokens, ['print', '(']): + return [{'id': 'print'}] + + return None diff --git a/robot/problems/introduction/printcolors/en.py b/robot/problems/introduction/printcolors/en.py new file mode 100644 index 0000000..bb57c9a --- /dev/null +++ b/robot/problems/introduction/printcolors/en.py @@ -0,0 +1,15 @@ +# coding=utf-8 +import server +mod = server.problems.load_language('python', 'en') + +id = 211 +name = 'Color detection' +slug = 'Color detection' + +description = '''\ + +''' + +hint = { + +} diff --git a/robot/problems/introduction/printcolors/naloga09_printColors.py b/robot/problems/introduction/printcolors/naloga09_printColors.py new file mode 100644 index 0000000..59dfdb0 --- /dev/null +++ b/robot/problems/introduction/printcolors/naloga09_printColors.py @@ -0,0 +1,42 @@ +#!/usr/bin/python +# coding=utf-8 + +print (""" +Naloga 9: + Robot naj se pelje cez barvne crte in na zaslon izpise barvo, ki jo zazna s svetlobnim senzorjem. + Value Color + 0 none + 1 black + 2 blue + 3 green + 4 yellow + 5 red + 6 white + 7 brown +""") +import sys +sys.path.append('/home/user/codeq') +import time +from ev3dev import * +from mindstorms_widgets import mindstorms_widgets + +color_table = ['none', 'black', 'blue', 'green', 'yellow', 'red', 'white', 'brown'] + +#cs = color_sensor() +#assert cs.connected +#cs.mode = 'COL-COLOR' # values: 0-7 see the scale above + +robot = mindstorms_widgets() +robot.connect_motor( 'left' ) +robot.connect_motor( 'right' ) +robot.connect_sensor( 'color' ) + +robot.move_steering( 'on') +start = time.time() +color = -1 +while time.time()-start < 2.1: + c = robot.color_sensor_measure('color') + if c!=color: + print( c, color_table[c]) + color = c +robot.move_steering( 'off' ) diff --git a/robot/problems/introduction/printcolors/sl.py b/robot/problems/introduction/printcolors/sl.py new file mode 100644 index 0000000..d233b0e --- /dev/null +++ b/robot/problems/introduction/printcolors/sl.py @@ -0,0 +1,36 @@ +# coding=utf-8 + +name = 'Zaznavanje barv' +slug = 'Zaznavanje barv' + +description = '''\ +<p>Robot naj se pelje čez barvne črte in na zaslon izpiše barvo, ki jo zazna s svetlobnim senzorjem. + Value Color + 0 none + 1 black + 2 blue + 3 green + 4 yellow + 5 red + 6 white + 7 brown</p>''' + +hint = { + 'mW_init':['''<p>Robota v programu predstavimo z mindstorms_widgets(): <code>robot = mindstorms_widgets()</code>.</p>'''], + 'connectMotorLeft':['''<p>Robotu priključi levi motor</p>''', + '''<p><code>robot.connect_motor( 'left' )</code>.</p>'''], + 'connectMotorRight':['''<p>Robotu priključi desni motor</p>''', + '''<p><code>robot.connect_motor( 'right' )</code>.</p>'''], + 'moveSteeringOn':['''<p>Sinhroniziraj motorja in ju zaženi.</p>''', + '''<p>Za sinhronizirano vožnjo je najbolj primerna metoda <code>robot.move_steering( 'on' )</code>.</p>'''], + 'moveSteeringOff':['''<p>Ustavi motorja.</p>''', + '''<p><code>robot.move_steering( 'off' )</code>.</p>'''], + 'connectColorSensor':['''<p>Robotu moramo priključiti barvni senzor.</p>''', + '''<p><code>robot.connect_sensor( 'color' )</code>.</p>'''], + 'colorSensorMeasure':['''<p>Medtem ko se robot pomika naprej, naj uporabi barvni senzor v načinu 'color', s katerim pove, katero barvo trenutno vidi.</p>''', + '''<p><code>robot.color_sensor_measure( 'color' )</code>.</p>'''], + 'while':['''<p>Uporabi zanko, znotraj katere robot odčitava barve in povečuje števec.</p>''', + '''<p>Zanka je lahko časovno omejena, npr. z uporabo metode <code>time.time()</code>.</p>''', + '''<p><code>while time.time()-start < 1.1:</code>.</p>'''], + 'print':['''<p>V zanki na zaslon izpisuj barvo, ki jo zazna robot.</p>'''] +} diff --git a/robot/problems/introduction/rotateback/common.py b/robot/problems/introduction/rotateback/common.py new file mode 100644 index 0000000..c009d37 --- /dev/null +++ b/robot/problems/introduction/rotateback/common.py @@ -0,0 +1,111 @@ +# coding=utf-8 + +from python.util import has_token_sequence, string_almost_equal, \ + string_contains_number, get_tokens, get_numbers, get_exception_desc +from server.hints import Hint, HintSequence + +id = 212 +group = 'introduction' +number = 11 +visible = True + +solution = '''\ +from ev3dev import * +from mindstorms_widgets import mindstorms_widgets + +def sgn(x): + return -1 if x<0 else (1 if x>0 else 0) + +robot = mindstorms_widgets() +robot.connect_motor( 'left' ) +robot.connect_motor( 'right' ) +robot.connect_sensor( 'gyro' ) +robot.connect_sensor( 'touch' ) + +smer = hitrost = 0 +robot.reset_gyro() +robot.gyro_set_mode( 'angle' ) +prev_smer = -1 +while 1: + smer = robot.gyro_sensor_measure() + if smer!=prev_smer: + print ("Kot:", smer) + prev_smer = smer + + if robot.touch_sensor_measure(): + robot.reset_gyro() + print ( robot.gyro_sensor_measure() ) + + if abs(smer) < 10: + hitrost = 10*sgn(smer) + else: + if abs(smer) > 100: + hitrost = 100*sgn(smer) + else: + hitrost = smer + + if abs(smer) > 1: + L, R = -hitrost, hitrost + else: + L = R = 0 + robot.move_tank('on', lr_power=[L,R]) +''' + +hint_type = { + 'mW_init': Hint('mW_init'), + 'connectMotorLeft': Hint('connectMotorLeft'), + 'connectMotorRight': Hint('connectMotorRight'), + 'connectGyro': Hint('connectGyro'), + 'connectTouch': Hint('connectTouch'), + 'resetGyro': Hint('resetGyro'), + 'setGyroMode': Hint('setGyroMode'), + 'gyroMeasure': Hint('gyroMeasure'), + 'ifTouch': Hint('ifTouch'), + 'moveTank': Hint('moveTank'), + 'lrPower': Hint('lrPower'), + 'while': Hint('while') +} + +def hint( code): + tokens = get_tokens(code) + + # if code does not include mindstorms_widgets(), a student gets a hint that the robot should be somehow represented in the program + if not has_token_sequence(tokens, ['mindstorms_widgets', '(',')']): + return [{'id': 'mW_init'}] + + # if code does not include connect_motor statement, a student needs to learn about how to connect the motors + if not has_token_sequence(tokens, ['connect_motor']) and not has_token_sequence(tokens, ['left']): + return [{'id': 'connectMotorLeft'}] + + # if code does not include connect_motor statement, a student needs to learn about how to connect the motors + if not has_token_sequence(tokens, ['connect_motor']) and not has_token_sequence(tokens, ['right']): + return [{'id': 'connectMotorRight'}] + + if not (has_token_sequence(tokens, ['connect_gyro','(']) and 'gyro' in code): + return [{'id': 'connectGyro'}] + + if not (has_token_sequence(tokens, ['connect_sensor','(']) and 'touch' in code): + return [{'id': 'connectTouch'}] + + if not has_token_sequence(tokens, ['reset_gyro']): + return [{'id': 'resetGyro'}] + + if not has_token_sequence(tokens, ['gyro_set_mode']): + return [{'id': 'setGyroMode'}] + + if not has_token_sequence(tokens, ['gyro_sensor_measure']): + return [{'id': 'gyroMeasure'}] + + if not has_token_sequence(tokens, ['if','touch_sensor_measure','(']): + return [{'id': 'ifTouch'}] + + if not has_token_sequence(tokens, ['move_tank']): + return [{'id': 'moveTank'}] + + if not 'lr_power' in code: + return [{'id': 'lrPower'}] + + if not has_token_sequence(tokens, ['while']): + return [{'id': 'while'}] + + return None diff --git a/robot/problems/introduction/rotateback/en.py b/robot/problems/introduction/rotateback/en.py new file mode 100644 index 0000000..5d8d059 --- /dev/null +++ b/robot/problems/introduction/rotateback/en.py @@ -0,0 +1,15 @@ +# coding=utf-8 +import server +mod = server.problems.load_language('python', 'en') + +id = 212 +name = 'Rotate back' +slug = 'Rotate back' + +description = '''\ + +''' + +hint = { + +} diff --git a/robot/problems/introduction/rotateback/naloga11_rotateBack.py b/robot/problems/introduction/rotateback/naloga11_rotateBack.py new file mode 100644 index 0000000..0bab735 --- /dev/null +++ b/robot/problems/introduction/rotateback/naloga11_rotateBack.py @@ -0,0 +1,48 @@ +#!/usr/bin/python +# coding=utf-8 + +print (""" +Naloga 11: +""") +import sys +sys.path.append('/home/user/codeq') +import time +from ev3dev import * +from mindstorms_widgets import mindstorms_widgets + +def sgn(x): + return -1 if x<0 else (1 if x>0 else 0) + +robot = mindstorms_widgets() +robot.connect_motor( 'left' ) +robot.connect_motor( 'right' ) +robot.connect_sensor( 'gyro' ) +robot.connect_sensor( 'touch' ) + +smer = hitrost = 0 +robot.reset_gyro() +robot.gyro_set_mode( 'angle' ) +prev_smer = -1 +while 1: + smer = robot.gyro_sensor_measure() + if smer!=prev_smer: + print ("Kot:", smer) + prev_smer = smer + + if robot.touch_sensor_measure(): + robot.reset_gyro() + print ( robot.gyro_sensor_measure() ) + + if abs(smer) < 10: + hitrost = 10*sgn(smer) + else: + if abs(smer) > 100: + hitrost = 100*sgn(smer) + else: + hitrost = smer + + if abs(smer) > 1: + L, R = -hitrost, hitrost + else: + L = R = 0 + robot.move_tank('on', lr_power=[L,R]) diff --git a/robot/problems/introduction/rotateback/naloga11_rotateBack_old.py b/robot/problems/introduction/rotateback/naloga11_rotateBack_old.py new file mode 100644 index 0000000..39ab3bb --- /dev/null +++ b/robot/problems/introduction/rotateback/naloga11_rotateBack_old.py @@ -0,0 +1,56 @@ +#!/usr/bin/python +# coding=utf-8 + +print """ +Naloga 11: +""" + +import time +from ev3dev import * +from mindstorms_widgets import mindstorms_widgets + +# the sensor should be perfectly still while resetting the gyro +def resetGyro(gyro): + gyro.mode = 'GYRO-RATE' + gyro.mode = 'GYRO-ANG' + +def sgn(x): + return -1 if x<0 else (1 if x>0 else 0) + +gyro = gyro_sensor() +assert gyro.connected +gyro.mode = 'GYRO-ANG' + +touch = touch_sensor() +assert touch.connected + +robot = mindstorms_widgets() +robot.connect_motor( 'left' ) +robot.connect_motor( 'right' ) + +smer = hitrost = 0 +resetGyro(gyro) +prev_smer = -1 +while 1: + smer = gyro.value() + if smer!=prev_smer: + print "Kot:", smer + prev_smer = smer + + if touch.value(): + resetGyro(gyro) + print gyro.value() + + if abs(smer) < 10: + hitrost = 10*sgn(smer) + else: + if abs(smer) > 100: + hitrost = 100*sgn(smer) + else: + hitrost = smer + + if abs(smer) > 1: + L, R = -hitrost, hitrost + else: + L = R = 0 + robot.move_tank('on', lr_power=[L,R]) diff --git a/robot/problems/introduction/rotateback/old_naloga11_rotateBack.py b/robot/problems/introduction/rotateback/old_naloga11_rotateBack.py new file mode 100644 index 0000000..a14c91d --- /dev/null +++ b/robot/problems/introduction/rotateback/old_naloga11_rotateBack.py @@ -0,0 +1,53 @@ +#!/usr/bin/python +# coding=utf-8 +import sys +sys.path.append('/home/user/codeq') +import time +from ev3dev import * +from mindstorms_widgets import mindstorms_widgets + +# the sensor should be perfectly still while resetting the gyro +def resetGyro(gyro): + gyro.mode = 'GYRO-RATE' + gyro.mode = 'GYRO-ANG' + +def sgn(x): + return -1 if x<0 else (1 if x>0 else 0) + +gyro = gyro_sensor() +assert gyro.connected +gyro.mode = 'GYRO-ANG' + +touch = touch_sensor() +assert touch.connected + +robot = mindstorms_widgets() +robot.connect_motor( 'left' ) +robot.connect_motor( 'right' ) + +smer = hitrost = 0 +resetGyro(gyro) +prev_smer = -1 +while 1: + smer = gyro.value() + if smer!=prev_smer: + print ("Kot:", smer) + prev_smer = smer + + if touch.value(): + resetGyro(gyro) + print( gyro.value()) + + if abs(smer) < 10: + hitrost = 10*sgn(smer) + else: + if abs(smer) > 100: + hitrost = 100*sgn(smer) + else: + hitrost = smer + + if abs(smer) > 1: + L, R = -hitrost, hitrost + else: + L = R = 0 + robot.move_tank('on', lr_power=[L,R]) diff --git a/robot/problems/introduction/rotateback/sl.py b/robot/problems/introduction/rotateback/sl.py new file mode 100644 index 0000000..269ea34 --- /dev/null +++ b/robot/problems/introduction/rotateback/sl.py @@ -0,0 +1,33 @@ +# coding=utf-8 + +name = 'Drži smer' +slug = 'Drži smer' + +description = '''\ +<p>Robot naj drži smer - če ga obrnemo iz začetne smeri, naj se zasuka nazaj v prvotno smer. Ob pritisku na stikalo, naj se resetira (smer nastavi na 0).</p>''' + +hint = { + 'mW_init':['''<p>Robota v programu predstavimo z mindstorms_widgets(): <code>robot = mindstorms_widgets()</code>.</p>'''], + 'connectMotorLeft':['''<p>Robotu priključi levi motor</p>''', + '''<p><code>robot.connect_motor( 'left' )</code>.</p>'''], + 'connectMotorRight':['''<p>Robotu priključi desni motor</p>''', + '''<p><code>robot.connect_motor( 'right' )</code>.</p>'''], + 'connectGyro':['''<p>Robotu priključi žiroskop</p>''', + '''<p><code>robot.connect_sensor( 'gyro' )</code>.</p>'''], + 'connectTouch':['''<p>Robotu priključi stikalo</p>''', + '''<p><code>robot.connect_sensor( 'touch' )</code>.</p>'''], + 'resetGyro':['''<p>Žiroskop na začetku in ob pritisku na stikalo resetiraj.</p>''', + '''<p><code>robot.reset_gyro()</code>.</p>'''], + 'setGyroMode':['''<p>Žiroskopu nastavi način delovanja na merjenje kotov.</p>''', + '''<p><code>robot.gyro_set_mode('angle')</code>.</p>'''], + 'gyroMeasure':['''<p>Preberi vrednost žiroskopa.</p>''', + '''<p><code>robot.gyro_sensor_measure()</code>.</p>'''], + 'ifTouch':['''<p>Preveri, če je stikalo pritisnjeno.</p>''', + '''<p><code>if robot.touch_sensor_measure():</code>.</p>'''], + 'lrPower':['''<p>Za obrat na mestu se morata kolesi vrteti z enako močjo in v nasprotni smeri.</p>''', + '''<p><code>robot.move_tank( 'on_for_seconds', lr_power=[hitrost, -hitrost], ... )</code>.</p>'''], + 'moveTank':['''<p>Robot naj se obrne na mestu.</p>''', + '''<p>Najprimernejša metoda za to je <code>move_tank</code></p>'''], + 'while':['''<p>Program naj teče v neskončni zanki.</p>''', + '''<p><code>while 1:</code></p>'''], +} diff --git a/robot/problems/introduction/spotturn90/common.py b/robot/problems/introduction/spotturn90/common.py new file mode 100644 index 0000000..39e9148 --- /dev/null +++ b/robot/problems/introduction/spotturn90/common.py @@ -0,0 +1,60 @@ +# coding=utf-8 + +from python.util import has_token_sequence, string_almost_equal, \ + string_contains_number, get_tokens, get_numbers, get_exception_desc +from server.hints import Hint, HintSequence + +id = 214 +group = 'introduction' +number = 3 +visible = True + +solution = '''\ +from ev3dev import * +from mindstorms_widgets import mindstorms_widgets + +robot = mindstorms_widgets() +robot.connect_motor( 'left' ) +robot.connect_motor( 'right' ) + +robot.move_tank( 'on_for_seconds', lr_power=[20,-20], seconds=.9 ) +''' + +hint_type = { + 'mW_init': Hint('mW_init'), + 'connectMotorLeft': Hint('connectMotorLeft'), + 'connectMotorRight': Hint('connectMotorRight'), + 'moveTank': Hint('moveTank'), + 'onForSeconds': Hint('onForSeconds'), + 'lrPower': Hint('lrPower'), + 'seconds': Hint('seconds'), +} + +def hint( code): + tokens = get_tokens(code) + + # if code does not include mindstorms_widgets(), a student gets a hint that the robot should be somehow represented in the program + if not has_token_sequence(tokens, ['mindstorms_widgets', '(',')']): + return [{'id': 'mW_init'}] + + # if code does not include connect_motor statement, a student needs to learn about how to connect the motors + if not has_token_sequence(tokens, ['connect_motor']) and not has_token_sequence(tokens, ['left']): + return [{'id': 'connectMotorLeft'}] + + # if code does not include connect_motor statement, a student needs to learn about how to connect the motors + if not has_token_sequence(tokens, ['connect_motor']) and not has_token_sequence(tokens, ['right']): + return [{'id': 'connectMotorRight'}] + + if not has_token_sequence(tokens, ['move_tank']): + return [{'id': 'moveTank'}] + + if not 'on_for_seconds' in code: + return [{'id': 'onForSeconds'}] + + if not 'seconds' in code: + return [{'id': 'seconds'}] + + if not 'lr_power' in code: + return [{'id': 'lrPower'}] + + return None diff --git a/robot/problems/introduction/spotturn90/en.py b/robot/problems/introduction/spotturn90/en.py new file mode 100644 index 0000000..348c517 --- /dev/null +++ b/robot/problems/introduction/spotturn90/en.py @@ -0,0 +1,14 @@ +# coding=utf-8 +import server +mod = server.problems.load_language('python', 'en') + +id = 214 +name = 'Turn on spot 90' +slug = 'Turn on spot 90' + +description = '''\ +''' + +hint = { + +} diff --git a/robot/problems/introduction/spotturn90/naloga03_turnOnSpotRight90.py b/robot/problems/introduction/spotturn90/naloga03_turnOnSpotRight90.py new file mode 100644 index 0000000..033290b --- /dev/null +++ b/robot/problems/introduction/spotturn90/naloga03_turnOnSpotRight90.py @@ -0,0 +1,23 @@ +#!/usr/bin/python + +print ("Robot naj se na mestu obrne za 90 stopinj v desno.") +import sys +sys.path.append('/home/user/codeq') +import time +from ev3dev import * +from mindstorms_widgets import mindstorms_widgets + +robot = mindstorms_widgets() +robot.connect_motor( 'left' ) +robot.connect_motor( 'right' ) + +# Solution 1: +#robot.move_tank( 'on_for_seconds', lr_power=[20,-20], seconds=.9 ) + +# Solution 2: +robot.motor['left'].position = 0 +robot.motor['right'].position = 0 +while robot.motor['left'].position <180 or robot.motor['right'].position <180: + robot.large_motor( 'left', 'on', power=10 ) + robot.large_motor( 'right', 'on', power=10 , invert=True) + #print robot.motor['left'].position, robot.motor['right'].position diff --git a/robot/problems/introduction/spotturn90/sl.py b/robot/problems/introduction/spotturn90/sl.py new file mode 100644 index 0000000..fe477aa --- /dev/null +++ b/robot/problems/introduction/spotturn90/sl.py @@ -0,0 +1,21 @@ +# coding=utf-8 + +name = 'Obrat na mestu 90' +slug = 'Obrat na mestu 90' + +description = '''\ +<p>Robot naj se na mestu obrne za 90 stopinj v desno.</p>''' + +hint = { + 'mW_init':['''<p>Robota v programu predstavimo z mindstorms_widgets(): <code>robot = mindstorms_widgets()</code>.</p>'''], + 'connectMotorLeft':['''<p>Robotu moramo priključiti levi motor: <code>robot.connect_motor( 'left' )</code>.</p>'''], + 'connectMotorRight':['''<p>Robotu moramo priključiti desni motor: <code>robot.connect_motor( 'right' )</code>.</p>'''], + 'moveTank':['''<p>Robot naj se obrne na mestu.</p>''', + '''<p>Najprimernejša metoda za to je <code>move_tank</code></p>'''], + 'onForSeconds':['''<p>Prvi argument metode <code>robot.move_tank</code> naj pove, da bo delovanje motorjev časovno omejeno.</p>''', + '''<p><code>robot.move_tank('on_for_seconds', ... )</code>.</p>'''], + 'lrPower':['''<p>Za obrat na mestu se morata kolesi vrteti z enako močjo in v nasprotni smeri.</p>''', + '''<p><code>robot.move_tank( 'on_for_seconds', lr_power=[hitrost, -hitrost], ... )</code>.</p>'''], + 'seconds':['''<p>Napiši časovno omejitev v sekundah.</p>''', + '''<p><code>robot.move_tank( 'on_for_seconds', ..., seconds=... )</code>.</p>'''], +} diff --git a/robot/problems/introduction/square20/common.py b/robot/problems/introduction/square20/common.py new file mode 100644 index 0000000..f868a1c --- /dev/null +++ b/robot/problems/introduction/square20/common.py @@ -0,0 +1,76 @@ +# coding=utf-8 + +from python.util import has_token_sequence, string_almost_equal, \ + string_contains_number, get_tokens, get_numbers, get_exception_desc +from server.hints import Hint, HintSequence + +id = 213 +group = 'introduction' +number = 4 +visible = True + +solution = '''\ +from ev3dev import * +from mindstorms_widgets import mindstorms_widgets + +robot = mindstorms_widgets() +robot.connect_motor( 'left' ) +robot.connect_motor( 'right' ) + +turntime = .95 +robot.move_steering( 'on_for_seconds', direction=0, power=20, seconds=2 ) +robot.move_tank( 'on_for_seconds', lr_power=[20,-20], seconds=turntime ) +robot.move_steering( 'on_for_seconds', direction=0, power=20, seconds=2 ) +robot.move_tank( 'on_for_seconds', lr_power=[20,-20], seconds=turntime ) +robot.move_steering( 'on_for_seconds', direction=0, power=20, seconds=2 ) +robot.move_tank( 'on_for_seconds', lr_power=[20,-20], seconds=turntime ) +robot.move_steering( 'on_for_seconds', direction=0, power=20, seconds=2 ) +robot.move_tank( 'on_for_seconds', lr_power=[20,-20], seconds=turntime ) +''' + +hint_type = { + 'mW_init': Hint('mW_init'), + 'connectMotorLeft': Hint('connectMotorLeft'), + 'connectMotorRight': Hint('connectMotorRight'), + 'moveSteering': Hint('moveSteering'), + 'onForSeconds': Hint('onForSeconds'), + 'direction': Hint('direction'), + 'seconds': Hint('seconds'), + 'moveTank': Hint('moveTank'), + 'lrPower': Hint('lrPower') +} + +def hint( code): + tokens = get_tokens(code) + + # if code does not include mindstorms_widgets(), a student gets a hint that the robot should be somehow represented in the program + if not has_token_sequence(tokens, ['mindstorms_widgets', '(',')']): + return [{'id': 'mW_init'}] + + # if code does not include connect_motor statement, a student needs to learn about how to connect the motors + if not has_token_sequence(tokens, ['connect_motor']) and not has_token_sequence(tokens, ['left']): + return [{'id': 'connectMotorLeft'}] + + # if code does not include connect_motor statement, a student needs to learn about how to connect the motors + if not has_token_sequence(tokens, ['connect_motor']) and not has_token_sequence(tokens, ['right']): + return [{'id': 'connectMotorRight'}] + + if not has_token_sequence(tokens, ['move_steering']): + return [{'id': 'moveSteering'}] + + if not 'on_for_seconds' in code: + return [{'id': 'onForSeconds'}] + + if not 'direction' in code: + return [{'id': 'direction'}] + + if not 'seconds' in code: + return [{'id': 'seconds'}] + + if not has_token_sequence(tokens, ['move_tank']): + return [{'id': 'moveTank'}] + + if not 'lr_power' in code: + return [{'id': 'lrPower'}] + + return None diff --git a/robot/problems/introduction/square20/en.py b/robot/problems/introduction/square20/en.py new file mode 100644 index 0000000..907acfe --- /dev/null +++ b/robot/problems/introduction/square20/en.py @@ -0,0 +1,14 @@ +# coding=utf-8 +import server +mod = server.problems.load_language('python', 'en') + +id = 213 +name = 'Square 20' +slug = 'Square 20' + +description = '''\ +''' + +hint = { + +} diff --git a/robot/problems/introduction/square20/naloga04_square20.py b/robot/problems/introduction/square20/naloga04_square20.py new file mode 100644 index 0000000..171c4b8 --- /dev/null +++ b/robot/problems/introduction/square20/naloga04_square20.py @@ -0,0 +1,21 @@ +#!/usr/bin/python + +print "Robot naj enkrat prevozi kvadrat s stranico 20 cm." + +import time +from ev3dev import * +from mindstorms_widgets import mindstorms_widgets + +robot = mindstorms_widgets() +robot.connect_motor( 'left' ) +robot.connect_motor( 'right' ) + +turntime = .95 +robot.move_steering( 'on_for_seconds', direction=0, power=20, seconds=2 ) +robot.move_tank( 'on_for_seconds', lr_power=[20,-20], seconds=turntime ) +robot.move_steering( 'on_for_seconds', direction=0, power=20, seconds=2 ) +robot.move_tank( 'on_for_seconds', lr_power=[20,-20], seconds=turntime ) +robot.move_steering( 'on_for_seconds', direction=0, power=20, seconds=2 ) +robot.move_tank( 'on_for_seconds', lr_power=[20,-20], seconds=turntime ) +robot.move_steering( 'on_for_seconds', direction=0, power=20, seconds=2 ) +robot.move_tank( 'on_for_seconds', lr_power=[20,-20], seconds=turntime )
\ No newline at end of file diff --git a/robot/problems/introduction/square20/sl.py b/robot/problems/introduction/square20/sl.py new file mode 100644 index 0000000..f52ddf7 --- /dev/null +++ b/robot/problems/introduction/square20/sl.py @@ -0,0 +1,29 @@ +# coding=utf-8 + +name = 'Kvadrat 20 cm' +slug = 'Kvadrat 20 cm' + +description = '''\ +<p>Robot naj enkrat prevozi kvadrat s stranico 20 cm.</p>''' + +hint = { + 'mW_init':['''<p>Robota v programu predstavimo z mindstorms_widgets(): <code>robot = mindstorms_widgets()</code>.</p>'''], + 'connectMotorLeft':['''<p>Robotu priključi levi motor</p>''', + '''<p><code>robot.connect_motor( 'left' )</code>.</p>'''], + 'connectMotorRight':['''<p>Robotu priključi desni motor</p>''', + '''<p><code>robot.connect_motor( 'right' )</code>.</p>'''], + 'moveSteering':['''<p>Sinhroniziraj motorja in ju zaženi.</p>''', + '''<p>Za sinhronizirano vožnjo je najbolj primerna metoda <code>robot.move_steering( ... )</code>.</p>'''], + 'onForSeconds':['''<p>Prvi argument metode <code>robot.move_steering</code> naj pove, da bo delovanje motorjev časovno omejeno.</p>''', + '''<p><code>robot.move_steering('on_for_seconds', ... )</code>.</p>'''], + 'direction':['''<p>Navedi smer premikanja motorjev, naravnost = 0.</p>''', + '''<p><code>robot.move_steering( 'on_for_seconds', direction=0, ... )</code>.</p>'''], + 'seconds':['''<p>Napiši časovno omejitev v sekundah.</p>''', + '''<p><code>robot.move_steering( 'on_for_seconds', direction=0, seconds=... )</code>.</p>''', + '''<p><code>robot.move_tank( 'on_for_seconds', lr_power=[20,-20], seconds=.95 )</code>.</p>''',], + 'moveTank': ['''<p>Robot naj se obrne na mestu.</p>''', + '''<p>Najprimernejša metoda za to je <code>move_tank</code></p>''', + '''<p><code>robot.move_tank( 'on_for_seconds', ... )</code>.</p>'''], + 'lrPower': ['''<p>Za obrat na mestu se morata kolesi vrteti z enako močjo in v nasprotni smeri.</p>''', + '''<p><code>robot.move_tank( 'on_for_seconds', lr_power=[20,-20], seconds=.95 )</code>.</p>'''], +} diff --git a/robot/problems/introduction/wall1m/common.py b/robot/problems/introduction/wall1m/common.py new file mode 100644 index 0000000..2a93573 --- /dev/null +++ b/robot/problems/introduction/wall1m/common.py @@ -0,0 +1,80 @@ +# coding=utf-8 + +from python.util import has_token_sequence, string_almost_equal, \ + string_contains_number, get_tokens, get_numbers, get_exception_desc +from server.hints import Hint, HintSequence + +id = 215 +group = 'introduction' +number = 6 +visible = True + +solution = '''\ +from ev3dev import * +from mindstorms_widgets import mindstorms_widgets + +robot = mindstorms_widgets() +robot.connect_motor( 'left' ) +robot.connect_motor( 'right' ) +robot.connect_sensor( 'ultrasonic' ) +robot.move_steering( 'on', power=80 ) +while robot.ultrasonic_sensor_measure( 'distance-cm' ) > 500: + pass +robot.move_steering( 'on', power=20 ) +while robot.ultrasonic_sensor_measure( 'distance-cm' ) > 200: + pass +robot.move_steering( 'off' ) +''' + +hint_type = { + 'mW_init': Hint('mW_init'), + 'connectMotorLeft': Hint('connectMotorLeft'), + 'connectMotorRight': Hint('connectMotorRight'), + 'moveSteeringOn': Hint('moveSteeringOn'), + 'moveSteeringOff': Hint('moveSteeringOff'), + 'connectUltrasonicSensor': Hint('connectUltrasonicSensor'), + 'ultrasonicSensorMeasure': Hint('ultrasonicSensorMeasure'), + 'power80': Hint('power80'), + 'power20': Hint('power20'), + 'while': Hint('while') +} + +def hint( code ): + tokens = get_tokens(code) + lines = code.split('\n') + + # if code does not include mindstorms_widgets(), a student gets a hint that the robot should be somehow represented in the program + if not has_token_sequence(tokens, ['mindstorms_widgets', '(',')']): + return [{'id': 'mW_init'}] + + # if code does not include connect_motor statement, a student needs to learn about how to connect the motors + if not has_token_sequence(tokens, ['connect_motor']) and not has_token_sequence(tokens, ['left']): + return [{'id': 'connectMotorLeft'}] + + # if code does not include connect_motor statement, a student needs to learn about how to connect the motors + if not has_token_sequence(tokens, ['connect_motor']) and not has_token_sequence(tokens, ['right']): + return [{'id': 'connectMotorRight'}] + + if not (has_token_sequence(tokens, ['connect_sensor', '(']) and 'ultrasonic' in code) : + return [{'id': 'connectUltrasonicSensor'}] + + if not (has_token_sequence(tokens, ['move_steering', '(']) and 'on' in code): + return [{'id': 'moveSteeringOn'}] + + if not (has_token_sequence(tokens, ['move_steering', '(']) and 'off' in code): + return [{'id': 'moveSteeringOff'}] + + #if not (has_token_sequence(tokens, ['ultrasonic_sensor_measure', '(']) and 'distance-cm' in code): + if not any(('ultrasonic_sensor_measure' in s and '(' in s and 'distance-cm' in s) for s in lines): + return [{'id': 'ultrasonicSensorMeasure'}] + + if not any(('power' in s and '80' in s and '=' in s) for s in lines): + return [{'id': 'power80'}] + + if not any(('power' in s and '20' in s and '=' in s) for s in lines): + return [{'id': 'power20'}] + + if not has_token_sequence(tokens, ['while']): + return [{'id': 'while'}] + + return None diff --git a/robot/problems/introduction/wall1m/en.py b/robot/problems/introduction/wall1m/en.py new file mode 100644 index 0000000..ec91c7b --- /dev/null +++ b/robot/problems/introduction/wall1m/en.py @@ -0,0 +1,14 @@ +# coding=utf-8 +import server +mod = server.problems.load_language('python', 'en') + +id = 215 +name = 'To the wall' +slug = 'To the wall' + +description = '''\ +''' + +hint = { + +} diff --git a/robot/problems/introduction/wall1m/naloga06_wall1m.py b/robot/problems/introduction/wall1m/naloga06_wall1m.py new file mode 100644 index 0000000..e3be37c --- /dev/null +++ b/robot/problems/introduction/wall1m/naloga06_wall1m.py @@ -0,0 +1,27 @@ +#!/usr/bin/python +# coding=utf-8 + +print (""" +Naloga 6: + Robota postavi pred zid, tako da bo od njega oddaljen vsaj 1 m; vmes naj ne bo ovir. + Napisi program, s katerim bo robot vozil naravnost proti zidu z 80% mocjo. + Na razdalji 50 cm od zida naj robot zmanjsa hitrost na 20%, na razdalji 20 cm od zida pa naj se ustavi. +""") +import sys +sys.path.append('/home/user/codeq') +import time +from ev3dev import * +from mindstorms_widgets import mindstorms_widgets + +robot = mindstorms_widgets() +robot.connect_motor( 'left' ) +robot.connect_motor( 'right' ) +robot.connect_sensor( 'ultrasonic' ) +robot.move_steering( 'on', power=80 ) +while robot.ultrasonic_sensor_measure( 'distance-cm' ) > 500: + pass +robot.move_steering( 'on', power=20 ) +while robot.ultrasonic_sensor_measure( 'distance-cm' ) > 200: + pass +robot.move_steering( 'off' ) + diff --git a/robot/problems/introduction/wall1m/naloga06_wall1m_testing.py b/robot/problems/introduction/wall1m/naloga06_wall1m_testing.py new file mode 100644 index 0000000..a7e449c --- /dev/null +++ b/robot/problems/introduction/wall1m/naloga06_wall1m_testing.py @@ -0,0 +1,43 @@ +#!/usr/bin/python +# coding=utf-8 + +print (""" +Naloga 6: + Robota postavi pred zid, tako da bo od njega oddaljen vsaj 1 m; vmes naj ne bo ovir. + Napisi program, s katerim bo robot vozil naravnost proti zidu z 80% mocjo. + Na razdalji 50 cm od zida naj robot zmanjsa hitrost na 20%, na razdalji 20 cm od zida pa naj se ustavi. +""") +import sys +sys.path.append('/home/user/codeq') +import time +from ev3dev import * +from mindstorms_widgets import mindstorms_widgets + +robot = mindstorms_widgets() +robot.connect_motor( 'left' ) +robot.connect_motor( 'right' ) + +mw=True + +if mw: + robot.connect_sensor( 'ultrasonic' ) + robot.move_steering( 'on', power=80 ) + while robot.ultrasonic_sensor_measure( 'distance-cm' ) > 500: + print(robot.ultrasonic_sensor_measure( 'distance-cm' ) ) + print(robot.ultrasonic_sensor_measure( 'distance-cm' ) ) + robot.move_steering( 'on', power=20 ) + while robot.ultrasonic_sensor_measure( 'distance-cm' ) > 200: + print(robot.ultrasonic_sensor_measure( 'distance-cm' ) ) + robot.move_steering( 'off' ) +else: + us = ultrasonic_sensor() + assert us.connected + us.mode = 'US-DIST-CM' # Continuous measurement of distance (0-2550 mm) + robot.move_steering( 'on', power=80 ) + while us.value()>500: + print( us.value() ) + print( us.value() ) + robot.move_steering( 'on', power=20 ) + while us.value()>200: + print( us.value() ) + robot.move_steering( 'off' ) diff --git a/robot/problems/introduction/wall1m/sl.py b/robot/problems/introduction/wall1m/sl.py new file mode 100644 index 0000000..18518d5 --- /dev/null +++ b/robot/problems/introduction/wall1m/sl.py @@ -0,0 +1,31 @@ +# coding=utf-8 + +name = 'Pelji do zida' +slug = 'Pelji do zida' + +description = '''\ +<p>Robota postavi pred zid, tako da bo od njega oddaljen vsaj 1 m; vmes naj ne bo ovir. + Napiši program, s katerim bo robot vozil naravnost proti zidu z 80% močjo. + Na razdalji 50 cm od zida naj robot zmanjša hitrost na 20%, na razdalji 20 cm od zida pa naj se ustavi.</p>''' + +hint = { + 'mW_init':['''<p>Robota v programu predstavimo z mindstorms_widgets(): <code>robot = mindstorms_widgets()</code>.</p>'''], + 'connectMotorLeft':['''<p>Robotu priključi levi motor</p>''', + '''<p><code>robot.connect_motor( 'left' )</code>.</p>'''], + 'connectMotorRight':['''<p>Robotu priključi desni motor</p>''', + '''<p><code>robot.connect_motor( 'right' )</code>.</p>'''], + 'moveSteeringOn':['''<p>Sinhroniziraj motorja in ju zaženi.</p>''', + '''<p>Za sinhronizirano vožnjo je najbolj primerna metoda <code>robot.move_steering( 'on' )</code>.</p>'''], + 'moveSteeringOff':['''<p>Ustavi motorja.</p>''', + '''<p><code>robot.move_steering( 'off' )</code>.</p>'''], + 'power80': ['''<p>Nastavi moč motorjev na 80%.</p>''', + '''<p><code>robot.move_steering( 'on', power=80 )</code>.</p>'''], + 'power20': ['''<p>Nastavi moč motorjev na 20%.</p>''', + '''<p><code>robot.move_steering( 'on', power=20 )</code>.</p>'''], + 'connectUltrasonicSensor':['''<p>Robotu moramo priključiti ultrazvočni senzor.</p>''', + '''<p><code>robot.connect_sensor( 'ultrasonic' )</code>.</p>'''], + 'ultrasonicSensorMeasure':['''<p>Medtem ko se robot pomika naprej, naj uporabi ultrazvočni senzor v načinu 'distance-cm', s katerim meri razdaljo v cm.</p>''', + '''<p><code>robot.ultrasonic_sensor_measure( 'distance-cm' )</code>.</p>'''], + 'while':['''<p>Uporabi zanko: robot naj se pelje naprej, dokler ne pride do razdalje 50 cm od zida.</p>''', + '''<p>Uporabi še eno zanko: robot naj se pelje naprej z 20% močjo, dokler ne pride do razdalje 20 cm od zida.</p>'''], +} |