Skip to content

Commit

Permalink
OpenRoberta#1222: add support for Ultrasonic HC-SR04
Browse files Browse the repository at this point in the history
  • Loading branch information
chpoulter committed Feb 13, 2022
1 parent b0adb86 commit 10e1607
Show file tree
Hide file tree
Showing 10 changed files with 166 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

(repeat [FOREVER, BoolConst[value: true]]
Var [abstand] := SensorExpr [UltrasonicSensor [U1, DISTANCE, - EMPTY_SLOT -]]

AktionStmt [DisplayTextAction [TEXT, FunctionExpr [MathSingleFunct [ROUND, [Var [abstand]]]]]]WaitTimeStmt[time: NumConst[value: 1000]]
)

Var [abstand] := SensorExpr [UltrasonicSensor [U2, DISTANCE, - EMPTY_SLOT -]]


Var [abstand] := SensorExpr [UltrasonicSensor [U3, DISTANCE, - EMPTY_SLOT -]]

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

(repeat [FOREVER, BoolConst[value: true]]
Var [abstand] := SensorExpr [UltrasonicSensor [U1, DISTANCE, - EMPTY_SLOT -]]

AktionStmt [DisplayTextAction [TEXT, FunctionExpr [MathSingleFunct [ROUND, [Var [abstand]]]]]]WaitTimeStmt[time: NumConst[value: 1000]]
)

Var [abstand] := SensorExpr [UltrasonicSensor [U2, DISTANCE, - EMPTY_SLOT -]]


Var [abstand] := SensorExpr [UltrasonicSensor [U3, DISTANCE, - EMPTY_SLOT -]]

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

(repeat [FOREVER, BoolConst[value: true]]
Var [abstand] := SensorExpr [UltrasonicSensor [U1, DISTANCE, - EMPTY_SLOT -]]

AktionStmt [DisplayTextAction [TEXT, FunctionExpr [MathSingleFunct [ROUND, [Var [abstand]]]]]]WaitTimeStmt[time: NumConst[value: 1000]]
)

Var [abstand] := SensorExpr [UltrasonicSensor [U2, DISTANCE, - EMPTY_SLOT -]]


Var [abstand] := SensorExpr [UltrasonicSensor [U3, DISTANCE, - EMPTY_SLOT -]]

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Sensors:
[]
Actors:
[UsedActor [U1, ULTRASONIC], UsedActor [, DISPLAY], UsedActor [U2, ULTRASONIC], UsedActor [U3, ULTRASONIC]]
Methods:
[GET_ULTARSONIC_SAMPLE, ROUND]
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Sensors:
[]
Actors:
[UsedActor [U1, ULTRASONIC], UsedActor [, DISPLAY], UsedActor [U2, ULTRASONIC], UsedActor [U3, ULTRASONIC]]
Methods:
[GET_ULTARSONIC_SAMPLE, ROUND]
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Sensors:
[]
Actors:
[UsedActor [U1, ULTRASONIC], UsedActor [, DISPLAY], UsedActor [U2, ULTRASONIC], UsedActor [U3, ULTRASONIC]]
Methods:
[GET_ULTARSONIC_SAMPLE, ROUND]
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#define _GNU_SOURCE

#include "MicroBit.h"
#include "NEPODefs.h"
#include <list>
#include <array>
#include <stdlib.h>
MicroBit _uBit;


double getUltarsonicSample(double factor, MicroBitPin trigPin, MicroBitPin echoPin);

double ___abstand;

int main()
{
_uBit.init();
___abstand = 0;

while ( true ) {
___abstand = getUltarsonicSample(58, _uBit.io.P8, _uBit.io.P2);
_uBit.display.scroll(ManagedString(round(___abstand)));
_uBit.sleep(1000);
_uBit.sleep(_ITERATION_SLEEP_TIMEOUT);
}
___abstand = getUltarsonicSample(58, _uBit.io.P9, _uBit.io.P7);
___abstand = (_uBit.io.P2.readPulseHigh() * 0.017);
release_fiber();
}

