Android Documentation - GallopingSnaiI/SmartCar GitHub Wiki

Contents

...

Main Activity

The Main Activity contains all the views and functionality that the user will in- teract with. Along with the standard button views, there is the pathDrawView, and the manualControlView, which will be described below.

In the MainActivity, there are a couple important methods that will need to be understood before changes can be made to the system.

runBT()

The runBT method gets called when the user presses the connect button. It creates new instances of everything that is needed to make a connection between the android device and the arduino's bluetooth module.

There is a single static field, the OutPutStream, which is mentioned from both the pathDrawView and the manualControlView.

onRestart()

On restart is important to the MainActivity because it gets called every time the user enters the activity from the settings menu. Any changes the user made while in the settings will be revalidated automatically to show the changes.

replaceView()

replaceView is called whenever the swap button is pressed. It efficiently handles the visibility of the pathDrawView and manualControlView so that the structure of the activity remains the same when they are switched out.

Path Draw View

validateLine()

The Validate Line method is called whenever the users lifts his finger from the screen. indicating they have finished drawing their path. It runs a number of methods to improves the line quality, such as removing duplicate points. It also calls simplifyLines() and toStringList().

simplifyLines()

The Simplify Lines method is called by ValidateLine(). It uses and implemen- tation of the Douglas-Peucker algorithm to remove points from the line which do not contribute to the overall shape. Depending on the settings chosen by the user more or less points are removed by the algorithm.

toStringList()

This method is called by validate line. It reads the list of points given by the user when they drew the line, and converts into an instruction that the arduino car can carry out. It does this by looking at every set of three adjacent points (That is, every pair of adjacent line segments) and calculating: The length of the first line segment, in regards to the scale of the draw view defined by the user The angle between the two line segments, and if that angle is clockwise or anti-clockwise These instructions are compiled into an array of Strings, such as:

goForward 50*
rotateClockwise 90*
goForward 30*
rotateCounterClockwise 45*
goForward 20*
(These would be the instructions for a path with three line segments, and four points)

Note: The angles are in degrees, and the distances are in centimeters.

ManualControlView

sendBluetoothInstruction

This method sends single strings as instructions through the static Output- Stream field that was created in the MainActivity. The commands are designed to make the car execute that instruction until a new one is given, or until the joystick returns to the center and sends a \stop" instruction.

####onTouchevent() This method overrides the parent method. If the touch event is a Move or a Down event, then the method checks the region of the screen that the user is pressing, and if relevant, sends a manual control bluetooth command to the car. If the touch event is an up event, then the stop command is sent to the car.

##Settings Activity The settings activity is structured in a way to allow for future modification. The settings activity implements an instance of SettingsFragment. A new settings fragment can be created and used by the current settings activity without having to make any changes to the system itself.

####onActivityResult() The onActivityResult method handles the result of a selection made from the phone's external storage. This happens after the user has pressed the change background button. When the user chooses a valid image, the onActivityResult rotates and resizes the image when applicable, and sets it as the background.