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.
Fork of TVDctrller2017_brdRev1_ver6 by
Steering.cpp
- Committer:
- sift
- Date:
- 2016-07-24
- Revision:
- 2:9d69f27a3d3b
- Parent:
- 1:4d86ec2fe4b1
- Child:
- 13:6dc51981f391
File content as of revision 2:9d69f27a3d3b:
#include "mbed.h"
#include "Steering.h"
#define myAbs(x) ((x>0)?(x):(-(x)))
#define M_PI 3.1415f
#define ratioLPF 0.4559f //cut off:10Hz(at 10ms sampling)
extern SPI spi;
extern DigitalOut cs;
extern DigitalOut LED[];
Ticker ticker;
int gSteerAngle = 0;
void initIoExp(void)
{
//SPIバスに複数のデバイスをぶら下げる場合,modeをそろえる必要がある
spi.format(8, 0);
spi.frequency(1000000);
cs = 1;
wait(0.1); //Omajinai
cs = 0;
spi.write(IOEXP_WRITEADD);
spi.write(IOEXP_IODIR); //IO方向レジスタ
spi.write(0xFF); //All input
cs = 1;
wait_us(10); //Omajinai
cs = 0;
spi.write(IOEXP_WRITEADD);
spi.write(IOEXP_IPOL); //入力極性
spi.write(0xFF); //All inverted
cs = 1;
wait_us(10);
cs = 0;
spi.write(IOEXP_WRITEADD);
spi.write(IOEXP_GPPU); //PULL-UP
spi.write(0xFF); //ALL PULL-UP
cs = 1;
wait_us(10);
}
int readIoExp(void)
{
int gpio = 0;
cs = 0;
spi.write(IOEXP_READADD);
spi.write(IOEXP_GPIO);
gpio = spi.write(0x00);
cs = 1;
//wait_us(10);
return gpio;
}
volatile bool steerIntFlag = false;
void loadSteerAngleISR(void)
{
steerIntFlag = true;
}
void loadSteerAngle(void)
{
static int preAngle=0;
if(steerIntFlag == true) {
steerIntFlag = false;
int readData = GTB[readIoExp()];
if(readData == 127) {
LED[1] = LED[2] = 1;
} else if(readData < 127) {
LED[1] = 1;
LED[2] = 0;
} else {
LED[1] = 0;
LED[2] = 1;
}
gSteerAngle = (int)(readData*ratioLPF + preAngle*(1.0f-ratioLPF));
preAngle = gSteerAngle;
}
}
float getSteerAngle(void)
{
//gSteerAngle:0~255
//rad:0~2PI
//return:-PI~PI
float rad = 2.0f*M_PI*(gSteerAngle/255.0f);
if(rad > 2*M_PI)
rad = 2*M_PI;
else if(rad < 0)
rad = 0;
return myAbs(rad - M_PI); //steer left->[+]
}
int getSteerDirection(void)
{
return (gSteerAngle > 127) ? 1 : 0; //steer left->1
}
void initSteering(void)
{
initIoExp();
ticker.attach(loadSteerAngleISR, 0.01);
}
