/* * This file is part of the Remote Sensor Actuator Interface (ReSAI). * * Copyright(c) Andreas Kipp, Frederic Siepmann * http://opensource.cit-ec.de/projects/resai * * This file may be licensed under the terms of of the * GNU Lesser General Public License Version 3 (the ``LGPL''), * or (at your option) any later version. * * Software distributed under the License is distributed * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either * express or implied. See the LGPL for the specific language * governing rights and limitations. * * You should have received a copy of the LGPL along with this * program. If not, go to http://www.gnu.org/licenses/lgpl.html * or write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * * The development of this software was supported by the * Excellence Cluster EXC 277 Cognitive Interaction Technology. * The Excellence Cluster EXC 277 is a grant of the Deutsche * Forschungsgemeinschaft (DFG) in the context of the German * Excellence Initiative. * */ /** * Tab used to script behavior of the icub. * * @author akipp */ package de.unibi.airobots.resaidroid.tabactivities; import java.util.ArrayList; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.AdapterView; import android.widget.AdapterView.OnItemSelectedListener; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.EditText; import android.widget.SeekBar; import android.widget.Spinner; import android.widget.TableRow; import android.widget.TextView; import de.unibi.airobots.resaidroid.R; import de.unibi.airobots.resaidroid.constants.ServerConfig; import de.unibi.airobots.resaidroid.helper.EasyDialog; import de.unibi.airobots.resaidroid.tabactivities.template.TabTemplate; public class TabScripter extends TabTemplate implements SeekBar.OnSeekBarChangeListener, OnItemSelectedListener { private Spinner m_ElementSpinner; private ArrayAdapter m_adapterForElementSpinner; private Spinner m_ScriptSpinner; private ArrayAdapter m_adapterForScriptSpinner; private SeekBar joint0, joint1, joint2, joint3, joint4, joint5; private SeekBar joint6, joint7, joint8, joint9, joint10; private SeekBar joint11, joint12, joint13, joint14, joint15; private SeekBar seekMoveDelay; private TextView txtMoveDelay; private EditText txtSpeech; private int joint0Offset = 0, joint1Offset = 0, joint2Offset = 0; private int joint3Offset = 0, joint4Offset = 0, joint5Offset = 0; private int joint6Offset = 0, joint7Offset = 0, joint8Offset = 0; private int joint9Offset = 0, joint10Offset = 0, joint11Offset = 0; private int joint12Offset = 0, joint13Offset = 0, joint14Offset = 0; private int joint15Offset = 0; private TableRow rowJoint0, rowJoint1, rowJoint2, rowJoint3, rowJoint4; private TableRow rowJoint5, rowJoint6, rowJoint7, rowJoint8, rowJoint9; private TableRow rowJoint10, rowJoint11, rowJoint12, rowJoint13; private TableRow rowJoint14, rowJoint15, rowSpeech; private Button btnTest, btnAdd, btnExecute, btnInterrupt; private ArrayList scripts = new ArrayList(); private String selectedElement = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.scripter); // CREATE VIEW ELEMENTS initElementSpinner(); initScriptSpinner(); initValueFields(); m_ScriptSpinner.setOnItemSelectedListener(this); m_ElementSpinner.setOnItemSelectedListener(this); txtMoveDelay = (TextView) findViewById(R.id.txtMoveDelay); seekMoveDelay = (SeekBar) findViewById(R.id.seekMoveDelay); seekMoveDelay.setOnSeekBarChangeListener(this); // INIT SEEKBAR SELECTION AND SLIDERS setAvaibleSeekBars("Head"); setSliderMinMax(); // REQUEST SCRIPTS sendScriptLine("get_scriptnames", false); // CREATE BUTTONS AND THEIR LISTENERS btnTest = (Button) findViewById(R.id.btnTest); btnTest.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (m_ElementSpinner.getSelectedItem() != null) { sendScriptLine("test", true); } else { EasyDialog.makeToast(getApplicationContext(), "Please select an element first!"); } } }); btnInterrupt = (Button) findViewById(R.id.btnInterrupt); btnInterrupt.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { sendScriptLine("interrupt", false); } }); btnAdd = (Button) findViewById(R.id.btnAdd); btnAdd.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (m_ElementSpinner.getSelectedItem() != null && m_ScriptSpinner.getSelectedItem() != null) { sendScriptLine("add", true); } else { EasyDialog.makeToast(getApplicationContext(), "Please select a script and element first!"); } } }); btnExecute = (Button) findViewById(R.id.btnExecute); btnExecute.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (m_ScriptSpinner.getSelectedItem() != null) { sendScriptLine("execute_script", false); } else { EasyDialog.makeToast(getApplicationContext(), "Please select a script first!"); } } }); } @Override protected void onResume() { // TODO Auto-generated method stub super.onResume(); // REQUEST SCRIPTS sendScriptLine("get_scriptnames", false); } @Override public void processProperties() { messageHandler.post(new Runnable() { public void run() { try { synchronized (receivedProperties) { if (receivedProperties .containsKey("ScripterThread_ACTION")) { if (receivedProperties.get("ScripterThread_ACTION") .equals("scriptnames")) { scripts.clear(); for (String prop : receivedProperties.keySet()) { if (prop.startsWith( "ScripterThread_SCRIPT_", 0)) { scripts.add(receivedProperties .get(prop)); } } initScriptSpinner(); } else if (receivedProperties.get( "ScripterThread_ACTION").equals("error")) { EasyDialog .makeToast( getApplicationContext(), receivedProperties .get("ScripterThread_ERROR_MESSAGE")); } } } } catch (Exception e) { // TODO: handle exception Log.i(TAG, "###ERROR " + e.getMessage()); } } }); } private void setAvaibleSeekBars(String visElement) { rowJoint0.setVisibility(View.GONE); rowJoint1.setVisibility(View.GONE); rowJoint2.setVisibility(View.GONE); rowJoint3.setVisibility(View.GONE); rowJoint4.setVisibility(View.GONE); rowJoint5.setVisibility(View.GONE); rowJoint6.setVisibility(View.GONE); rowJoint7.setVisibility(View.GONE); rowJoint8.setVisibility(View.GONE); rowJoint9.setVisibility(View.GONE); rowJoint10.setVisibility(View.GONE); rowJoint11.setVisibility(View.GONE); rowJoint12.setVisibility(View.GONE); rowJoint13.setVisibility(View.GONE); rowJoint14.setVisibility(View.GONE); rowJoint15.setVisibility(View.GONE); rowSpeech.setVisibility(View.GONE); if (visElement.equals("Head")) { rowJoint0.setVisibility(View.VISIBLE); rowJoint1.setVisibility(View.VISIBLE); rowJoint2.setVisibility(View.VISIBLE); rowJoint3.setVisibility(View.VISIBLE); rowJoint4.setVisibility(View.VISIBLE); rowJoint5.setVisibility(View.VISIBLE); } if (visElement.equals("Torso")) { rowJoint0.setVisibility(View.VISIBLE); rowJoint1.setVisibility(View.VISIBLE); rowJoint2.setVisibility(View.VISIBLE); } if (visElement.equals("Left_Arm") || visElement.equals("Right_Arm")) { rowJoint0.setVisibility(View.VISIBLE); rowJoint1.setVisibility(View.VISIBLE); rowJoint2.setVisibility(View.VISIBLE); rowJoint3.setVisibility(View.VISIBLE); rowJoint4.setVisibility(View.VISIBLE); rowJoint5.setVisibility(View.VISIBLE); rowJoint6.setVisibility(View.VISIBLE); rowJoint7.setVisibility(View.VISIBLE); rowJoint8.setVisibility(View.VISIBLE); rowJoint9.setVisibility(View.VISIBLE); rowJoint10.setVisibility(View.VISIBLE); rowJoint11.setVisibility(View.VISIBLE); rowJoint12.setVisibility(View.VISIBLE); rowJoint13.setVisibility(View.VISIBLE); rowJoint14.setVisibility(View.VISIBLE); rowJoint15.setVisibility(View.VISIBLE); } if (visElement.equals("Left_Leg") || visElement.equals("Right_Leg")) { rowJoint0.setVisibility(View.VISIBLE); rowJoint1.setVisibility(View.VISIBLE); rowJoint2.setVisibility(View.VISIBLE); rowJoint3.setVisibility(View.VISIBLE); rowJoint4.setVisibility(View.VISIBLE); rowJoint5.setVisibility(View.VISIBLE); } if (visElement.equals("Speech")) { rowSpeech.setVisibility(View.VISIBLE); } } private void setSliderMinMax() { if (selectedElement == null) { return; } if (selectedElement.equals("Head")) { joint0Offset = 40; joint0.setMax(30 + joint0Offset); joint0.setProgress(35); joint1Offset = 70; joint1.setMax(60 + joint1Offset); joint1.setProgress(65); joint2Offset = 55; joint2.setMax(55 + joint2Offset); joint2.setProgress(55); joint3Offset = 35; joint3.setMax(15 + joint3Offset); joint3.setProgress(25); joint4Offset = 50; joint4.setMax(52 + joint4Offset); joint4.setProgress(51); joint5Offset = 0; joint5.setMax(90 + joint5Offset); joint5.setProgress(45); } if (selectedElement.equals("Torso")) { joint0Offset = 50; joint0.setMax(50 + joint0Offset); joint0.setProgress(50); joint1Offset = 30; joint1.setMax(30 + joint1Offset); joint1.setProgress(30); joint2Offset = 10; joint2.setMax(70 + joint2Offset); joint2.setProgress(40); } if (selectedElement.equals("Left_Arm") || selectedElement.equals("Right_Arm")) { joint0Offset = 95; joint0.setMax(10 + joint0Offset); joint0.setProgress(52); joint1Offset = 0; joint1.setMax(161 + joint1Offset); joint1.setProgress(80); joint2Offset = 22; joint2.setMax(95 + joint2Offset); joint2.setProgress(63); joint3Offset = 15; joint3.setMax(106 + joint3Offset); joint3.setProgress(60); joint4Offset = 90; joint4.setMax(90 + joint4Offset); joint4.setProgress(90); joint5Offset = 90; joint5.setMax(0 + joint5Offset); joint5.setProgress(45); joint6Offset = 20; joint6.setMax(40 + joint6Offset); joint6.setProgress(30); joint7Offset = 0; joint7.setMax(60 + joint7Offset); joint7.setProgress(30); joint8Offset = 0; joint8.setMax(90 + joint8Offset); joint8.setProgress(45); joint9Offset = 0; joint9.setMax(90 + joint9Offset); joint9.setProgress(45); joint10Offset = 0; joint10.setMax(90 + joint10Offset); joint10.setProgress(45); joint11Offset = 0; joint11.setMax(90 + joint11Offset); joint11.setProgress(45); joint12Offset = 0; joint12.setMax(90 + joint12Offset); joint12.setProgress(45); joint13Offset = 0; joint13.setMax(90 + joint13Offset); joint13.setProgress(45); joint14Offset = 0; joint14.setMax(90 + joint14Offset); joint14.setProgress(45); joint15Offset = 0; joint15.setMax(115 + joint15Offset); joint15.setProgress(57); } if (selectedElement.equals("Left_Leg") || selectedElement.equals("Right_Leg")) { joint0Offset = 30; joint0.setMax(90 + joint0Offset); joint0.setProgress(60); joint1Offset = 0; joint1.setMax(90 + joint1Offset); joint1.setProgress(45); joint2Offset = 80; joint2.setMax(78 + joint2Offset); joint2.setProgress(79); joint3Offset = 125; joint3.setMax(15 + joint3Offset); joint3.setProgress(70); joint4Offset = 20; joint4.setMax(44 + joint4Offset); joint4.setProgress(32); joint5Offset = 22; joint5.setMax(22 + joint5Offset); joint5.setProgress(22); } } private void initValueFields() { // CONNECT JOINTS TO TEXT joint0 = (SeekBar) findViewById(R.id.seekJoint0); joint0.setOnSeekBarChangeListener(this); rowJoint0 = (TableRow) findViewById(R.id.rowJoint0); joint1 = (SeekBar) findViewById(R.id.seekJoint1); joint1.setOnSeekBarChangeListener(this); rowJoint1 = (TableRow) findViewById(R.id.rowJoint1); joint2 = (SeekBar) findViewById(R.id.seekJoint2); joint2.setOnSeekBarChangeListener(this); rowJoint2 = (TableRow) findViewById(R.id.rowJoint2); joint3 = (SeekBar) findViewById(R.id.seekJoint3); joint3.setOnSeekBarChangeListener(this); rowJoint3 = (TableRow) findViewById(R.id.rowJoint3); joint4 = (SeekBar) findViewById(R.id.seekJoint4); joint4.setOnSeekBarChangeListener(this); rowJoint4 = (TableRow) findViewById(R.id.rowJoint4); joint5 = (SeekBar) findViewById(R.id.seekJoint5); joint5.setOnSeekBarChangeListener(this); rowJoint5 = (TableRow) findViewById(R.id.rowJoint5); joint6 = (SeekBar) findViewById(R.id.seekJoint6); joint6.setOnSeekBarChangeListener(this); rowJoint6 = (TableRow) findViewById(R.id.rowJoint6); joint7 = (SeekBar) findViewById(R.id.seekJoint7); joint7.setOnSeekBarChangeListener(this); rowJoint7 = (TableRow) findViewById(R.id.rowJoint7); joint8 = (SeekBar) findViewById(R.id.seekJoint8); joint8.setOnSeekBarChangeListener(this); rowJoint8 = (TableRow) findViewById(R.id.rowJoint8); joint9 = (SeekBar) findViewById(R.id.seekJoint9); joint9.setOnSeekBarChangeListener(this); rowJoint9 = (TableRow) findViewById(R.id.rowJoint9); joint10 = (SeekBar) findViewById(R.id.seekJoint10); joint10.setOnSeekBarChangeListener(this); rowJoint10 = (TableRow) findViewById(R.id.rowJoint10); joint11 = (SeekBar) findViewById(R.id.seekJoint11); joint11.setOnSeekBarChangeListener(this); rowJoint11 = (TableRow) findViewById(R.id.rowJoint11); joint12 = (SeekBar) findViewById(R.id.seekJoint12); joint12.setOnSeekBarChangeListener(this); rowJoint12 = (TableRow) findViewById(R.id.rowJoint12); joint13 = (SeekBar) findViewById(R.id.seekJoint13); joint13.setOnSeekBarChangeListener(this); rowJoint13 = (TableRow) findViewById(R.id.rowJoint13); joint14 = (SeekBar) findViewById(R.id.seekJoint14); joint14.setOnSeekBarChangeListener(this); rowJoint14 = (TableRow) findViewById(R.id.rowJoint14); joint15 = (SeekBar) findViewById(R.id.seekJoint15); joint15.setOnSeekBarChangeListener(this); rowJoint15 = (TableRow) findViewById(R.id.rowJoint15); rowSpeech = (TableRow) findViewById(R.id.rowSpeech); txtSpeech = (EditText) findViewById(R.id.txtSpeech); } public void initElementSpinner() { // ELEMENT SPINNER m_ElementSpinner = (Spinner) findViewById(R.id.spinnerElement); m_adapterForElementSpinner = new ArrayAdapter(this, android.R.layout.simple_spinner_item); m_adapterForElementSpinner .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); m_ElementSpinner.setAdapter(m_adapterForElementSpinner); // TODO Add for safety purpose? // m_adapterForElementSpinner.add("