Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: Motor_NIT_Nagaoka_College PID Servo QEI SoftPWM mbed HBridge
main.cpp
- Committer:
- WAT34
- Date:
- 2015-09-14
- Revision:
- 8:9d8999740604
- Parent:
- 7:920cbfb28112
- Child:
- 9:98ef1eee7ace
- Child:
- 10:fefff533c442
File content as of revision 8:9d8999740604:
#define pi 3.141593
#include "mbed.h"
#include "Motor.h"
#include "PID.h"
#include "QEI.h"
#include "Servo.h"
#define RATE 0.05
BusOut air(PC_10,PC_11);
DigitalOut out(PC_12);
Serial conn(PA_15,PB_7);
Serial pc(USBTX,USBRX);
PID Tp(50,40000,0,0.001);
BusOut led(LED1,LED2,LED3,LED4);
Motor ot(PB_13,PB_3,PA_10);
QEI sensort(PC_3,PC_0,NC,624);
Servo L(PC_9);
Servo R(PC_8);
Timeout ai;
char read;
int Rs = 0,Ls = 0;
int i = 0;
void zero(){
air = 0;
i = 0;
out = 1;
}
void rev(){
air = 2;
ai.attach(&zero,1.0);
out = 0;
}
int main()
{
ot.setfrequency(60000);
double tilt = 0,lo = 0,ro = 0;
int8_t ttilt = 0,tmpread = 0,tmpttilt = 0;
char tro = 0,tlo = 0;
Tp.setInputLimits(-45,45);
Tp.setOutputLimits(-0.9,0.9);
Tp.setMode(1);
Tp.setBias(0.0);
while(1) {
if(conn.getc() == 255) {
tmpread = conn.getc();
tmpttilt = conn.getc();
tro = conn.getc();
tlo = conn.getc();
if(tmpread^tmpttilt^tro^tlo == conn.getc()){
ttilt = tmpttilt;
read = tmpread;
ro = (tro-127)/127.0*0.9;
lo = (tlo-127)/127.0*0.9;
}
}
if((read>>2)%2 && i == 0){
air = 1;
ai.attach(&rev,1.0);
i = 1;
}
Tp.setSetPoint(ttilt);
tilt = double(sensort.getPulses());
tilt = tilt*61/5128.0;
Tp.setProcessValue(tilt);
if (abs(lo) < 0.1){
lo = 0;
}
if (abs(ro) < 0.1){
ro = 0;
}
L.write((lo+1.0)/2.0);
R.write((ro+1.0)/2.0);
ot.speed(Tp.compute());
//pc.printf("%f %d\n\r",Tp.compute(),read);
//pc.printf("%d-%d\r\n",tlo,tro);
wait_ms(1);
}
}