double getUltarsonicSample(double factor, MicroBitPin trigPin, MicroBitPin echoPin) {
trigPin.setDigitalValue(1);
_uBit.sleep(25);
trigPin.setDigitalValue(0);

return ((double)_uBit.io.P2.readPulseHigh()) / factor;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#define _GNU_SOURCE

#include "MicroBit.h"
#include "NEPODefs.h"
#include <list>
#include <array>
#include <stdlib.h>
MicroBit _uBit;


double getUltarsonicSample(double factor, MicroBitPin trigPin, MicroBitPin echoPin);

double ___abstand;

int main()
{
_uBit.init();
___abstand = 0;

while ( true ) {
___abstand = getUltarsonicSample(58, _uBit.io.P8, _uBit.io.P2);
_uBit.display.scroll(ManagedString(round(___abstand)));
_uBit.sleep(1000);
_uBit.sleep(_ITERATION_SLEEP_TIMEOUT);
}
___abstand = getUltarsonicSample(58, _uBit.io.P9, _uBit.io.P7);
___abstand = (_uBit.io.P2.readPulseHigh() * 0.017);
release_fiber();
}

double getUltarsonicSample(double factor, MicroBitPin trigPin, MicroBitPin echoPin) {
trigPin.setDigitalValue(1);
_uBit.sleep(25);
trigPin.setDigitalValue(0);

return ((double)_uBit.io.P2.readPulseHigh()) / factor;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#define _GNU_SOURCE

#include "MicroBit.h"
#include "NEPODefs.h"
#include <list>
#include <array>
#include <stdlib.h>
MicroBit _uBit;


double getUltarsonicSample(double factor, MicroBitPin trigPin, MicroBitPin echoPin);

double ___abstand;

int main()
{
_uBit.init();
___abstand = 0;

while ( true ) {
___abstand = getUltarsonicSample(58, _uBit.io.P8, _uBit.io.P2);
_uBit.display.scroll(ManagedString(round(___abstand)));
_uBit.sleep(1000);
_uBit.sleep(_ITERATION_SLEEP_TIMEOUT);
}
___abstand = getUltarsonicSample(58, _uBit.io.P9, _uBit.io.P7);
___abstand = (_uBit.io.P2.readPulseHigh() * 0.017);
release_fiber();
}

double getUltarsonicSample(double factor, MicroBitPin trigPin, MicroBitPin echoPin) {
trigPin.setDigitalValue(1);
_uBit.sleep(25);
trigPin.setDigitalValue(0);

return ((double)_uBit.io.P2.readPulseHigh()) / factor;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<export xmlns="http://de.fhg.iais.roberta.blockly"><program><block_set xmlns="http://de.fhg.iais.roberta.blockly" robottype="calliope" xmlversion="3.1" description="" tags=""><instance x="384" y="50"><block type="robControls_start" id="rLis%0[zgo({imw{KpU4" intask="true" deletable="false"><mutation declare="true"></mutation><field name="DEBUG"></field><statement name="ST"><block type="robGlobalVariables_declare" id="x{{e`,XOhY/Hs%C0Kwzm" intask="true" deletable="false" movable="false"><mutation next="false" declaration_type="Number"></mutation><field name="VAR">abstand</field><field name="TYPE">Number</field><value name="VALUE"><block type="math_number" id="p8+G{3~z.e`DVHt)VEyo" intask="true"><field name="NUM">0</field></block></value></block></statement></block><block type="robControls_loopForever" id="JDZ{gETgi2ZRBxwuila0" intask="true"><statement name="DO"><block type="variables_set" id="bB5u+:dMsdXKAM_aO~95" intask="true"><mutation datatype="Number"></mutation><field name="VAR">abstand</field><value name="VALUE"><block type="robSensors_ultrasonic_getSample" id="#oo1?lDL=y)@SwT/1QG)" intask="true"><mutation mode="DISTANCE"></mutation><field name="MODE">DISTANCE</field><field name="SENSORPORT">U1</field><field name="SLOT"></field></block></value></block><block type="mbedActions_display_text" id="KhbK)x0!T[[~g]|XDQS8" intask="true"><field name="TYPE">TEXT</field><value name="OUT"><block type="math_round" id="q-[7IZo*H5s|zO0Ee9=^" intask="true"><field name="OP">ROUND</field><value name="NUM"><block type="variables_get" id="Ux:n789b@|^XZ2f;E=,A" intask="true"><mutation datatype="Number"></mutation><field name="VAR">abstand</field></block></value></block></value></block><block type="robControls_wait_time" id="?0b97zM.tr3p@ot,!d]T" intask="true"><value name="WAIT"><block type="math_number" id="_r]@(Jaa,rk%AM]O;?`D" intask="true"><field name="NUM">1000</field></block></value></block></statement></block><block type="variables_set" id="=Y_7XQ2~*U]p:[AV!{}t" intask="true"><mutation datatype="Number"></mutation><field name="VAR">abstand</field><value name="VALUE"><block type="robSensors_ultrasonic_getSample" id="wjn(WF!B)!j,_[ko;7G~" intask="true"><mutation mode="DISTANCE"></mutation><field name="MODE">DISTANCE</field><field name="SENSORPORT">U2</field><field name="SLOT"></field></block></value></block><block type="variables_set" id="h7~fQcv_SP%x`v(GEU|Z" intask="true"><mutation datatype="Number"></mutation><field name="VAR">abstand</field><value name="VALUE"><block type="robSensors_ultrasonic_getSample" id="c(|pt*+PfApbD[JnDI-3" intask="true"><mutation mode="DISTANCE"></mutation><field name="MODE">DISTANCE</field><field name="SENSORPORT">U3</field><field name="SLOT"></field></block></value></block></instance></block_set></program><config><block_set xmlns="http://de.fhg.iais.roberta.blockly" robottype="calliope" xmlversion="3.1" description="" tags=""><instance x="1104" y="40"><block type="robConf_ultrasonic_hcsr04" id="cI*AYI1F5Z_Zx;TE:Z3W" intask="true"><field name="NAME">U1</field><field name="FACTOR">58</field><field name="TRIG">C17</field><field name="ECHO">C16</field></block></instance><instance x="-34" y="120"><block type="robConf_key" id="~wk*JZQvB.fj=I`N#NWp" intask="true" deletable="false"><field name="NAME">B</field><field name="PIN1">B</field></block></instance><instance x="197" y="162"><block type="robConf_sound" id="e:4V0FcDx%9,.7xO,Ns@" intask="true" deletable="false"><field name="NAME">_S</field></block></instance><instance x="197" y="190"><block type="robConf_light" id="9Ktt(7_4+XmKC0]5Zha?" intask="true" deletable="false"><field name="NAME">_L</field></block></instance><instance x="80" y="211"><block type="robConf_gyro" id="lIT?C5y|(7FWCQIUn~pc" intask="true" deletable="false"><field name="NAME">_G</field></block></instance><instance x="80" y="239"><block type="robConf_compass" id="O%Oi0Tq`hGEvrgHGPYY=" intask="true" deletable="false"><field name="NAME">_C</field></block></instance><instance x="250" y="250"><block type="robConf_robot" id="robot" intask="false" deletable="false"><field name="ROBOT">undefined</field></block></instance><instance x="1104" y="209"><block type="robConf_ultrasonic_hcsr04" id="E%{`vd#|{ke5gcGBmcs3" intask="true"><field name="NAME">U2</field><field name="FACTOR">58</field><field name="TRIG">C10</field><field name="ECHO">C11</field></block></instance><instance x="-34" y="270"><block type="robConf_key" id="Kbp:=x@[email protected]~NH|1|e" intask="true" deletable="false"><field name="NAME">A</field><field name="PIN1">A</field></block></instance><instance x="80" y="268"><block type="robConf_accelerometer" id="#(ufvG,s^FcqI,aFn/A{" intask="true" deletable="false"><field name="NAME">_A</field></block></instance><instance x="80" y="378"><block type="robConf_temperature" id="0y].dnS)P)~Jjzeu3/0C" intask="true" deletable="false"><field name="NAME">_T</field></block></instance><instance x="-34" y="422"><block type="robConf_rgbled" id="lgi%C3,J!smR:eP0aal_" intask="true" deletable="false"><field name="NAME">_R</field></block></instance><instance x="1100" y="385"><block type="robConf_ultrasonic" id="?d=~1mSy0`aasZwfLO2X" intask="true"><field name="NAME">U3</field><field name="PIN1">1</field></block></instance><instance x="-34" y="450"><block type="robConf_buzzer" id="}%d2_68IJd*7Oy{qrMR," intask="true" deletable="false"><field name="NAME">_B</field></block></instance></block_set></config></export>

0 comments on commit 10e1607

Please sign in to comment.