This method is used to draw the map with the obstacles, waypoint and robot.
protected void onDraw(Canvas canvas) { //pseudo-code is shown here determine how long (i.e. height) the entire map will be; determine how wide (i.e. width) the entire map will be; adjusting the map cell when in different orientation (i.e. portrait/horizontal); draw background (unexplored map); draw explored map & obstacles; draw waypoint; draw robot; }
The onTouch() function will tell users the cell that they have selected on the map and show a toast of the point selected at the bottom of the screen.
public boolean onTouch(View view, MotionEvent me) { //calculation to get the tapped position from view geometry location ... X = me.getX(); Y = me.getY(); selectedX = X- paddingX; selectedY = Y- paddingY; posX = (int)(selectedX/cellWidth); posY = 19-(int)(selectedY/cellHeight); //to remember the last position X and Y lastX = posX; lastY = posY; toastText = "tapped " + posX + ", " + posY; showToast(toastText); ... }
The onFling() function will allow users to swipe on the map to move the robot on the map.
public boolean onFling (MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { boolean result = false; try { //determine if user is swiping left or right ... if (Math.abs(diffX) > Math.abs(diffY)) { if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) { if (diffX > 0) onSwipeRight(); else onSwipeLeft(); result = true; } //determine if the user is swiping up or down else if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) { if (diffY > 0) { onSwipeBottom(); }else onSwipeTop(); result = true; } } ... }
How do you animate the movement of robot
When the message is received by Android, it will decode the message received and animate the movement.
public void incomingMessage(String readMsg) { //pseudo-code is shown here //fastest path message if(message_header == FP)){ check if there are multiple movements; if there are multiple movements, run in a for loop. for (each movement in the message){ if (robot moves forward) move forward by 10cm; if (robot rotate to the right) rotate the robot to the right by 90 deg; if (robot rotate to the left) rotate the robot to the left by 90 deg; }
Messages exchanged between Raspberry Pi and Android
The messages exchanged are described here under the section Interface Message.
The setConfiguredString() function will store the non-volatile string.
private void setConfiguredString(final int index){ ... //get user input to set the non-volatile string using dialog GUI new AlertDialog.Builder(this) .setTitle("Configure String "+index) .setView(txtField) //Store string from textfield into shared preference .setPositiveButton("Save", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { String input = txtField.getText().toString(); SharedPreferences.Editor editor = getSharedPreferences(String.valueOf(R.string.app_name), MODE_PRIVATE).edit(); editor.putString("string"+index, input); editor.apply(); } ... }