1 package com.generalrobotix.ui.view;
3 import java.util.ArrayList;
15 import org.eclipse.jface.dialogs.MessageDialog;
16 import org.eclipse.swt.SWT;
17 import org.eclipse.swt.events.KeyAdapter;
18 import org.eclipse.swt.events.KeyEvent;
19 import org.eclipse.swt.events.SelectionEvent;
20 import org.eclipse.swt.events.SelectionListener;
21 import org.eclipse.swt.graphics.Image;
22 import org.eclipse.swt.layout.GridData;
23 import org.eclipse.swt.layout.GridLayout;
24 import org.eclipse.swt.widgets.Button;
25 import org.eclipse.swt.widgets.Composite;
26 import org.eclipse.swt.widgets.Display;
27 import org.eclipse.swt.widgets.Label;
28 import org.eclipse.swt.widgets.Text;
30 import OpenHRP.RobotHardwareService;
31 import OpenHRP.RobotHardwareServiceHelper;
32 import OpenHRP.StateHolderService;
33 import OpenHRP.StateHolderServiceHelper;
34 import OpenHRP.StateHolderServicePackage.CommandHolder;
35 import OpenHRP.RobotHardwareServicePackage.RobotStateHolder;
36 import OpenHRP.RobotHardwareServicePackage.SwitchStatus;
37 import RTC.ComponentProfile;
38 import RTC.ConnectorProfile;
39 import RTC.ConnectorProfileHolder;
40 import RTC.ExecutionContext;
41 import RTC.LifeCycleState;
42 import RTC.PortInterfaceProfile;
43 import RTC.PortProfile;
44 import RTC.PortService;
46 import RTC.RTObjectHelper;
60 @SuppressWarnings(
"serial")
65 public static final String TITLE =
"RobotHardware RTC Client";
66 private static final int NOT_CONNECTED = 0;
67 private static final int CONNECTING = 1;
68 private static final int CONNECTED = 2;
79 private WorldStateHolder worldStateH_ =
new WorldStateHolder();
80 private RobotStateHolder robotStateH_ =
new RobotStateHolder();
82 private String robotType_ =
"-----";
83 private String robotHost_ =
"localhost";
84 private int robotPort_ = 2809;
85 private int interval_ = 200;
86 private String setupFile_ =
"-----";
87 private String RobotHardwareRTC_ =
"RobotHardware0";
88 private String StateHolderRTC_ =
"StateHolder0";
92 private int state_ = NOT_CONNECTED;
93 private boolean actualFlag =
false;
109 private ArrayList<SetPropertyPanel> propList_ =
new ArrayList<SetPropertyPanel>();
119 super(name, manager, vp, parent);
121 composite_.setLayout(
new GridLayout(1,
false));
122 Composite layout1 =
new Composite(composite_, SWT.NONE);
123 layout1.setLayoutData(
new GridData(GridData.HORIZONTAL_ALIGN_CENTER));
124 GridLayout gridLayout =
new GridLayout(6,
false);
125 gridLayout.marginWidth = 5;
126 gridLayout.horizontalSpacing = 5;
127 layout1.setLayout(gridLayout);
129 btnServo_ =
new Button(layout1, SWT.TOGGLE);
130 btnServo_.setImage(servoOnIcon_);
131 GridData btnGridData =
new GridData();
132 btnGridData.widthHint = 32;
133 btnGridData.heightHint = 32;
134 btnServo_.setLayoutData(btnGridData);
136 lblStatus_ =
new Label(layout1, SWT.NONE);
137 lblStatus_.setText(
"Status");
139 lblLamp_ =
new Label(layout1, SWT.NONE);
140 lblLamp_.setImage(lampOffIcon_);
142 fldStatus_ =
new Text(layout1, SWT.SINGLE | SWT.BORDER);
143 fldStatus_.setText(
"Not Connected.");
144 GridData textGridData =
new GridData();
145 textGridData.widthHint = 100;
146 fldStatus_.setLayoutData(textGridData);
148 btnConnect_ =
new Button(layout1, SWT.TOGGLE);
149 btnConnect_.setLayoutData(btnGridData);
150 btnConnect_.setImage(startMonitorIcon_);
152 btnSetup_ =
new Button(layout1, SWT.NONE);
153 btnSetup_.setText(
"ROBOT SETUP");
155 propertyPanel_ =
new Composite(composite_,SWT.NONE);
156 propertyPanel_.setLayoutData(
new GridData(GridData.HORIZONTAL_ALIGN_CENTER));
157 GridLayout gridLayout1 =
new GridLayout(1,
false);
158 gridLayout.marginWidth = 5;
159 gridLayout.horizontalSpacing = 5;
160 propertyPanel_.setLayout(gridLayout1);
161 propList_.add(
new SetPropertyPanel(propertyPanel_, SWT.NONE,
"Robot Host",
"robotHost",
true, robotHost_));
162 propList_.add(
new SetPropertyPanel(propertyPanel_, SWT.NONE,
"Robot Port",
"robotPort",
true,
new Integer(robotPort_).
toString()));
163 propList_.add(
new SetPropertyPanel(propertyPanel_, SWT.NONE,
"Robot Name",
"ROBOT",
true, robotType_));
164 propList_.add(
new SetPropertyPanel(propertyPanel_, SWT.NONE,
"Interval[ms]",
"interval",
true,
new Integer(interval_).
toString()));
165 propList_.add(
new SetPropertyPanel(propertyPanel_, SWT.NONE,
"Setup File",
"setupFile",
true, setupFile_));
166 propList_.add(
new SetPropertyPanel(propertyPanel_, SWT.NONE,
"RobotHardwareService",
"RobotHardwareServiceRTC",
true, RobotHardwareRTC_));
167 propList_.add(
new SetPropertyPanel(propertyPanel_, SWT.NONE,
"StateHolderService",
"StateHolderRTC",
true, StateHolderRTC_));
169 btnConnect_.addSelectionListener(
new SelectionListener() {
170 public void widgetDefaultSelected(SelectionEvent e) {}
171 public void widgetSelected(SelectionEvent e) {
172 if (btnConnect_.getSelection()) {
175 boolean ans = MessageDialog.openConfirm(getParent().getShell(),
176 "Do you really want to stop monitoring?",
177 "Stopping Robot State Monitor");
182 btnConnect_.setSelection(
true);
188 btnSetup_.setEnabled(
false);
189 btnSetup_.addSelectionListener(
new SelectionListener() {
190 public void widgetDefaultSelected(SelectionEvent e) {}
191 public void widgetSelected(SelectionEvent e) {
192 setupFile_ = getProperty(
"setupFile");
197 btnServo_.setEnabled(
false);
198 btnServo_.addSelectionListener(
new SelectionListener() {
199 public void widgetDefaultSelected(SelectionEvent e) {}
200 public void widgetSelected(SelectionEvent e) {
201 if (btnServo_.getSelection()){
210 if(currentItem_!=
null){
218 GrxWorldStateItem worldStateItem = (GrxWorldStateItem) item;
221 if(currentItem_!=worldStateItem){
222 currentItem_ = worldStateItem;
228 if(currentItem_==worldStateItem){
241 private boolean isLocal =
true;
250 public SetPropertyPanel(Composite parent,
int style, String _title, String _propName,
boolean _isLocal, String _defaultVal) {
251 super(parent, style);
252 GridLayout gridLayout =
new GridLayout(4,
false);
253 gridLayout.marginWidth = 5;
254 gridLayout.horizontalSpacing = 5;
255 this.setLayout(gridLayout);
256 GridData labelGridData =
new GridData();
257 labelGridData.widthHint = 150;
258 label =
new Label(
this, SWT.NONE);
259 label.setText(_title);
260 label.setLayoutData(labelGridData);
261 fld =
new Text(
this, SWT.NONE);
262 GridData textGridData =
new GridData();
263 textGridData.widthHint = 120;
264 fld.setLayoutData(textGridData);
265 set =
new Button(
this, SWT.NONE);
267 resume =
new Button(
this, SWT.NONE);
268 resume.setText(
"Resume");
269 propName = _propName;
271 defaultVal = _defaultVal;
273 fld.addKeyListener(
new KeyAdapter() {
274 public void keyReleased(KeyEvent e){
275 if (e.keyCode == SWT.CR) {
278 boolean hasChanged = !fld.getText().equals(getValue());
279 set.setEnabled(hasChanged);
280 resume.setEnabled(hasChanged);
285 set.setEnabled(
false);
286 set.addSelectionListener(
new SelectionListener() {
287 public void widgetDefaultSelected(SelectionEvent e) {
289 public void widgetSelected(SelectionEvent e) {
294 resume.setEnabled(
false);
295 resume.addSelectionListener(
new SelectionListener() {
296 public void widgetDefaultSelected(SelectionEvent e) {
298 public void widgetSelected(SelectionEvent e) {
305 fld.setEditable(isEditable);
306 resume.setEnabled(isEditable);
307 set.setEnabled(isEditable);
313 str = getProperty(propName);
315 str = manager_.getProjectProperty(propName);
322 String
value = fld.getText();
324 setProperty(propName, value);
326 manager_.setProjectProperty(propName, value);
327 System.out.println(
"("+propName+
","+value+
")");
329 set.setEnabled(
false);
330 resume.setEnabled(
false);
334 fld.setText(getValue());
335 set.setEnabled(
false);
336 resume.setEnabled(
false);
341 if (isMonitorRunning_)
return;
343 isMonitorRunning_ =
true;
344 btnConnect_.setImage(stopMonitorIcon_);
345 btnConnect_.setToolTipText(
"Stop Robot State Monitor");
346 btnConnect_.setSelection(
true);
348 setConnectionState(CONNECTING);
352 Runnable runnable =
new Runnable() {
354 Display display = composite_.getDisplay();
355 if (state_ == CONNECTING){
356 tryConnection(isInteractive);
357 if (!display.isDisposed())
358 display.timerExec(1000,
this);
359 }
else if (state_ == CONNECTED){
362 }
catch(Exception ex){
363 ex.printStackTrace();
365 interval_ = getInt(
"interval", 200);
366 if (!display.isDisposed())
367 display.timerExec(interval_,
this);
372 Display display = composite_.getDisplay();
373 if (!display.isDisposed()){
374 display.timerExec(1, runnable);
381 System.out.println(
"tryConnection()");
382 if (currentItem_ ==
null)
return;
384 for (
int i=0;
i<propList_.size();
i++)
385 propList_.get(
i).resume();
388 robotHost_ = getStr(
"robotHost");
389 if (robotHost_ !=
null){
390 manager_.setProjectProperty(
"nsHost", robotHost_);
393 robotPort_ = Integer.parseInt(manager_.getProjectProperty(
"robotPort"));
394 }
catch (Exception e) {
398 robotType_ = getStr(
"ROBOT");
401 if (currentModel_ ==
null){
402 System.out.println(
"Can't find robot model("+robotType_+
")");
413 System.out.println(
"RobotHardwareRTC_ = "+RobotHardwareRTC_);
415 ExecutionContext
ec = rtc.get_owned_contexts()[0];
417 ec.activate_component(rtc);
419 hwCtrl_ = RobotHardwareServiceHelper.narrow(
findService(rtc,
"service0"));
422 if (isAnyServoOn()) {
423 btnServo_.setSelection(
true);
424 btnServo_.setImage(servoOffIcon_);
425 btnServo_.setToolTipText(
"Servo Off");
427 btnServo_.setSelection(
false);
428 btnServo_.setImage(servoOnIcon_);
429 btnServo_.setToolTipText(
"Servo On");
431 prevDate_ =
new Date();
433 initDynamicsSimulator(
false);
435 setConnectionState(CONNECTED);
437 }
catch (Exception e) {
440 if (isInteractive && currentModel_ ==
null) {
441 MessageDialog.openInformation(getParent().getShell(),
442 "Load Model(" + robotType_ +
") first.",
443 "Start Robot State Monitor");
446 setConnectionState(CONNECTING);
452 btnConnect_.setImage(startMonitorIcon_);
453 btnConnect_.setToolTipText(
"Start Robot State Monitor");
454 btnConnect_.setSelection(
false);
456 setConnectionState(NOT_CONNECTED);
457 isMonitorRunning_ =
false;
463 btnSetup_.setEnabled(
false);
464 btnServo_.setEnabled(
false);
465 fldStatus_.setText(
"Not Connected.");
466 lblLamp_.setImage(lampOffIcon_);
468 for (
int i=0;
i<propList_.size();
i++) {
469 propList_.get(
i).set.setEnabled(
false);
470 propList_.get(
i).resume.setEnabled(
false);
476 btnSetup_.setEnabled(
false);
477 btnServo_.setEnabled(
false);
478 fldStatus_.setText(
"Connecting ...");
483 btnSetup_.setEnabled(
true);
484 btnServo_.setEnabled(
true);
485 fldStatus_.setText(
"Connected.");
486 lblLamp_.setImage(lampOnIcon_);
493 super.restoreProperties();
494 robotHost_ = getStr(
"robotHost");
495 if (robotHost_ !=
null){
496 manager_.setProjectProperty(
"nsHost", robotHost_);
504 return RTObjectHelper.narrow(
obj);
508 ComponentProfile prof = rtc.get_component_profile();
509 PortProfile[] port_prof = prof.port_profiles;
510 PortService port =
null;
511 for (
int i=0;
i<port_prof.length;
i++){
512 PortProfile pp = port_prof[
i];
513 PortInterfaceProfile[]
ifs = pp.interfaces;
514 for (
int j=0;
j<ifs.length;
j++){
515 PortInterfaceProfile aif = ifs[
j];
516 if (aif.instance_name.equals(service)){
521 ConnectorProfile con_prof =
new ConnectorProfile();
522 con_prof.name =
"noname";
523 con_prof.connector_id =
"";
524 con_prof.ports =
new PortService[1];
525 con_prof.ports[0] = port;
526 con_prof.properties =
new _SDOPackage.NameValue[0];
527 ConnectorProfileHolder con_prof_holder =
new ConnectorProfileHolder();
528 con_prof_holder.value = con_prof;
529 port.connect(con_prof_holder);
530 String
ior = con_prof_holder.value.properties[0].value.extract_string();
531 port.disconnect(con_prof_holder.value.connector_id);
538 if (currentItem_ ==
null) {
543 Date now =
new Date();
544 long time = now.getTime();
545 if (prevDate_ !=
null) {
546 time -= prevDate_.getTime();
548 if (state_ == CONNECTING) {
551 if (lblLamp_.getImage() == lampOffIcon_)
552 lblLamp_.setImage(lampOnIcon_);
554 lblLamp_.setImage(lampOffIcon_);
560 if (state_ == CONNECTED && time > interval_) {
563 if (hwCtrl_ !=
null){
564 hwCtrl_.getStatus(robotStateH_);
568 dynamics_.setCharacterAllLinkData(robotType_, LinkDataType.JOINT_VALUE, robotStateH_.value.angle);
574 if (sholder_ ==
null){
575 StateHolderRTC_ = getStr(
"StateHolderRTC", StateHolderRTC_);
577 sholder_ = StateHolderServiceHelper.narrow(
findService(rtc,
"service0"));
579 CommandHolder
com =
new CommandHolder();
580 sholder_.getCommand(com);
581 dynamics_.setCharacterLinkData(robotType_, currentModel_.
rootLink().
getName(), LinkDataType.ABS_TRANSFORM, com.value.baseTransform);
582 dynamics_.setCharacterAllLinkData(robotType_, LinkDataType.JOINT_VALUE, robotStateH_.value.command);
588 dynamics_.calcWorldForwardKinematics();
589 dynamics_.getWorldState(worldStateH_);
591 }
catch (Exception e) {
593 setConnectionState(CONNECTING);
597 SensorState ss =
new SensorState();
598 ss.q = robotStateH_.value.angle;
599 ss.u = robotStateH_.value.torque;
600 ss.force = robotStateH_.value.force;
601 ss.accel = robotStateH_.value.accel;
602 ss.rateGyro = robotStateH_.value.rateGyro;
608 wsx.
time = (now.getTime() - initialDate_.getTime()) / 1000.0;
612 int [] sstate =
new int[robotStateH_.value.servoState.length];
613 for (
int i=0;
i<sstate.length;
i++){
614 sstate[
i] = robotStateH_.value.servoState[
i][0];
617 wsx.
setPowerState(robotType_, robotStateH_.value.voltage, robotStateH_.value.current);
624 if (dynamics_ !=
null && !update) {
628 DynamicsSimulatorFactory dynFactory = DynamicsSimulatorFactoryHelper.narrow(
obj);
629 if (dynamics_ !=
null) {
632 }
catch(Exception e) {
637 dynamics_ = dynFactory.create();
638 dynamics_.registerCharacter(robotType_, currentModel_.
getBodyInfo());
639 dynamics_.init(0.001, IntegrateMethod.EULER, SensorOption.DISABLE_SENSOR);
640 }
catch (Exception e) {
649 if (jythonView_ ==
null) {
657 if (isAnyServoOn()) {
660 if(hwCtrl_ !=
null) {
661 hwCtrl_.power(
"all", SwitchStatus.SWITCH_ON);
664 }
catch(Exception ex){
665 ex.printStackTrace();
669 boolean ans = MessageDialog.openConfirm(getParent().getShell(),
670 "!! Robot Motion Warning (SERVO ON) !!\n" +
671 "Confirm RELAY turned ON.\n" +
672 "Then Push [OK] to servo ON.\n",
677 StateHolderRTC_ = getStr(
"StateHolderRTC", StateHolderRTC_);
679 sholder_ = StateHolderServiceHelper.narrow(
findService(rtc,
"service0"));
681 if (sholder_ !=
null) {
685 }
catch(Exception ex){
686 ex.printStackTrace();
691 hwCtrl_.servo(
"all", SwitchStatus.SWITCH_ON);
694 }
catch(Exception ex){
695 ex.printStackTrace();
699 btnServo_.setImage(servoOffIcon_);
700 btnServo_.setToolTipText(
"Servo Off");
701 }
catch (Exception e) {
703 hwCtrl_.power(
"all", SwitchStatus.SWITCH_OFF);
708 hwCtrl_.power(
"all", SwitchStatus.SWITCH_OFF);
709 btnServo_.setSelection(
false);
714 boolean ans = MessageDialog.openConfirm(getParent().getShell(),
715 "!! Robot Motion Warning (SERVO OFF) !!\n\n" +
716 "Push [OK] to servo OFF.\n",
720 if(hwCtrl_ !=
null) {
721 hwCtrl_.servo(
"all", SwitchStatus.SWITCH_OFF);
722 hwCtrl_.power(
"all", SwitchStatus.SWITCH_OFF);
724 btnServo_.setImage(servoOnIcon_);
725 btnServo_.setToolTipText(
"Servo On");
726 }
catch (Exception e) {
730 btnServo_.setSelection(
true);
735 final int SERVO_STATE_MASK = 0x2;
736 if(hwCtrl_ !=
null) {
737 hwCtrl_.getStatus(robotStateH_);
738 int[][]
state = robotStateH_.value.servoState;
739 for (
int i=0;
i<currentModel_.
getDOF();
i++) {
740 if ((state[
i][0]&SERVO_STATE_MASK) != 0) {
GrxRobotHardwareClientView(String name, GrxPluginManager manager, GrxBaseViewPart vp, Composite parent)
constructor
void setSensorState(String charName, SensorState state)
void registerCharacter(String cname, BodyInfo binfo)
bool toString(IOP::IOR &ior, std::string &iorstr)
void addValue(Double t, Object obj)
RTObject findRTC(String name)
GrxModelItem currentModel_
void addObserver(GrxObserver v)
def findRTC(name, rnc=None)
get RT component
void deleteObserver(GrxObserver v)
GrxWorldStateItem currentItem_
void setTargetState(String charName, double[] targets)
SetPropertyPanel(Composite parent, int style, String _title, String _propName, boolean _isLocal, String _defaultVal)
Image getImage(String iconName)
void setPosition(Integer pos)
DynamicsSimulator dynamics_
static void printErr(String s)
static final int SELECTED_ITEM
static org.omg.CORBA.ORB getORB(String[] argv)
def findService(rtc, port_name, type_name, instance_name)
get a service of RT component
static Activator getDefault()
GrxJythonPromptView getJythonView()
def j(str, encoding="cp932")
DynamicsSimulator initDynamicsSimulator(boolean update)
static final int REMOVE_ITEM
def servoOff(joint="all")
GrxJythonPromptView jythonView_
static String expandEnvVal(String str)
RobotHardware RTC client view.
boolean isMonitorRunning_
void startMonitor(final boolean isInteractive)
static org.omg.CORBA.Object getReference(String id)
void setConnectionState(int state)
StateHolderService sholder_
RobotHardwareService hwCtrl_
void tryConnection(boolean isInteractive)
void registerItemChange(GrxBaseItem item, int event)
void setServoState(String charName, int[] servoStat)
void setEditable(boolean isEditable)
org.omg.CORBA.Object findService(RTObject rtc, String service)
static final int NOTSELECTED_ITEM
void setPowerState(String charName, double voltage, double current)