Haha~~~Immediately after the Eleventh holiday, the foolish author will go out in seven hours?
Suddenly it came to mind that there has been no wording for a long time. However, if I dare to take my computer to travel roughly, I will be killed.
It completely eliminated the possibility that I would love to study and work while on vacation.
I decided to write a technical article before I left. After all, Mr. Lu Xun said,
Programmers who don't output technology blogs aren't good programmers.
Just because Android Things controls the SG90 steering gear, it's not a core technology, it's business-independent, and there's little online information. It's good for writing a hhh.
1. Hardware preparation
Raspberry Pie 3 development sheet 1
Rudder 1
2. Circuit connection
Red wire - 5V power supply (02)
Brown Line - GND (06)
Yellow line - Data control line, on PWM port (12 corresponding to PWM0)
3.SG90 Background Knowledge
1) First of all, it is important to know that SG90 uses different pulse widths to identify different rotation angles, so it is controlled by PWM, not Gpio.The Gpio control of Android Things is a state signal, suitable for 01 switch control.
2) The SG90 pulse period is 20 milliseconds, or Hz=50.
3) The rotation angle supported by SG90 is 180 degrees.The pulse width of 0.5ms-2.5ms corresponds to an angle of -90 to + 90 degrees, and the corresponding duty cycle is 2.5% - 12.5%.So 7.5% corresponds to the steering at the neutral point (0 degrees).
4. Code
First of all, how should we get the data serial port connected to SG90?
public SteeringEngine(String pwmName){
PeripheralManagerService manager =new PeripheralManagerService();
try{
mPwm= manager.openPwm(pwmName);
mPwm.setPwmFrequencyHz(50);
Log.w("----->","access PWM");
}catch(IOException e) {
Log.w("----->","Unable to access PWM",e);
}
}
What interfaces do you need to control SG90?
1) Let the rudder rotate to the specified angle:
Since the mechanical rotation speed of the SG90 is not so fast in a pulse cycle after the angle is specified, the maximum rotation angle (180 degrees) tested will take about 17 pulse cycles, and 18 are set to ensure that it must be turned.
public voidsetDegree(doubledegree){
if(degree<0|| degree>180)
return;
try{
mPwm.setEnabled(true);
int i =18;
while(i>0){
i--;
try{
mPwm.setPwmDutyCycle(2.5+10* degree /180);
Thread.sleep(20);
}catch(IOException e) {
e.printStackTrace();
}catch(InterruptedException e) {
e.printStackTrace();
}
}
mPwm.setEnabled(false);
}catch(IOException e) {
e.printStackTrace();
}
}
Sometimes we want to control the speed at which the helm is turning, so don't just finish brushing.
/**
*Controls the steering to the corresponding angle, degree between [0,180].
* The gap is used to control when the rotation is completed, that is, to control the speed of the rotation.
* The unit of gap is milliseconds, ms.
*A single rotation requires several pulse cycles. After each pulse signal is rotated, the gap will produce a flat signal in the time interval.
*@paramdegree
*@paramgap
*/
public void setDegree(doubledegree,intgap){
if(degree<0|| degree>180)
return;
try{
mPwm.setEnabled(true);
inti =18;
while(i>0){
i--;
try{
mPwm.setPwmDutyCycle(2.5+10* degree /180);
Thread.sleep(20);
mPwm.setPwmDutyCycle(0);
Thread.sleep(gap);
}catch(IOException e) {
e.printStackTrace();
}catch(InterruptedException e) {
e.printStackTrace();
}
}
mPwm.setEnabled(false);
}catch(IOException e) {
e.printStackTrace();
}
}
2) We may also need an interface where the rudder swings back and forth between two angles:
public void startSwing(final doublebegin, final doubleend, final intgapTime){
if(begin<0|| begin>180|| end<0|| end>180||gapTime<0)
return;
if(STATE_STOP==mState) {
mState=STATE_START;
swingThread=newThread() {
@Override
public voidrun() {
swing(begin,end,gapTime);
}
};
if(null!=swingThread) {
swingThread.start();
}
}
}
private void swing(doublebegin, doubleend,intgapTime){
if(STATE_START==mState)
setDegree(BEGIN_PLACE);
while(STATE_START==mState) {
setDegree(begin,gapTime);
setDegree(end,gapTime);
}
}
Stop oscillating:
public void stopSwing(){
if(STATE_START==mState) {
mState=STATE_STOP;
Log.d("---->","stop start");
if(null!=swingThread) {
try{
Log.d("---->","wait recognition thread exit");
swingThread.join();
}catch(InterruptedException e) {
e.printStackTrace();
}finally{
swingThread=null;
}
}
Log.d("---->","stop end");
}
}
3) Call example:
Using an example, let the rudder swing between 0 and 180 degrees for 1 minute, gap time for 100ms
String PWM_NAME = "PWM0";
SteeringEngine se = new SteeringEngine(PWM_NAME);
se.startSwing(0,180,100);
try {
//The current thread can do something else here, and the swing is performed on another thread
Thread.sleep(60*1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
se.stopSwing();
se.close();
5.Demo
I put Github on, Source code~~
(Would you like to bring a star when your baby comes to fetch it, pen core ^^)
What a rushed article!
Go out and surf~~