1- import 'package:collection/collection.dart' ;
21import 'package:flutter/material.dart' ;
32
43import '../../viam_sdk.dart' ;
5- import '../../widgets.dart' ;
4+ import 'arm_widgets/joint_positions_widget.dart' ;
5+ import 'arm_widgets/pose_widget.dart' ;
6+
7+ class ArmNotifier extends ChangeNotifier {
8+ void armHasMoved () {
9+ notifyListeners ();
10+ }
11+ }
612
713/// A widget to control an [Arm] .
814class ViamArmWidget extends StatefulWidget {
@@ -18,154 +24,22 @@ class ViamArmWidget extends StatefulWidget {
1824 State <ViamArmWidget > createState () => _ViamArmWidgetState ();
1925}
2026
21- enum _PoseField {
22- x,
23- y,
24- z,
25- theta,
26- oX,
27- oY,
28- oZ;
29-
30- String get title {
31- switch (this ) {
32- case x:
33- return 'X' ;
34- case y:
35- return 'Y' ;
36- case z:
37- return 'Z' ;
38- case theta:
39- return 'Theta' ;
40- case oX:
41- return 'OX' ;
42- case oY:
43- return 'OY' ;
44- case oZ:
45- return 'OZ' ;
46- }
47- }
48- }
49-
5027class _ViamArmWidgetState extends State <ViamArmWidget > {
51- Pose endPosition = Pose ();
52- List <double > jointPositions = [];
53-
54- Future <void > _getPositions () async {
55- final ep = await widget.arm.endPosition ();
56- final jp = await widget.arm.jointPositions ();
57- setState (() {
58- jointPositions = jp;
59- endPosition = ep;
60- });
61- }
28+ final ArmNotifier _armNotifier = ArmNotifier ();
6229
6330 @override
64- void initState () {
65- super .initState ();
66- _getPositions ();
67- }
68-
69- Future <void > updateEndPosition (_PoseField field, double increment) async {
70- final ep = endPosition;
71- switch (field) {
72- case _PoseField .x:
73- ep.x += increment;
74- case _PoseField .y:
75- ep.y += increment;
76- case _PoseField .z:
77- ep.z += increment;
78- case _PoseField .theta:
79- ep.theta += increment;
80- case _PoseField .oX:
81- ep.oX += increment;
82- case _PoseField .oY:
83- ep.oY += increment;
84- case _PoseField .oZ:
85- ep.oZ += increment;
86- }
87-
88- await widget.arm.moveToPosition (ep);
89- await _getPositions ();
90- }
91-
92- Future <void > updateJointPosition (int joint, double increment) async {
93- final jp = jointPositions;
94- jp[joint] += increment;
95- await widget.arm.moveToJointPositions (jp);
96- await _getPositions ();
97- }
98-
99- TableRow _getEndPositionRow (_PoseField field) {
100- double value;
101- switch (field) {
102- case _PoseField .x:
103- value = endPosition.x;
104- case _PoseField .y:
105- value = endPosition.y;
106- case _PoseField .z:
107- value = endPosition.z;
108- case _PoseField .theta:
109- value = endPosition.theta;
110- case _PoseField .oX:
111- value = endPosition.oX;
112- case _PoseField .oY:
113- value = endPosition.oY;
114- case _PoseField .oZ:
115- value = endPosition.oZ;
116- }
117-
118- return TableRow (children: [
119- _ArmTableCell (Text (field.title, textAlign: TextAlign .end)),
120- _ArmTableCell (ViamButton (onPressed: () => updateEndPosition (field, - 10 ), text: '--' , size: ViamButtonSizeClass .small)),
121- _ArmTableCell (ViamButton (onPressed: () => updateEndPosition (field, - 1 ), text: '-' , size: ViamButtonSizeClass .small)),
122- _ArmTableCell (Text (value.toStringAsFixed (2 ), textAlign: TextAlign .center)),
123- _ArmTableCell (ViamButton (onPressed: () => updateEndPosition (field, 1 ), text: '+' , size: ViamButtonSizeClass .small)),
124- _ArmTableCell (ViamButton (onPressed: () => updateEndPosition (field, 10 ), text: '++' , size: ViamButtonSizeClass .small)),
125- ]);
31+ void dispose () {
32+ _armNotifier.dispose ();
33+ super .dispose ();
12634 }
12735
12836 @override
12937 Widget build (BuildContext context) {
13038 return Column (
131- mainAxisAlignment: MainAxisAlignment .center,
132- crossAxisAlignment: CrossAxisAlignment .center,
13339 children: [
134- const Text ('End Positions (mm)' , style: TextStyle (fontWeight: FontWeight .bold)),
135- Table (
136- columnWidths: const {0 : IntrinsicColumnWidth ()},
137- defaultVerticalAlignment: TableCellVerticalAlignment .middle,
138- children: _PoseField .values.map ((e) => _getEndPositionRow (e)).toList (),
139- ),
140- const SizedBox (height: 16 ),
141- const Text ('Joints (degrees)' , style: TextStyle (fontWeight: FontWeight .bold)),
142- Table (
143- columnWidths: const {0 : IntrinsicColumnWidth ()},
144- defaultVerticalAlignment: TableCellVerticalAlignment .middle,
145- children: jointPositions
146- .mapIndexed ((index, element) => TableRow (children: [
147- _ArmTableCell (Text ('Joint $index ' , textAlign: TextAlign .end)),
148- _ArmTableCell (
149- ViamButton (onPressed: () => updateJointPosition (index, - 10 ), text: '--' , size: ViamButtonSizeClass .small)),
150- _ArmTableCell (ViamButton (onPressed: () => updateJointPosition (index, - 1 ), text: '-' , size: ViamButtonSizeClass .small)),
151- _ArmTableCell (Text (element.toStringAsFixed (2 ), textAlign: TextAlign .center)),
152- _ArmTableCell (ViamButton (onPressed: () => updateJointPosition (index, 1 ), text: '+' , size: ViamButtonSizeClass .small)),
153- _ArmTableCell (ViamButton (onPressed: () => updateJointPosition (index, 10 ), text: '++' , size: ViamButtonSizeClass .small)),
154- ]))
155- .toList (),
156- )
40+ JointPositionsWidget (arm: widget.arm, updateNotifier: _armNotifier),
41+ PoseWidget (arm: widget.arm, updateNotifier: _armNotifier),
15742 ],
15843 );
15944 }
16045}
161-
162- class _ArmTableCell extends StatelessWidget {
163- final Widget child;
164-
165- const _ArmTableCell (this .child);
166-
167- @override
168- Widget build (BuildContext context) {
169- return TableCell (child: Padding (padding: const EdgeInsets .symmetric (horizontal: 2 , vertical: 1 ), child: child));
170- }
171- }
0 commit comments