Guest User

Untitled

a guest
Sep 22nd, 2018
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.83 KB | None | 0 0
  1. package com.example;
  2.  
  3. import android.app.Activity;
  4. import android.content.Intent;
  5. import android.os.Bundle;
  6. import android.view.MotionEvent;
  7. import android.view.View;
  8. import android.widget.CheckBox;
  9. import android.widget.EditText;
  10. import android.widget.TextView;
  11. import orbotix.robot.app.StartupActivity;
  12. import orbotix.robot.base.*;
  13. import orbotix.robot.sensor.AccelerometerData;
  14. import orbotix.robot.sensor.AttitudeData;
  15. import orbotix.robot.sensor.DeviceSensorsData;
  16. import orbotix.robot.sensor.LocatorData;
  17. import orbotix.robot.widgets.calibration.CalibrationView;
  18.  
  19. import java.util.List;
  20.  
  21. public class LocatorActivity extends Activity
  22. {
  23.     /**
  24.      * ID for starting the StartupActivity
  25.      */
  26.     private final static int sStartupActivity = 0;
  27.    
  28.     /**
  29.      * Data Streaming Packet Counts
  30.      */
  31.     private final static int TOTAL_PACKET_COUNT = 200;
  32.     private final static int PACKET_COUNT_THRESHOLD = 50;
  33.     private int mPacketCounter;
  34.  
  35.     /**
  36.      * Robot to from which we are streaming
  37.      */
  38.     private Robot mRobot = null;
  39.  
  40.     /**
  41.      * Other stuff
  42.      */
  43.    
  44.     private final static int BOUNDARY_DISTANCE_FROM_CENTER_CM = 160;
  45.     private final static int BACK_IN_BOUNDS_FROM_CENTER_CM = 140;
  46.     private boolean roll_back = false;
  47.    
  48.     /**
  49.      * AsyncDataListener that will be assigned to the DeviceMessager, listen for streaming data, and then do the
  50.      *
  51.      */
  52.     private DeviceMessenger.AsyncDataListener mDataListener = new DeviceMessenger.AsyncDataListener() {
  53.         @Override
  54.         public void onDataReceived(DeviceAsyncData data) {
  55.  
  56.             if(data instanceof DeviceSensorsAsyncData){
  57.                
  58.                 // If we are getting close to packet limit, request more
  59.                 mPacketCounter++;
  60.                 if( mPacketCounter > (TOTAL_PACKET_COUNT - PACKET_COUNT_THRESHOLD) ) {
  61.                     requestDataStreaming();
  62.                 }
  63.  
  64.                 //get the frames in the response
  65.                 List<DeviceSensorsData> data_list = ((DeviceSensorsAsyncData)data).getAsyncData();
  66.                 if(data_list != null){
  67.  
  68.                     // Iterate over each frame, however we set data streaming as only one frame
  69.                     for(DeviceSensorsData datum : data_list){
  70.  
  71.                         LocatorData locatorData = datum.getLocatorData();
  72.                         if( locatorData != null ) {
  73.                             ((TextView)findViewById(R.id.txt_locator_x)).setText(locatorData.getPositionX() + " cm");
  74.                             ((TextView)findViewById(R.id.txt_locator_y)).setText(locatorData.getPositionY() + " cm");
  75.                             ((TextView)findViewById(R.id.txt_locator_vx)).setText(locatorData.getVelocityX() + " cm/s");
  76.                             ((TextView)findViewById(R.id.txt_locator_vy)).setText(locatorData.getVelocityY() + " cm/s");
  77.                            
  78.                             if (roll_back) {
  79.                                 if (locatorData.getPositionX() < BACK_IN_BOUNDS_FROM_CENTER_CM
  80.                                         && locatorData.getPositionX() > -BACK_IN_BOUNDS_FROM_CENTER_CM
  81.                                         && locatorData.getPositionY() < BACK_IN_BOUNDS_FROM_CENTER_CM
  82.                                         && locatorData.getPositionY() > -BACK_IN_BOUNDS_FROM_CENTER_CM) {
  83.                                     RollCommand.sendStop(mRobot);
  84.                                     roll_back = false;
  85.                                 }
  86.                             }
  87.                            
  88.                             else if (locatorData.getPositionX() > BOUNDARY_DISTANCE_FROM_CENTER_CM
  89.                                     || locatorData.getPositionX() < -BOUNDARY_DISTANCE_FROM_CENTER_CM
  90.                                     || locatorData.getPositionY() > BOUNDARY_DISTANCE_FROM_CENTER_CM
  91.                                     || locatorData.getPositionY() < -BOUNDARY_DISTANCE_FROM_CENTER_CM) {
  92.                                 //FrontLEDOutputCommand.sendCommand(mRobot, 255);
  93.                                 RollCommand.sendStop(mRobot);
  94.                                 if (!roll_back) {
  95.                                     // Roll back into bounds
  96.                                    
  97.                                     // Find current angle
  98.                                     double angle = Math.atan2((double) locatorData.getPositionY(), (double) locatorData.getPositionX());
  99.                                     angle = 90 - Math.toDegrees(angle);
  100.                                        
  101.                                     // Invert
  102.                                     if (angle < 180)
  103.                                         angle += 180;
  104.                                     else
  105.                                         angle -= 180;
  106.                                
  107.                                     roll_back = true;
  108.                                     RollCommand.sendCommand(mRobot, (int) angle, 0.6f);
  109.                                 }
  110.                                
  111.                             } else {
  112.                                 FrontLEDOutputCommand.sendCommand(mRobot, 0);
  113.                             }
  114.                         }
  115.                     }
  116.                 }
  117.                
  118.             }
  119.         }
  120.     };
  121.  
  122.  
  123.     /** Called when the activity is first created. */
  124.     @Override
  125.     public void onCreate(Bundle savedInstanceState)
  126.     {
  127.         super.onCreate(savedInstanceState);
  128.         setContentView(R.layout.main);
  129.  
  130.         // Show the StartupActivity to connect to Sphero
  131.         startActivityForResult(new Intent(this, StartupActivity.class), sStartupActivity);
  132.     }
  133.  
  134.     @Override
  135.     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  136.         super.onActivityResult(requestCode, resultCode, data);
  137.  
  138.         if(resultCode == RESULT_OK){
  139.  
  140.             if(requestCode == sStartupActivity){
  141.  
  142.                 //Get the Robot from the StartupActivity
  143.                 String id = data.getStringExtra(StartupActivity.EXTRA_ROBOT_ID);
  144.                 mRobot = RobotProvider.getDefaultProvider().findRobot(id);
  145.  
  146.                 // Start streaming Locator values
  147.                 requestDataStreaming();
  148.                
  149.                 //Set the AsyncDataListener that will process each response.
  150.                 DeviceMessenger.getInstance().addAsyncDataListener(mRobot, mDataListener);
  151.  
  152.                 // Let Calibration View know which robot we are connected to
  153.                 CalibrationView calibrationView = (CalibrationView)findViewById(R.id.calibration_widget);
  154.                 calibrationView.setRobot(mRobot);
  155.             }
  156.         }
  157.     }
  158.  
  159.     /**
  160.      * Calibrate Sphero when a two finger event occurs
  161.      */
  162.     public boolean dispatchTouchEvent(MotionEvent event) {
  163.         CalibrationView calibrationView = (CalibrationView)findViewById(R.id.calibration_widget);
  164.         // Notify Calibration widget of a touch event
  165.         calibrationView.interpretMotionEvent(event);
  166.         return super.dispatchTouchEvent(event);
  167.     }
  168.  
  169.     @Override
  170.     protected void onStop() {
  171.         super.onStop();
  172.  
  173.         if(mRobot != null){
  174.             // Make sure the ball doesn't roll across the world
  175.             RollCommand.sendStop(mRobot);
  176.             // Disconnect properly
  177.             RobotProvider.getDefaultProvider().disconnectControlledRobots();
  178.         }
  179.     }
  180.  
  181.     private void requestDataStreaming(){
  182.  
  183.         if(mRobot == null) return;
  184.  
  185.         // Set up a bitmask containing the sensor information we want to stream, in this case locator
  186.         // with which only works with Firmware 1.20 or greater.
  187.         final long mask = SetDataStreamingCommand.DATA_STREAMING_MASK_LOCATOR_ALL;
  188.  
  189.         //Specify a divisor. The frequency of responses that will be sent is 400hz divided by this divisor.
  190.         final int divisor = 50;
  191.  
  192.         //Specify the number of frames that will be in each response. You can use a higher number to "save up" responses
  193.         //and send them at once with a lower frequency, but more packets per response.
  194.         final int packet_frames = 1;
  195.  
  196.         // Reset finite packet counter
  197.         mPacketCounter = 0;
  198.        
  199.         // Count is the number of async data packets Sphero will send you before
  200.         // it stops.  You want to register for a finite count and then send the command
  201.         // again once you approach the limit.  Otherwise data streaming may be left
  202.         // on when your app crashes, putting Sphero in a bad state
  203.         final int response_count = TOTAL_PACKET_COUNT;
  204.  
  205.  
  206.         // Send this command to Sphero to start streaming.  
  207.         // If your Sphero is on Firmware less than 1.20, Locator values will display as 0's
  208.         SetDataStreamingCommand.sendCommand(mRobot, divisor, packet_frames, mask, response_count);
  209.     }
  210.  
  211.     /**
  212.      * When the user clicks the configure button, it calls this function
  213.      * @param v
  214.      */
  215.     public void configurePressed(View v) {
  216.  
  217.         if( mRobot == null ) return;
  218.  
  219.         int newX = 0;   // The locator's current X position value will be set to this value
  220.         int newY = 0;   // The locator's current Y position value will be set to this value
  221.         int newYaw = 0; // The yaw value you set this to, will represent facing down the +y_axis
  222.  
  223.         // Try parsing the integer values from the edit text boxes, if not, use zeros
  224.         try {
  225.             newX = Integer.parseInt(((EditText)findViewById(R.id.edit_new_x)).getText().toString());
  226.         } catch (NumberFormatException e) {}
  227.  
  228.         try {
  229.             newY = Integer.parseInt(((EditText)findViewById(R.id.edit_new_y)).getText().toString());
  230.         } catch (NumberFormatException e) {}
  231.  
  232.         try {
  233.             newYaw = Integer.parseInt(((EditText)findViewById(R.id.edit_new_yaw)).getText().toString());
  234.         } catch (NumberFormatException e) {}
  235.  
  236.         // Flag will be true if the check box is clicked, false if it is not
  237.         // When the flag is off (default behavior) the x, y locator grid is rotated with the calibration
  238.         // When the flag is on the x, y locator grid is fixed and Sphero simply calibrates within it
  239.         int flag = ((CheckBox)findViewById(R.id.checkbox_flag)).isChecked() ?
  240.                         ConfigureLocatorCommand.ROTATE_WITH_CALIBRATE_FLAG_ON :
  241.                         ConfigureLocatorCommand.ROTATE_WITH_CALIBRATE_FLAG_OFF;
  242.  
  243.         ConfigureLocatorCommand.sendCommand(mRobot, flag, newX, newY, newYaw);
  244.     }
  245.  
  246.     public void upPressed(View v) {
  247.         RollCommand.sendCommand(mRobot, 0, 0.6f);
  248.     }
  249.  
  250.     public void rightPressed(View v) {
  251.         RollCommand.sendCommand(mRobot, 90, 0.6f);
  252.     }
  253.  
  254.     public void downPressed(View v) {
  255.         RollCommand.sendCommand(mRobot, 180, 0.6f);
  256.     }
  257.  
  258.     public void leftPressed(View v) {
  259.         RollCommand.sendCommand(mRobot, 270, 0.6f);
  260.     }
  261.  
  262.     public void stopPressed(View v) {
  263.         RollCommand.sendStop(mRobot);
  264.     }
  265. }
Add Comment
Please, Sign In to add comment