(* Content-type: application/vnd.wolfram.mathematica *) (*** Wolfram Notebook File ***) (* http://www.wolfram.com/nb *) (* CreatedBy='Mathematica 8.0' *) (*CacheID: 234*) (* Internal cache information: NotebookFileLineBreakTest NotebookFileLineBreakTest NotebookDataPosition[ 157, 7] NotebookDataLength[ 21157, 551] NotebookOptionsPosition[ 20148, 512] NotebookOutlinePosition[ 20514, 528] CellTagsIndexPosition[ 20471, 525] WindowFrame->Normal*) (* Beginning of Notebook Content *) Notebook[{ Cell[CellGroupData[{ Cell["\<\ Controling your robot with a virtual joystick\ \>", "Title", CellChangeTimes->{{3.527204777664669*^9, 3.527204790472292*^9}}], Cell["\<\ This notebook creates a panel in which you can move a joystick. The position \ of the joystick in turns control the power of the motors. All the functions \ loads automatically when you open the notebook.\ \>", "Subsubtitle", CellChangeTimes->{{3.5272047943098984`*^9, 3.5272048308607626`*^9}, { 3.5272051569233336`*^9, 3.527205177062969*^9}, {3.530981190463523*^9, 3.530981215285943*^9}}], Cell["\<\ Architecture: Two Motors, one on port A, the other on port B control the left \ and the right wheels.\ \>", "Subsubtitle", CellChangeTimes->{{3.527204834183569*^9, 3.5272048603448143`*^9}, { 3.527205295595977*^9, 3.527205296703579*^9}}], Cell["\<\ Note: the position of the joystick is given by the position of the red dot. \ Click and move the dot, while maintaining the button pressed. A simple click \ reposition the dot in the center.\ \>", "Subsubtitle", CellChangeTimes->{{3.5272049879374385`*^9, 3.5272050331619177`*^9}}], Cell[CellGroupData[{ Cell["\<\ Step 1: Load the NXT4Math package (adjust path for your computer)\ \>", "Subsection", CellChangeTimes->{{3.5272048711244335`*^9, 3.527204889173665*^9}, { 3.527204934647745*^9, 3.527204936878549*^9}, {3.5273304951587906`*^9, 3.527330502428404*^9}, {3.5273309185487337`*^9, 3.5273309207483377`*^9}, { 3.530980484321134*^9, 3.5309804920985785`*^9}}], Cell[BoxData[{ RowBox[{ RowBox[{"SetDirectory", "[", RowBox[{ "$InstallationDirectory", "<>", " ", "\"\<\\\\AddOns\\\\ExtraPackages\\\\SerialIO\\\\\>\"", "<>", "$SystemID"}], "]"}], ";"}], "\[IndentingNewLine]", RowBox[{"Needs", "[", RowBox[{"\"\\"", ",", RowBox[{ "$HomeDirectory", "<>", "\"\<\\\\Desktop\\\\LegoNXT\\\\Code\\\\Mathematica\\\\Math4NXT.m\>\""}]}], "]"}]}], "Input", InitializationCell->True, CellChangeTimes->{{3.5272035897433844`*^9, 3.527203617402233*^9}, 3.5272054966307297`*^9, 3.5272056268681583`*^9, {3.530825704767642*^9, 3.5308257133164573`*^9}, 3.530980419093403*^9}], Cell[TextData[{ StyleBox["Math4NXT", FontWeight->Bold, FontColor->RGBColor[1, 0, 0]], " loaded. \nDo ", StyleBox["? Math4NXT`*", FontSlant->Italic, FontColor->RGBColor[1, 0, 0]], " to see all the functions available \nor ", StyleBox["? NXT*", FontSlant->Italic, FontColor->RGBColor[1, 0, 0]], " for the direct commands and ", StyleBox["? M4N*", FontSlant->Italic, FontColor->RGBColor[1, 0, 0]], " for the higher-level commands." }], "Text", GeneratedCell->True, CellAutoOverwrite->True, CellChangeTimes->{3.5309811651470747`*^9}] }, Closed]], Cell[CellGroupData[{ Cell["Step 2: Define an extended ClickPane.", "Subsection", CellChangeTimes->{{3.527204899500883*^9, 3.5272049315745397`*^9}, { 3.5309810903597975`*^9, 3.5309810908328247`*^9}}], Cell[BoxData[{ RowBox[{ RowBox[{ RowBox[{"AltClick", "::", "usage"}], "=", "\"\<\>\""}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"CtrlClick", "::", "usage"}], "=", "\"\<\>\""}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"ShiftClick", "::", "usage"}], "=", "\"\<\>\""}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"Click", "::", "usage"}], "=", "\"\<\>\""}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"AltMovingMouse", "::", "usage"}], "=", "\"\<\>\""}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"CtrlMovingMouse", "::", "usage"}], "=", "\"\<\>\""}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"ShiftMovingMouse", "::", "usage"}], "=", "\"\<\>\""}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"MovingMouse", "::", "usage"}], "=", "\"\<\>\""}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"AltReleaseMouse", "::", "usage"}], "=", "\"\<\>\""}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"CtrlReleaseMouse", "::", "usage"}], "=", "\"\<\>\""}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"ShiftReleaseMouse", "::", "usage"}], "=", "\"\<\>\""}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{ RowBox[{"ReleaseMouse", "::", "usage"}], "=", "\"\<\>\""}], ";"}], "\[IndentingNewLine]"}], "\[IndentingNewLine]", RowBox[{ RowBox[{"myClickPane", "[", RowBox[{"object_", ",", "opts___"}], "]"}], ":=", RowBox[{"DynamicModule", "[", RowBox[{ RowBox[{"{", RowBox[{"k1", ",", "k2", ",", "k3"}], "}"}], ",", "\[IndentingNewLine]", RowBox[{"Dynamic", "[", "\[IndentingNewLine]", RowBox[{"EventHandler", "[", RowBox[{"object", ",", "\[IndentingNewLine]", RowBox[{"\"\\"", "\[RuleDelayed]", RowBox[{"(", "\[IndentingNewLine]", RowBox[{ RowBox[{"If", "[", RowBox[{ RowBox[{"k1", "=", RowBox[{"CurrentValue", "[", "\"\\"", "]"}]}], ",", RowBox[{"AltClick", "/.", RowBox[{"{", "opts", "}"}]}]}], "]"}], ";", "\[IndentingNewLine]", RowBox[{"If", "[", RowBox[{ RowBox[{"k2", "=", RowBox[{"CurrentValue", "[", "\"\\"", "]"}]}], ",", RowBox[{"CtrlClick", "/.", RowBox[{"{", "opts", "}"}]}]}], "]"}], ";", "\[IndentingNewLine]", RowBox[{"If", "[", RowBox[{ RowBox[{"k3", "=", RowBox[{"CurrentValue", "[", "\"\\"", "]"}]}], ",", RowBox[{"ShiftClick", "/.", RowBox[{"{", "opts", "}"}]}]}], "]"}], ";", "\[IndentingNewLine]", RowBox[{"If", "[", RowBox[{ RowBox[{ RowBox[{"Not", "[", "k1", "]"}], "&&", RowBox[{"Not", "[", "k2", "]"}], "&&", RowBox[{"Not", "[", "k3", "]"}]}], ",", RowBox[{"Click", "/.", RowBox[{"{", "opts", "}"}]}]}], "]"}]}], "\[IndentingNewLine]", ")"}]}], ",", "\[IndentingNewLine]", RowBox[{"\"\\"", "\[RuleDelayed]", RowBox[{"(", "\[IndentingNewLine]", RowBox[{ RowBox[{"If", "[", RowBox[{ RowBox[{"k1", "=", RowBox[{"CurrentValue", "[", "\"\\"", "]"}]}], ",", RowBox[{"AltMovingMouse", "/.", RowBox[{"{", "opts", "}"}]}]}], "]"}], ";", "\[IndentingNewLine]", RowBox[{"If", "[", RowBox[{ RowBox[{"k1", "=", RowBox[{"CurrentValue", "[", "\"\\"", "]"}]}], ",", RowBox[{"CtrlMovingMouse", "/.", RowBox[{"{", "opts", "}"}]}]}], "]"}], ";", "\[IndentingNewLine]", RowBox[{"If", "[", RowBox[{ RowBox[{"k1", "=", RowBox[{"CurrentValue", "[", "\"\\"", "]"}]}], ",", RowBox[{"ShiftMovingMouse", "/.", RowBox[{"{", "opts", "}"}]}]}], "]"}], ";", "\[IndentingNewLine]", RowBox[{"If", "[", RowBox[{ RowBox[{ RowBox[{"Not", "[", "k1", "]"}], "&&", RowBox[{"Not", "[", "k2", "]"}], "&&", RowBox[{"Not", "[", "k3", "]"}]}], ",", RowBox[{"MovingMouse", "/.", RowBox[{"{", "opts", "}"}]}]}], "]"}]}], "\[IndentingNewLine]", ")"}]}], ",", "\[IndentingNewLine]", RowBox[{"\"\\"", "\[RuleDelayed]", RowBox[{"(", "\[IndentingNewLine]", RowBox[{ RowBox[{"If", "[", RowBox[{ RowBox[{"k1", "=", RowBox[{"CurrentValue", "[", "\"\\"", "]"}]}], ",", RowBox[{"AltReleaseMouse", "/.", RowBox[{"{", "opts", "}"}]}]}], "]"}], ";", "\[IndentingNewLine]", RowBox[{"If", "[", RowBox[{ RowBox[{"k1", "=", RowBox[{"CurrentValue", "[", "\"\\"", "]"}]}], ",", RowBox[{"CtrlReleaseMouse", "/.", RowBox[{"{", "opts", "}"}]}]}], "]"}], ";", "\[IndentingNewLine]", RowBox[{"If", "[", RowBox[{ RowBox[{"k1", "=", RowBox[{"CurrentValue", "[", "\"\\"", "]"}]}], ",", RowBox[{"ShiftReleaseMouse", "/.", RowBox[{"{", "opts", "}"}]}]}], "]"}], ";", "\[IndentingNewLine]", RowBox[{"If", "[", RowBox[{ RowBox[{ RowBox[{"Not", "[", "k1", "]"}], "&&", RowBox[{"Not", "[", "k2", "]"}], "&&", RowBox[{"Not", "[", "k3", "]"}]}], ",", RowBox[{"ReleaseMouse", "/.", RowBox[{"{", "opts", "}"}]}]}], "]"}]}], "\[IndentingNewLine]", ")"}]}]}], "\[IndentingNewLine]", "]"}], "\[IndentingNewLine]", "]"}]}], "\[IndentingNewLine]", "]"}]}]}], "Input", InitializationCell->True, CellChangeTimes->{ 3.527203148718612*^9, {3.5272032721952295`*^9, 3.527203279418042*^9}, 3.527203328429727*^9, {3.527203521822466*^9, 3.5272035619145365`*^9}, { 3.5273304683579435`*^9, 3.5273304733967524`*^9}, {3.5273305738133297`*^9, 3.5273306427810507`*^9}, {3.5309806803383455`*^9, 3.5309806818554325`*^9}}] }, Closed]], Cell[CellGroupData[{ Cell["\<\ Step 3: A function that open the port, initialize the robot and start the \ joystick pane\ \>", "Subsection", CellChangeTimes->{{3.527204956253783*^9, 3.5272049780002213`*^9}, { 3.5309805589054003`*^9, 3.530980579415573*^9}}], Cell[BoxData[ RowBox[{ RowBox[{"runit", "[", "port_", "]"}], ":=", RowBox[{"(", "\[IndentingNewLine]", RowBox[{ RowBox[{"mybrick", " ", "=", " ", RowBox[{"SerialOpen", "[", "port", "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"M4NInitialize", "[", RowBox[{"mybrick", ",", RowBox[{"MotorA", "\[Rule]", "TachoMotor"}], ",", RowBox[{"MotorB", "\[Rule]", "TachoMotor"}], ",", RowBox[{"SetVolume", "\[Rule]", "1"}]}], "]"}], ";", "\[IndentingNewLine]", "\[IndentingNewLine]", RowBox[{"DynamicModule", "[", RowBox[{ RowBox[{"{", RowBox[{ RowBox[{"pt", "=", RowBox[{"{", RowBox[{"0", ",", "0"}], "}"}]}], ",", RowBox[{"mult", "=", RowBox[{"{", RowBox[{"1", ",", "1"}], "}"}]}]}], "}"}], ",", "\[IndentingNewLine]", "\[IndentingNewLine]", RowBox[{"myClickPane", "[", "\[IndentingNewLine]", RowBox[{ RowBox[{"Dynamic", "[", "\[IndentingNewLine]", RowBox[{ RowBox[{"pt", "=", RowBox[{"pt", "\[Times]", "mult"}]}], ";", "\[IndentingNewLine]", RowBox[{"M4NRunMotor", "[", RowBox[{"mybrick", ",", "MOTORA", ",", RowBox[{"MotorPower", "\[Rule]", RowBox[{"Round", "[", RowBox[{"100", " ", RowBox[{"If", "[", RowBox[{ RowBox[{ RowBox[{ "pt", "\[LeftDoubleBracket]", "1", "\[RightDoubleBracket]"}], "<", "0"}], ",", RowBox[{"1", "+", RowBox[{ "pt", "\[LeftDoubleBracket]", "1", "\[RightDoubleBracket]"}]}], " ", ",", "1"}], "]"}], RowBox[{ "pt", "\[LeftDoubleBracket]", "2", "\[RightDoubleBracket]"}]}], "]"}]}]}], "]"}], ";", "\[IndentingNewLine]", RowBox[{"M4NRunMotor", "[", RowBox[{"mybrick", ",", "MOTORB", ",", RowBox[{"MotorPower", "\[Rule]", RowBox[{"Round", "[", RowBox[{"100", " ", RowBox[{"If", "[", RowBox[{ RowBox[{ RowBox[{ "pt", "\[LeftDoubleBracket]", "1", "\[RightDoubleBracket]"}], ">", "0"}], ",", RowBox[{"1", "-", RowBox[{ "pt", "\[LeftDoubleBracket]", "1", "\[RightDoubleBracket]"}]}], " ", ",", "1"}], "]"}], RowBox[{ "pt", "\[LeftDoubleBracket]", "2", "\[RightDoubleBracket]"}]}], "]"}]}]}], "]"}], ";", "\[IndentingNewLine]", "\[IndentingNewLine]", RowBox[{"Graphics", "[", RowBox[{ RowBox[{"{", "\[IndentingNewLine]", RowBox[{ RowBox[{"Text", "[", "\[IndentingNewLine]", RowBox[{ RowBox[{"\"\< Speed left = \>\"", "<>", RowBox[{"ToString", "[", RowBox[{"100", " ", RowBox[{"If", "[", RowBox[{ RowBox[{ RowBox[{ "pt", "\[LeftDoubleBracket]", "1", "\[RightDoubleBracket]"}], "<", "0"}], ",", RowBox[{"1", "+", RowBox[{ "pt", "\[LeftDoubleBracket]", "1", "\[RightDoubleBracket]"}]}], " ", ",", "1"}], "]"}], RowBox[{ "pt", "\[LeftDoubleBracket]", "2", "\[RightDoubleBracket]"}]}], "]"}], "<>", "\[IndentingNewLine]", "\"\< Speed rigth = \>\"", "<>", RowBox[{"ToString", "[", RowBox[{"100", " ", RowBox[{"If", "[", RowBox[{ RowBox[{ RowBox[{ "pt", "\[LeftDoubleBracket]", "1", "\[RightDoubleBracket]"}], ">", "0"}], ",", RowBox[{"1", "-", RowBox[{ "pt", "\[LeftDoubleBracket]", "1", "\[RightDoubleBracket]"}]}], " ", ",", "1"}], "]"}], RowBox[{ "pt", "\[LeftDoubleBracket]", "2", "\[RightDoubleBracket]"}]}], "]"}]}], ",", "\[IndentingNewLine]", RowBox[{"{", RowBox[{"0", ",", "0.5"}], "}"}]}], "]"}], ",", "\[IndentingNewLine]", "Red", ",", "\[IndentingNewLine]", RowBox[{"Disk", "[", RowBox[{ RowBox[{"{", RowBox[{ RowBox[{ "pt", "\[LeftDoubleBracket]", "1", "\[RightDoubleBracket]"}], ",", RowBox[{ "pt", "\[LeftDoubleBracket]", "2", "\[RightDoubleBracket]"}]}], "}"}], ",", "0.05"}], "]"}], ",", "\[IndentingNewLine]", "Transparent", ",", "\[IndentingNewLine]", RowBox[{"Rectangle", "[", RowBox[{ RowBox[{"{", RowBox[{ RowBox[{"-", "1.2"}], ",", RowBox[{"-", "1.2"}]}], "}"}], ",", RowBox[{"{", RowBox[{"1.2", ",", "1.2"}], "}"}]}], "]"}]}], "\[IndentingNewLine]", "}"}], ",", "\[IndentingNewLine]", RowBox[{"{", RowBox[{ RowBox[{"ImageSize", "\[Rule]", RowBox[{"{", RowBox[{ RowBox[{"8", " ", "72"}], ",", " ", RowBox[{"8", " ", "72"}]}], "}"}]}], ",", RowBox[{"Frame", "\[Rule]", "True"}]}], "}"}]}], "\[IndentingNewLine]", "]"}]}], "]"}], ",", "\[IndentingNewLine]", RowBox[{"MovingMouse", "\[RuleDelayed]", RowBox[{"(", RowBox[{ RowBox[{"mult", "=", RowBox[{"{", RowBox[{"1", ",", "1"}], "}"}]}], ";", RowBox[{"pt", "=", RowBox[{"MousePosition", "[", "\"\\"", "]"}]}], ";", RowBox[{"pt", "=", RowBox[{"{", RowBox[{ RowBox[{"Max", "[", RowBox[{ RowBox[{"-", "1"}], ",", RowBox[{"Min", "[", RowBox[{"1", ",", RowBox[{ "pt", "\[LeftDoubleBracket]", "1", "\[RightDoubleBracket]"}]}], "]"}]}], "]"}], ",", RowBox[{"Max", "[", RowBox[{ RowBox[{"-", "1"}], ",", RowBox[{"Min", "[", RowBox[{"1", ",", RowBox[{ "pt", "\[LeftDoubleBracket]", "2", "\[RightDoubleBracket]"}]}], "]"}]}], "]"}]}], "}"}]}]}], ")"}]}], ",", "\[IndentingNewLine]", RowBox[{"ReleaseMouse", "\[RuleDelayed]", RowBox[{"(", RowBox[{"mult", "=", RowBox[{"{", RowBox[{"0.2", ",", "1"}], "}"}]}], ")"}]}], ",", "\[IndentingNewLine]", RowBox[{"Click", "\[RuleDelayed]", " ", RowBox[{"(", RowBox[{ RowBox[{"pt", "=", RowBox[{"{", RowBox[{"0", ",", "0"}], "}"}]}], ";", RowBox[{"Beep", "[", "]"}]}], ")"}]}]}], "\[IndentingNewLine]", "]"}]}], "\[IndentingNewLine]", "]"}]}], "\[IndentingNewLine]", ")"}]}]], "Input", InitializationCell->True, CellChangeTimes->{{3.527203183927874*^9, 3.527203262180012*^9}, 3.5272033113512983`*^9, {3.5272033491309633`*^9, 3.5272033790986156`*^9}, { 3.5272034180050845`*^9, 3.5272034737439823`*^9}, {3.5272035813505697`*^9, 3.527203582645372*^9}, {3.5272037429356537`*^9, 3.5272038776262903`*^9}, { 3.5272039088887453`*^9, 3.5272039312903843`*^9}, {3.5272039981529016`*^9, 3.5272040505065937`*^9}, {3.527204111580701*^9, 3.5272042331517143`*^9}, { 3.5272042732125845`*^9, 3.527204273898986*^9}, {3.5272043043346395`*^9, 3.5272043470631146`*^9}, {3.5272044100404253`*^9, 3.527204438479275*^9}, { 3.5272044711813326`*^9, 3.5272045178098145`*^9}, {3.527204605045168*^9, 3.5272046154815865`*^9}, {3.527204742325409*^9, 3.527204747005417*^9}, { 3.527205199105808*^9, 3.527205258325512*^9}, {3.5272055709904604`*^9, 3.527205573814066*^9}, {3.527330653997471*^9, 3.527330694969141*^9}, { 3.527622144027626*^9, 3.527622219516159*^9}, {3.5276225871976023`*^9, 3.527622590863609*^9}, {3.5276226272584724`*^9, 3.5276226708749495`*^9}, { 3.5276227088030148`*^9, 3.527622709473816*^9}, {3.5309805172050147`*^9, 3.5309805441125536`*^9}, {3.530980994789331*^9, 3.530981005354935*^9}}] }, Closed]], Cell[CellGroupData[{ Cell["\<\ Let\[CloseCurlyQuote]s go! (the brick must not be connected as runit will \ open the connection link)\ \>", "Subsection", CellChangeTimes->{{3.527205588306491*^9, 3.527205591613697*^9}, { 3.530981116072268*^9, 3.530981134848342*^9}}], Cell[BoxData[ RowBox[{ RowBox[{"(*", RowBox[{ "press", " ", "shift", " ", "enter", " ", "here", " ", "to", " ", "start", " ", "everything"}], "*)"}], " ", RowBox[{"(*", RowBox[{ "comment", " ", "and", " ", "execute", " ", "again", " ", "to", " ", "stop"}], "*)"}], "\[IndentingNewLine]", RowBox[{"runit", "[", "\"\\"", "]"}]}]], "Input", CellChangeTimes->{{3.527205592924099*^9, 3.5272055935481005`*^9}, { 3.5272056334045696`*^9, 3.5272056386773787`*^9}, {3.5292411890164113`*^9, 3.529241195989623*^9}, 3.5308257282144833`*^9, {3.5309805994997215`*^9, 3.5309806060500965`*^9}, {3.5309813338217225`*^9, 3.5309813592741785`*^9}}] }, Open ]] }, Open ]] }, WindowSize->{917, 637}, WindowMargins->{{Automatic, 218}, {Automatic, 8}}, ShowSelection->True, FrontEndVersion->"8.0 for Microsoft Windows (64-bit) (February 23, 2011)", StyleDefinitions->"Default.nb" ] (* End of Notebook Content *) (* Internal cache information *) (*CellTagsOutline CellTagsIndex->{} *) (*CellTagsIndex CellTagsIndex->{} *) (*NotebookFileOutline Notebook[{ Cell[CellGroupData[{ Cell[579, 22, 136, 3, 83, "Title"], Cell[718, 27, 405, 7, 47, "Subsubtitle"], Cell[1126, 36, 249, 5, 28, "Subsubtitle"], Cell[1378, 43, 291, 5, 47, "Subsubtitle"], Cell[CellGroupData[{ Cell[1694, 52, 364, 6, 36, "Subsection"], Cell[2061, 60, 654, 16, 52, "Input", InitializationCell->True], Cell[2718, 78, 557, 20, 70, "Text"] }, Closed]], Cell[CellGroupData[{ Cell[3312, 103, 180, 2, 28, "Subsection"], Cell[3495, 107, 6490, 165, 752, "Input", InitializationCell->True] }, Closed]], Cell[CellGroupData[{ Cell[10022, 277, 238, 5, 28, "Subsection"], Cell[10263, 284, 8891, 198, 632, "Input", InitializationCell->True] }, Closed]], Cell[CellGroupData[{ Cell[19191, 487, 246, 5, 28, "Subsection"], Cell[19440, 494, 680, 14, 52, "Input"] }, Open ]] }, Open ]] } ] *) (* End of internal cache information *)