2018.07.26
Dependencies: QEI mbed-rtos mbed
Diff: 4_main.cpp
- Revision:
- 0:b1136bf48c38
- Child:
- 1:86705c6e0ab7
diff -r 000000000000 -r b1136bf48c38 4_main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/4_main.cpp Tue Feb 16 16:35:29 2016 +0000 @@ -0,0 +1,242 @@ +/********************************************************** + * Project: B2 (1F-1) + * Title: CrExp B2 Motor Ctrl Main + * Target: LPCXpresso824-Max + * Author: zinsor + * Date(Latest update) 2015.12.21(Mon) + * -------------------------------------------------------- + * Article + * + * -------------------------------------------------------- + * + * LPCXpresso 824-MAX + * +---------USB---------+ + * | | + * | | + * | | + * | | + * | | SCL P0_10 D15 -> IIC SCL + * | ## ### ## | SDA P0_11 D14 -> IIC SDA + * | # # # # # # | AVDD + * N/A | # # # # # | GND + * +3V3 | ## # # # | SCK P0_24 D13 + * NRST | # # # # # | MISO P0_25 D12 + * +3V3 | # # # # ###### | MOSI P0_26 D11 -> Resolver B + * +5V | ## ##### # | SSEL P0_15 D10 -> Resolver A + * GND | | P0_27 D9 + * GND | | P0_13 D8 + * N/A | | P0_17 D7 + * | | P0_16 D6 + * A0 P0_6 | | PWM P0_28 D5 + * A1 P0_14 | | PWM P0_18 D4 + * A2 P0_23 | | PWM P0_12 D3 + * A3 P0_22 | | PWM P0_19 D2 + * A4 P0_21 | | TX P0_4 D1 + * A5 P0_20 | | RX P0_0 D0 + * +---------------------+ + * + ***************************************/ + +#include "mbed.h" +#include "rtos.h" +#include "QEI.h" +#include "common.h" +#include <math.h> + +// Hardware setting + +Serial pc(USBTX, USBRX); // tx, rx + +QEI wheel( P0_15, P0_26, NC, ROTATE_PER_RESOLUTION, QEI::X2_ENCODING ); +/* + Dram r = 60mm + Cable fai = 3 + + (60+3)*3.14 = 197.82mm ==> 2^12*4 = 4096 * 4 = 16384 pulse + 1(mm) = 82.82(pulse) + 0.01208(mm) = 1(pulse) + +*/ +DigitalOut led1(LED1); // Red +DigitalOut led2(LED2); // Green +DigitalOut led3(LED3); // Blue + +I2CSlave slave(P0_11, P0_10); //I2C SDA SCL + +int i2c_saddress = 0; + +void led_demo(){ + int i; + for( i = 0; i < 20; i++ ) { + led1 = LED_ON; + led2 = LED_OFF; + led3 = LED_OFF; + wait_ms(20); + led1 = LED_OFF; + led2 = LED_OFF; + led3 = LED_OFF; + wait_ms(20); + led1 = LED_OFF; + led2 = LED_ON; + led3 = LED_OFF; + wait_ms(20); + led1 = LED_OFF; + led2 = LED_OFF; + led3 = LED_OFF; + wait_ms(20); + led1 = LED_OFF; + led2 = LED_OFF; + led3 = LED_ON; + wait_ms(20); + led1 = LED_OFF; + led2 = LED_OFF; + led3 = LED_OFF; + wait_ms(20); + } +// wait_ms(3000); +} + +// ======================================================================== +// Thread: Resolver Control Task +// ======================================================================== +void ResolverCtrl_task(void const *) { + + +} + +// ======================================================================== +// Thread: Main Controller Interface Task +// Get Command from Main controller and do the task. +// ======================================================================== +void MainCtrl_Interface_task(void const *) { + + char buf[10]; // command buffer + char msg[] = "C000000"; + char res_msg[] = "Slave!"; + char res_msg2[] = "Bravo!"; + + int flg_readCurrent = 0; + + int pulse; + int16_t distance; + + uint16_t dram_diameter; + uint16_t ccable_diameter; + uint8_t rresolution; + + int tmp; + + slave.address(i2c_saddress); + + DEBUG_PRINT_L1("*** Start Resolver thread ***\n"); + + /* + * I2C Access + */ + while(1) { + // DEBUG_PRINT_L1("."); + int i = slave.receive(); + slave.read(buf, NumberOfI2CCommand); + // DEBUG_PRINT_L1("?"); + // DEBUG_PRINT_L1("Slave Received\r\n"); + switch (i) { + case I2CSlave::NoData: + // DEBUG_PRINT_L1("the slave has not been addressed\r\n"); + // read_motor_current(2); + break; + case I2CSlave::ReadAddressed: + /* Master is reading winch current position here */ + // DEBUG_PRINT_L1("the master has requested a read from this slave\r\n"); + pulse = wheel.getPulses(); + + distance = (int16_t)( pulse * ( (double)( ((dram_diameter+ccable_diameter)/100) * PAI / (pow(2.0, (double)rresolution)*4 ) ) ) ); + // distance = (int16_t)( pulse * ( REAL_THREAD_DIAMETER * PAI / ROTATION_PULSE_PER_1ROUND )); + + DEBUG_PRINT_L1("Bd4> PULSE: %07d, ", pulse); + DEBUG_PRINT_L1("DISTANCE: %04d (mm)", distance); + res_msg2[0] = distance & 0xFF; + res_msg2[1] = (distance >> 8)&0xFF; + slave.write(res_msg2, 2); // Includes null char + tmp = (res_msg2[1] < 8)&0xFF00 | res_msg2[0]&0xFF; + DEBUG_PRINT_L1("\tSend data : %04d(%02x,%02x)\n", tmp, res_msg2[1], res_msg2[0]); + break; + case I2CSlave::WriteGeneral: + DEBUG_PRINT_L1("Bd4> the master is writing to all slave\r\n"); + slave.read(buf, NumberOfI2CCommand); + DEBUG_PRINT_L1("Bd4> Read G: %s\r\n", buf); + break; + case I2CSlave::WriteAddressed: + /* ******************************************* */ + /* Add setting data set operation in this area */ + /* ******************************************* */ + + + if( buf[1] == 'Z'){ + wheel.reset(); + } + else{ + dram_diameter = ( buf[2] << 8 ); + dram_diameter |= buf[3]; + ccable_diameter = ( buf[4] << 8 ); + ccable_diameter |= buf[5]; + rresolution = buf[6]; + + DEBUG_PRINT_L1("Bd4> ===========================================\n"); + DEBUG_PRINT_L1("Bd4> Dram Diameter : %d\t(mm)\n", dram_diameter/100); + DEBUG_PRINT_L1("Bd4> CCable Diameter : %d\t(mm)\n", ccable_diameter/100); + DEBUG_PRINT_L1("Bd4> Resolver Resolution : %d\t(bit)\n", rresolution); + DEBUG_PRINT_L1("Bd4> -------------------------------------------\n", rresolution); + DEBUG_PRINT_L1("Bd4> Real Diameter : %d\t(mm)\n", (dram_diameter+ccable_diameter)/100); + DEBUG_PRINT_L1("Bd4> Rotation Pulse / 1round : %d\t(pulse)\n", (int)(pow(2.0, (double)rresolution)*4)); + DEBUG_PRINT_L1("Bd4> Distance / 1pulse : %lf\t(mm)\n", (double)( ((dram_diameter+ccable_diameter)/100) * PAI / (pow(2.0, (double)rresolution)*4 ) ) ); + DEBUG_PRINT_L1("Bd4> ===========================================\n"); + + pulse = wheel.getPulses(); + DEBUG_PRINT_L1("Bd4> PULSE: %07d, ", pulse); + // DEBUG_PRINT_L1("Bd4> >DISTANCE: %04d (mm)\n", (int)( pulse * ( REAL_THREAD_DIAMETER * PAI / ROTATION_PULSE_PER_1ROUND ))); + DEBUG_PRINT_L1("DISTANCE: %04d (mm)\n", (int)( pulse * ( (double)( ((dram_diameter+ccable_diameter)/100) * PAI / (pow(2.0, (double)rresolution)*4 ) ) ) ) ); + DEBUG_PRINT_L1("Bd4> ---------------------------------------\n"); + } + break; + } + Thread::wait(1); + } +} +// ======================================================================== +// Main Function +// ======================================================================== +int main() { + int32_t counter = 0; + + pc.baud(115200); + + i2c_saddress = Target_IIC_ADDR; // defined at header + + DEBUG_PRINT_L0("> Board type: Resolver Counter controller(0x%02x)\r\n",i2c_saddress ); + + i2c_saddress <<= 1; + + led_demo(); + + DEBUG_PRINT_L0("\r\n"); + DEBUG_PRINT_L0("Bd4> +--------------------------------------\r\n"); + DEBUG_PRINT_L0("Bd4> | B2 CrExp Resolver Pulse Counter Main \r\n"); + DEBUG_PRINT_L0("Bd4> | Letest update: %s\r\n", LatestUpDate); + DEBUG_PRINT_L0("Bd4> | Program Revision: %s\r\n", ProgramRevision); + DEBUG_PRINT_L0("Bd4> | Author: %s\r\n", Author); + DEBUG_PRINT_L0("Bd4> | Copyright(C) 2015 %s Allright Reserved\r\n", Company); + DEBUG_PRINT_L0("Bd4> +--------------------------------------\r\n"); + + Thread resolverControlTask1(MainCtrl_Interface_task, NULL, osPriorityNormal, 128 * 4); + wheel.reset(); + + while(1){ + + counter++; + if( counter >= 50 ){ + led3 = !led3; // Blue + counter = 0; + } + Thread::wait(5); + } +}