| 103 |
Kevin |
1 |
package IEEERobotics.IOIOAI.VT;
|
|
|
2 |
|
|
|
3 |
import android.content.Context;
|
|
|
4 |
import android.content.SharedPreferences;
|
|
|
5 |
import android.graphics.Color;
|
|
|
6 |
import android.preference.PreferenceManager;
|
|
|
7 |
import android.view.View;
|
|
|
8 |
import android.view.View.OnClickListener;
|
|
|
9 |
import android.widget.Button;
|
|
|
10 |
import android.widget.LinearLayout;
|
|
|
11 |
import android.widget.TableRow.LayoutParams;
|
|
|
12 |
import ioio.lib.api.IOIO;
|
|
|
13 |
import ioio.lib.api.PwmOutput;
|
|
|
14 |
import ioio.lib.api.exception.ConnectionLostException;
|
|
|
15 |
|
|
|
16 |
public class MotorControl implements OnClickListener{
|
|
|
17 |
private IOIORoboticsActivity _parent;
|
|
|
18 |
private IOIO _ioio;
|
|
|
19 |
private Context _appContext;
|
|
|
20 |
private SharedPreferences _sharedPrefs;
|
|
|
21 |
private View _debugView;
|
|
|
22 |
|
|
|
23 |
private Button _forward;
|
|
|
24 |
private Button _back;
|
|
|
25 |
private Button _left;
|
|
|
26 |
private Button _right;
|
|
|
27 |
|
|
|
28 |
private PwmOutput leftPWM;
|
|
|
29 |
private PwmOutput rightPWM;
|
|
|
30 |
|
|
|
31 |
private static int MOTOR_LEFT = 12;
|
|
|
32 |
private static int MOTOR_RIGHT = 13;
|
|
|
33 |
|
|
|
34 |
private static int TEXT_SIZE = 20;
|
|
|
35 |
private static int SENSITIVITY = 120;
|
|
|
36 |
|
|
|
37 |
public MotorControl(IOIORoboticsActivity parent) {
|
|
|
38 |
_parent = parent;
|
|
|
39 |
_ioio = IOIORoboticsActivity.IOIOInstance_;
|
|
|
40 |
_appContext = IOIORoboticsActivity.context_;
|
|
|
41 |
_sharedPrefs = PreferenceManager.getDefaultSharedPreferences(_appContext);
|
|
|
42 |
|
|
|
43 |
createDebugView();
|
|
|
44 |
initializeOutputs();
|
|
|
45 |
}
|
|
|
46 |
|
|
|
47 |
public void setSensitivity(int sensitivity) {
|
|
|
48 |
SENSITIVITY = sensitivity;
|
|
|
49 |
}
|
|
|
50 |
|
|
|
51 |
public void setMotorSpeed() {
|
|
|
52 |
|
|
|
53 |
}
|
|
|
54 |
|
|
|
55 |
private void initializeOutputs() {
|
|
|
56 |
try {
|
|
|
57 |
leftPWM = _ioio.openPwmOutput(MOTOR_LEFT, SENSITIVITY);
|
|
|
58 |
rightPWM = _ioio.openPwmOutput(MOTOR_RIGHT, SENSITIVITY);
|
|
|
59 |
} catch (ConnectionLostException e) {
|
|
|
60 |
|
|
|
61 |
}
|
|
|
62 |
}
|
|
|
63 |
|
|
|
64 |
public void disconnect() {
|
|
|
65 |
leftPWM.close();
|
|
|
66 |
rightPWM.close();
|
|
|
67 |
}
|
|
|
68 |
|
|
|
69 |
@Override
|
|
|
70 |
public void onClick(View v) {
|
|
|
71 |
Button btn = (Button)v;
|
|
|
72 |
if (btn == _forward) {
|
|
|
73 |
_parent.logMessage("Front");
|
|
|
74 |
} else if (btn == _back) {
|
|
|
75 |
_parent.logMessage("Back");
|
|
|
76 |
} else if (btn == _left) {
|
|
|
77 |
_parent.logMessage("Left");
|
|
|
78 |
} else if (btn == _right) {
|
|
|
79 |
_parent.logMessage("Right");
|
|
|
80 |
}
|
|
|
81 |
}
|
|
|
82 |
|
|
|
83 |
private void createDebugView() {
|
|
|
84 |
_forward = new Button(_appContext);
|
|
|
85 |
_forward.setText("Forwards");
|
|
|
86 |
_forward.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, (float)0.3));
|
|
|
87 |
_forward.setBackgroundColor(Color.BLACK);
|
|
|
88 |
_forward.setTextColor(Color.WHITE);
|
|
|
89 |
_forward.setTextSize(TEXT_SIZE);
|
|
|
90 |
_forward.setOnClickListener(this);
|
|
|
91 |
|
|
|
92 |
_back = new Button(_appContext);
|
|
|
93 |
_back.setText("Backwards");
|
|
|
94 |
_back.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, (float)0.3));
|
|
|
95 |
_back.setBackgroundColor(Color.BLACK);
|
|
|
96 |
_back.setTextColor(Color.WHITE);
|
|
|
97 |
_back.setTextSize(TEXT_SIZE);
|
|
|
98 |
_back.setOnClickListener(this);
|
|
|
99 |
|
|
|
100 |
_left = new Button(_appContext);
|
|
|
101 |
_left.setText("Left");
|
|
|
102 |
_left.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, (float)0.5));
|
|
|
103 |
_left.setBackgroundColor(Color.BLACK);
|
|
|
104 |
_left.setTextColor(Color.WHITE);
|
|
|
105 |
_left.setTextSize(TEXT_SIZE);
|
|
|
106 |
_left.setOnClickListener(this);
|
|
|
107 |
|
|
|
108 |
_right = new Button(_appContext);
|
|
|
109 |
_right.setText("Right");
|
|
|
110 |
_right.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, (float)0.5));
|
|
|
111 |
_right.setBackgroundColor(Color.BLACK);
|
|
|
112 |
_right.setTextColor(Color.WHITE);
|
|
|
113 |
_right.setTextSize(TEXT_SIZE);
|
|
|
114 |
_right.setOnClickListener(this);
|
|
|
115 |
|
|
|
116 |
LinearLayout center = new LinearLayout(_appContext);
|
|
|
117 |
center.setOrientation(LinearLayout.HORIZONTAL);
|
|
|
118 |
center.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, (float)0.25));
|
|
|
119 |
center.addView(_left);
|
|
|
120 |
center.addView(_right);
|
|
|
121 |
|
|
|
122 |
LinearLayout root = new LinearLayout(_appContext);
|
|
|
123 |
root.setOrientation(LinearLayout.VERTICAL);
|
|
|
124 |
root.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, (float)1.0));
|
|
|
125 |
root.addView(_forward);
|
|
|
126 |
root.addView(center);
|
|
|
127 |
root.addView(_back);
|
|
|
128 |
|
|
|
129 |
_debugView = root;
|
|
|
130 |
}
|
|
|
131 |
|
|
|
132 |
public View getDebugView() {
|
|
|
133 |
return _debugView;
|
|
|
134 |
}
|
|
|
135 |
}
|