Use IQS62X sensor and move motor by detected angle
Dependencies: DRV8830 IQS62x IQSDisplayTerminal UIT_ACM1602NI mbed
Fork of Nucleo_ACM1602_I2C_DC by
main.cpp
- Committer:
- 8mona
- Date:
- 2017-09-21
- Revision:
- 6:e3afb1390167
- Parent:
- 5:2b9614aa1171
- Child:
- 7:acb1074eaba6
File content as of revision 6:e3afb1390167:
//------------------------------------------------------------
// Demo program for LCD ACM1602NI using I2C interface
// Pullup resistors for SDA and SCL: 4.7 kΩ
// 2016/04/01, Copyright (c) 2016 MIKAMI, Naoki
//------------------------------------------------------------
#include "ACM1602NI.hpp"
#include "DRV8830.h"
#include "IQS62x.h"
#include "IQSdisplayTerminal.h"
//IQS62xDisplay terminal; // class to display IQS62x registers on a terminal
IQS62xIO iqs62x; // class for basic IQS62x block read and write
//Cycle
#define UP_DURATION 14 //On time [*100ms]
#define WAIT_DELAY 10 //Delay time [*100ms]
#define DOWN_DURATION 8 //Down time [*50ms]
#define SWITCH_PERIOD 50 //Cycle time[*50ms]
#define TOTAL_TIMES 30000 //total times n
#define ACCEL_SIZE 6
#define DECEL_SIZE 6
const float spd_table[] = {1.0,0.9,0.8};
//const float vol_accel[ACCEL_SIZE] = {0,0.3,0.6,0.8,0.95,1};
const float vol_decel[DECEL_SIZE] = {1,0.75,0.4,0.15,0.05,0};
const float vol_accel[ACCEL_SIZE] = {0,0.5,0.8,1,1,1};
//const float vol_decel[DECEL_SIZE] = {1,0.75,0.15,-1,-0.2,0};
const float donw_ratio=0.85;
static int sp_index=0;
using namespace Mikami;
void ShowLCD(char * buffer, int startbyte, int endbyte); // for wheel output
Acm1602Ni lcd_; // Default, OK
//Acm1602Ni lcd_(D14, D15); // OK
//Acm1602Ni lcd_(D14, D15, 200000); // OK
//Acm1602Ni lcd_(D14, D15, 200000, true, true); // OK
//Acm1602Ni lcd_(PB_3, PB_10); // OK
//Acm1602Ni lcd_(PC_9, PA_8); // OK
//Acm1602Ni lcd_(PB_4, PA_8); // OK
Ticker timer_;
I2C i2c(D14, D15);
DRV8830 motor(i2c, DRV8830ADDR_NN);
//DigitalOut Relay1(D2);
InterruptIn button1(USER_BUTTON);
static float motor_speed;
// Display elapsed time in minutes and seconds
void ShowLCD(char * buffer, int startbyte, int endbyte)
{
for (int i=startbyte; i<=endbyte; i++) {
lcd_.WriteValue("%02x ", buffer[i]); // print out in black & white
}
}
void TimerIsr()
{
//For LED Time-Sec display
static int k = 0;
static char ctext[4]="---";
div_t d_Cycle = div (k, SWITCH_PERIOD);
//for Current time
div_t d_sec = div(k,600*2); //60s * 10n
int t_min = d_sec.quot;
div_t d_min = div(t_min,60); //1min=60s
int t_hr = d_min.quot;
//for Current time
div_t df_sec = div(TOTAL_TIMES*SWITCH_PERIOD,600); //60s * 10n
int tf_min = df_sec.quot;
div_t df_min = div(tf_min,60); //1min=60s
int tf_hr = df_min.quot;
//Motor activation
//Up Movement
if(WAIT_DELAY <= d_Cycle.rem && d_Cycle.rem < (WAIT_DELAY+UP_DURATION) )
{
int accel_index = d_Cycle.rem - WAIT_DELAY;
if (accel_index < ACCEL_SIZE)
{
motor_speed=vol_accel[accel_index] * spd_table[sp_index];
}
else
{
motor_speed=spd_table[sp_index];
}
strcpy(ctext," CW");
//ctext="CW";
}
//UP..stop
else if ( (WAIT_DELAY+UP_DURATION)<= d_Cycle.rem && d_Cycle.rem <(2*WAIT_DELAY+UP_DURATION) )
{
//wait_ms(20);
int accel_index = d_Cycle.rem - (WAIT_DELAY+UP_DURATION);
if (accel_index< DECEL_SIZE)
{
motor_speed=vol_decel[accel_index] * spd_table[sp_index];
}
else
{
motor_speed=0;
}
strcpy(ctext,"OFF");
}
//down..Start
else if ( (2*WAIT_DELAY+UP_DURATION) <= d_Cycle.rem && d_Cycle.rem <(2*WAIT_DELAY+UP_DURATION+DOWN_DURATION) )
{
int accel_index = d_Cycle.rem - (2*WAIT_DELAY+UP_DURATION);
if (accel_index < ACCEL_SIZE)
{
motor_speed= - vol_accel[accel_index] * spd_table[sp_index]*donw_ratio;
}
else
{
motor_speed= -spd_table[sp_index]*donw_ratio;
}
strcpy(ctext," CW");
//ctext="CW";
}
//down..stop
else if ( (2*WAIT_DELAY+UP_DURATION+DOWN_DURATION) <= d_Cycle.rem )
{
//wait_ms(20);
int accel_index = d_Cycle.rem - ((2*WAIT_DELAY+UP_DURATION+DOWN_DURATION));
if (accel_index< DECEL_SIZE)
{
motor_speed= -vol_decel[accel_index] * spd_table[sp_index]*donw_ratio;
}
else
{
motor_speed=0;
}
strcpy(ctext,"OFF");
}
if(d_Cycle.quot==TOTAL_TIMES)
{
timer_.detach();
}
/*
char str[20];
sprintf(str, "%d'%2d\"", msec.quot, msec.rem);
lcd_.WriteStringXY(str, 0, 1);
*/
//1 Row
//lcd_.WriteStringXY("#",0,0);
lcd_.WriteValueXY("%5d/", d_Cycle.quot,0,0);
lcd_.WriteValue("%0dM",TOTAL_TIMES);
lcd_.WriteValue("%1.2f", motor_speed);
//lcd_.WriteValue("V%0d",motor_speed);
//2 Row
lcd_.WriteValueXY("%03dh", t_hr, 0, 1);
lcd_.WriteValue("%02dm", d_min.rem);
lcd_.WriteValue("%03ds/", d_sec.rem);
lcd_.WriteValue("%03dh", tf_hr);
lcd_.WriteValue("%02dm", df_min.rem);
k++;
}
void flip() {
static bool b = false;
if(b==false)
{
timer_.attach(&TimerIsr, 0.05);
}
else
{
timer_.detach();
//Relay1=0;
sp_index++;
if (sp_index == 3)
{
sp_index = 0;
}
}
b=!b;
}
int main()
{
//motor.speed(0);
// Check error and reset
//LCD_cont=0;
//if (lcd_.IsConnected()) printf("\r\nConnected");
//else printf("\r\nDisconnected");
TimerIsr();
//timer_.attach(&TimerIsr, 0.1);
button1.fall(&flip);
iqs62x.configure(); // configure the ICD
bool status = motor.status();
if (status & DRV8830_F_FAULT){
motor.reset();
}
while (true) {
motor.speed(motor_speed);
//iqs62x.readIqsRegisters(0,NUMBER_OF_REGISTERS); // read all the registers
//ShowLCD(iqs62x.registers,0x80,0x8f);
wait(1);
}
}
