Software
Here is the overview of MoverDemo And MoverDemoLib projects.
MoverDemo system contains one application - MoverDemoApp.
Main application of the project. Inside the application you will find following components:
- CDP - core components of CDP Framework. No need to modify them.
- GPIOServer - controls Raspberry GPIO pins. Here must set into which GPIO pins the motors and power button are connected to.
- ADS1115IOServer - communicate with ADS1115 analog-to-digital converter over I2C bus. It is used to read sensor values of the robot.
- Controller - custom component created in MoverDemoLib. Contains the main logic of the robot and many configurable properties like BaseSpeed, SensorFrontWallThreshold etc.
MoverDemoApp also has some signals that are processed with CDP operators:
- SensorFront, SensorLeft, SensorRight - sensor values routed from ADS1115IOServer and scaled with operators to show proximity value between 0 and 100. The higher the value, the closer the wall is.
- MaxSpeed - maximum speed of the robot. By default 2 x BaseSpeed.
GPIOServer is a standard CDP component for controlling GPIO pins. In this project GPIO pins of our Raspberry are used to control motor speeds (using PWM - pulse width modulation) and to read PowerButton value that toggles motors on and off.
ADS1115IOServer is a CDP component that can communicate with ADS1115 analog-to-digital converter over I2C bus. In this project sensors are connected to pins A0, A1 and A2.
Before using this IOServer, it is important to configure Adapter, DataRate and Gain properties.
Controller is a custom component. MoverDemoLib contains the C++ source code for it.
Controller has two output signals: MotorLeft and MotorRight. They contain one operator to limit minimum and maximum speed. These values are routed by GPIOServer component which applies PWM operator on them and outputs result to Raspberry PI GPIO ports.
In addition, Controller component has several input signals:
- SensorFront, SensorLeft, SensorRight - these signals just route sensor values to make them accessible from C++ code.
- PIDLeft, PIDRight - these signals contain PID regulator operator configured to follow left or right wall respectively. For ease of use, all PID regulator parameters are also defined in Controller level in order to set same values to both PIDLeft and PIDRight regulators:
- Kr - overall gain of PID regulator. This signal depends on BaseSpeed and scaling operator to amplify PID regulator effect on higher motor speeds.
- Kp, Ki, Kd, Kf, Sat, MinI, MaxI, TDecay - parameters that should be manually set (can be done even at runtime). See Description column of each parameter for more information.
Controller component also has several properties to configure robot behavior. Properties are also accessible from Controller C++ code.
- BaseSpeed - Base motor speed when following a wall.
- DistanceFromWall - Preferred side sensor value (proximity) to when following a wall. Increasing this will make robot drive closer to wall.
- DriveStraightSpeed - Motor speed when no sensor detects a wall and robot just drives straight.
- SensorFrontWallThreshold, SensorLeftWallThreshold, SensorRightWallThreshold - Minimum sensor value before wall is detected and robot state changed. Helps to filter out noise.
- TurnSpeed - Motor speed during turn when front sensor detects a wall.
Contains the Controller component C++ code. Switch to Code Mode to explore the implementation.
The Controller component contains:
- 4 states. State behavior is defined by ProcessX function which is periodically called if component is in respective state.
- FollowLeftWall - robot uses left sensor and PID regulator to keep stable distance from left wall.
- FollowRightWall - robot uses right sensor and PID regulator to keep stable distance from right wall.
- Drive - robot drives straight.
- Turn - robot stops one wheel to make a sharp turn.
- State transition functions. Transition functions are periodically called and if any of them returns
true
, state is changed. - CDPSignals - inputs and outputs of the component that were configured in Configure Mode.
- CDPProperties - parameters to configure component behavior. Can be even changed at runtime from Configure Mode.
Following function controls robot behavior during FollowLeftWall state. It is called periodically by CDP framework and the function reduces or increases left/right motor speed to keep robot distance to wall the same. PIDLeft value is set from Configure Mode using PID regulator operator.
void Controller::ProcessFollowLeftWall()
{
MotorLeft = BaseSpeed - PIDLeft;
MotorRight = BaseSpeed + PIDLeft;
}
Following function is called periodically by CDP framework. If it returns true
then robot state is set to Turn, which means CDP framework starts calling ProcessTurn function that instructs the robot to make a sharp turn.
bool Controller::TransitionToTurn()
{
return CanSeeFrontWall();
}
CanSeeFrontWall is a helper function that compares front sensor value to SensorRightWallThreshold property.
bool Controller::CanSeeRightWall()
{
return SensorRight > SensorRightWallThreshold;
}
To build the library for your Raspberry Pi, go to Configure Mode and in project tree click on MoverDemoLib node. Then:
A. Check Raspbian toolchain.
B. In project tree right click on MoverDemoLib node and select Build.
To run the system on your robot, go to Configure Mode and in project tree click on MoverDemo node. Then:
A. Select the network where your Raspberry Pi is connected.
B. In the Devices table enter credentials for your Raspberry Pi.
C. Set your Raspberry Pi as the Device to deploy to.
D. Select correct toolchain to compile the application.
E. In project tree right click on MoverDemo node and select Run.
Next push the PowerButton on the robot (configured in GPIOServer component) which will toggle motors on. Now the robot should start driving: https://photos.app.goo.gl/Gn4Ap4aQLN0razH03
To connect to a running system, right click on MoverDemo node in project tree and select Connect.
Now you can see live values from sensors and modify properties to tune your robot behavior.
Tip: any value can be added to Analyze Mode plot. For more information see Analyze Mode Tutorial.
This demo project also includes a simple Web UI optimized for mobile devices.
Usage:
- Run application.
- Look at Application Output and find something like "StudioAPIServer: listening on 192.168.0.77:7689".
- Use you web browser and navigate to "192.168.0.77:7689/www/index.html" (or some other IP and port).
To edit this web UI, open Code Mode and in project tree navigate to MoverDemo.MoverDemoApp.Other files.Application.www.index.html
.
The example is based on angular-cdp but it is also possible to directly subscribe to value changes using Javascript API and use any framework for your web UI. See StudioAPI Javascript Client Tutorial for that.