The Orientation App with Accelerometer Sensors utilizes device sensors to provide real-time orientation data through graphical representation. This README provides an overview of the application structure, database schema, and details of each activity used.
- MainActivity: The main entry point of the application. It initiates sensor data collection and navigates to the AccelerometerActivity.
- AccelerometerActivity: Gathers sensor data from the device's accelerometer and displays real-time orientation data. It also allows exporting orientation data to a CSV file.
- GraphActivity: Displays graphical representations of orientation data collected by the AccelerometerActivity using line charts. Users can visualize roll, pitch, and yaw angles over time and export data to a CSV file.
- FirstScreen: A composable function representing the main screen of the application. It provides a button to initiate sensor data collection.
- AccelerometerScreen: Displays real-time orientation data in the AccelerometerActivity. It also allows users to export orientation data to a CSV file.
- GraphScreen: Displays line charts for roll, pitch, and yaw angles in the GraphActivity. Users can export orientation data to a CSV file.
The activity initializes a SensorManager
instance to interact with the device's sensors. It retrieves the default rotation vector sensor, which provides orientation data in the form of rotation vectors.
kotlinCopy code
private lateinit var sensorManager: SensorManager
private var rotationVectorSensor: Sensor? = null
An instance of the application's Room database (AppDatabase
) is initialized within the onCreate()
method of the activity. This allows for the storage of orientation data collected from the accelerometer.
kotlinCopy code
database = Room.databaseBuilder(applicationContext, AppDatabase::class.java, "my_database")
.build()
The activity implements the SensorEventListener
interface to receive sensor events, specifically from the rotation vector sensor. The onResume()
method registers the sensor listener with the sensor manager, specifying the desired sensor delay.
kotlinCopy code
override fun onResume() {
super.onResume()
rotationVectorSensor?.also { sensor ->
sensorManager.registerListener(
this,
sensor,
SensorManager.SENSOR_DELAY_FASTEST
)
}
}
The onSensorChanged()
method is called whenever sensor data changes. Here, it retrieves the rotation matrix and orientation angles from the sensor event and converts the angles from radians to degrees. The orientation angles (roll, pitch, and yaw) are then used to update the UI.
kotlinCopy code
override fun onSensorChanged(event: SensorEvent) {
if (event.sensor.type == Sensor.TYPE_ROTATION_VECTOR) {
SensorManager.getRotationMatrixFromVector(rotationMatrix, event.values)
SensorManager.getOrientation(rotationMatrix, orientationAngles)
// Convert angles from radians to degrees
orientationAngles = orientationAngles.map { it * 180 / Math.PI.toFloat() }.toFloatArray()
// Ensure that pitch value is not null
val pitch = orientationAngles.getOrElse(1) { 0f }
updateUI(orientationAngles[0], pitch, orientationAngles[2])
}
}
The updateUI()
function is responsible for updating the user interface with the latest orientation data. It recomposes the AccelerometerScreen
composable function with the new roll, pitch, and yaw angles.
The AccelerometerScreen
composable function displays the real-time orientation data on the UI. It includes text elements showing the roll, pitch, and yaw angles, as well as a button to navigate to the GraphActivity
for further visualization.
The saveOrientationData()
function within the AccelerometerScreen
composable saves the orientation data (roll, pitch, yaw, and timestamp) to the Room database using coroutines. This ensures that the data is persisted for future use.
- orientation_data
id
: Primary key, auto-generated unique identifier for each orientation data entry.roll
: Float, the roll angle representing the device's orientation around the X-axis.pitch
: Float, the pitch angle representing the device's orientation around the Y-axis.yaw
: Float, the yaw angle representing the device's orientation around the Z-axis.timestamp
: Long, timestamp indicating the time when the orientation data was collected.
-
Insert Data:
INSERT INTO orientation_data (roll, pitch, yaw, timestamp) VALUES (?, ?, ?, ?)
sqlCopy code
SELECT * FROM orientation_data ORDER BY timestamp DESC LIMIT 50
- Description: Retrieves the most recent 50 orientation data entries from the database.
- Ordering: The data is ordered by timestamp in descending order, ensuring the latest entries are fetched first.
sqlCopy code
SELECT * FROM orientation_data ORDER BY timestamp
- Description: Retrieves all orientation data entries from the database.
- Ordering: The data is ordered by timestamp in ascending order.
The activity utilizes the vico
library for graphing. Specifically, it leverages the CartesianChartHost
and related components provided by the vico
library to visualize data in a Cartesian coordinate system.
- The activity maintains two lists of
OrientationData
:orientationDataList
andorientationDataListfull
, which are populated asynchronously from the database. - The
fetchDataFromDatabase()
andfetchDataFromDatabasefull()
methods retrieve orientation data from the Room database using coroutines.
- The activity handles exporting orientation data to a CSV file on external storage. It requests and checks for write storage permission before performing file operations.
- The
exportDataToCSV()
function constructs a CSV file containing orientation data and saves it to the Downloads directory.
- The
GraphScreen
composable function displays multiple graphs for roll, pitch, and yaw angles. Each graph is rendered using theGraph
composable function. - The
Graph
composable function accepts title, xData, and yData parameters to render a specific graph. It utilizes theCartesianChartHost
to visualize the data series.
- The activity requests write storage permission (
WRITE_EXTERNAL_STORAGE
) from the user. If permission is granted, the activity proceeds with exporting data to CSV. Otherwise, it displays a message indicating permission denial.
GraphScreen
: Renders the layout for the graph activity, including multiple graphs and an export button.Graph
: Renders an individual graph with a specified title, xData, and yData. It utilizes theCartesianChartHost
to visualize the data series in a Cartesian coordinate system.
This Python script demonstrates how to predict orientation data (roll, pitch, yaw) using linear regression. It reads orientation data from a CSV file, trains separate linear regression models for each orientation angle, and generates predictions for the next values based on the previous window of data.
- Python 3.x
- pandas
- numpy
- scikit-learn
- matplotlib
- Place your CSV files containing orientation data in the
orientation_database
folder. - Adjust the
window_size
variable to specify the number of previous data points to consider for prediction. - Run the script.
- Import Libraries: Import required libraries including pandas, numpy, scikit-learn, matplotlib, and os.
- Read Data: Read orientation data from the first CSV file found in the
orientation_database
folder into a pandas DataFrame. - Data Preprocessing: Set an index for the DataFrame and define the window size for prediction.
- Model Training: Train separate linear regression models for roll, pitch, and yaw angles using scikit-learn's
LinearRegression
class. - Generate Predictions: Generate predictions for the next values of roll, pitch, and yaw angles using the trained models and the previous window of data.
- Plot Predictions: Plot the actual and predicted values of roll, pitch, and yaw angles using matplotlib.
- Ensure that your CSV files are formatted correctly with semicolon (
;
) as the delimiter. - The script assumes that the CSV files contain columns named
roll
,pitch
, andyaw
representing orientation angles. - Adjusting the
window_size
parameter can affect the accuracy of predictions. Experiment with different values to achieve the desired results.