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.
Revision 0:f8373bf202a6, committed 2018-07-26
- Comitter:
- sayzyas
- Date:
- Thu Jul 26 00:19:47 2018 +0000
- Child:
- 1:85e8e2c8f283
- Commit message:
- 2018.07.26
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/P1_1Y_main.cpp Thu Jul 26 00:19:47 2018 +0000
@@ -0,0 +1,435 @@
+/**********************************************************
+ * Project: B2 (1F-1)
+ * Title: CrExp B2 Motor Ctrl Main
+ * Target: LPCXpresso824-Max
+ * Author: zinsor
+ * Date(Latest update) 2015.12.21(Mon)
+ * --------------------------------------------------------
+ * Article
+ * Notification: Moving and Fix Winch rotation direction
+ * is opposit.
+ *
+ * --------------------------------------------------------
+ *
+ * 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>
+#include "mcchk.h"
+
+// Hardware setting
+
+Serial pc(USBTX, USBRX); // tx, rx
+
+#ifdef FFWinchPhaseSetting // For Fix Fallong Winch
+QEI wheel( P0_26, P0_15, NC, ROTATE_PER_RESOLUTION, QEI::X2_ENCODING );
+#else //For Falling Position Moving Winch
+QEI wheel( P0_15, P0_26, NC, ROTATE_PER_RESOLUTION, QEI::X2_ENCODING );
+#endif // FFWinchPhaseSetting
+
+/*
+ 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
+
+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);
+}
+
+// ========================================================================
+// Main Function
+// ========================================================================
+int main() {
+
+ char buf[12]; // command buffer
+ char msg[9];
+
+ int pulse;
+ double distance_d = 0.0f;
+ int16_t distance;
+ int16_t position_offset = 0;;
+
+ uint16_t dram_diameter;
+ uint16_t dis_correct_value;
+ uint8_t rresolution;
+
+ int tmp;
+ mcchk mcc;
+
+ pc.baud(115200);
+
+ slave.address(I2C_ADDRESS_RESOLVER);
+
+ 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");
+
+ wheel.reset();
+
+
+
+ while(1){
+
+ led2 = LED_OFF;
+ led3 = LED_OFF;
+
+ int i = slave.receive();
+ slave.read(buf, NumberOfI2CCommand);
+
+ switch (i) {
+ case I2CSlave::NoData:
+ // DEBUG_PRINT_L1("the slave has not been addressed\r\n");
+ // read_motor_current(2);
+ break;
+ //
+ // Slave(this) <== Master
+ //
+ //-----------------------------------------------------------------
+ // Master is reading data from client
+ //-----------------------------------------------------------------
+ case I2CSlave::ReadAddressed:
+ /* Master is reading winch current position here */
+ pulse = wheel.getPulses();
+
+ /* Distance calculation */
+ distance_d = ( pulse * ( (double)( ((double)dram_diameter/(double)100) * (double)PAI / (pow(2.0, (double)rresolution)*4 ) ) ) );
+ distance = (int16_t)(distance_d * (double)((double)dis_correct_value/(double)10000));
+ distance += position_offset;
+
+ DEBUG_PRINT_L2("Bd4> PULSE: %07d, ", pulse);
+ DEBUG_PRINT_L2("DISTANCE(OFFSET): %04d(%4d) mm", distance, position_offset);
+ msg[0] = 0x12; // Dummy data for data check
+ msg[1] = distance & 0xFF;
+ msg[2] = (distance >> 8)&0xFF;
+ // msg[3] = ... : was set by get the command "G".
+ msg[8] = 0x34; // Dummy data
+ slave.write(msg, 9); // Includes null char
+ tmp = (msg[2] < 8)&0xFF00 | msg[1]&0xFF;
+ DEBUG_PRINT_L2("\tSend data : %04d(%02x,%02x)\r\n", tmp, msg[1], msg[0]);
+ led3 = LED_ON;
+ 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;
+
+ //
+ // Master ==> Slave(this)
+ //
+ //-----------------------------------------------------------------
+ // Master is writing data to client
+ //-----------------------------------------------------------------
+ case I2CSlave::WriteAddressed:
+ // *******************************************
+ // Command list
+ // ------------
+ // Z: Wheel reset
+ // S: Preset current winch position
+ // R: Set resolver calculation base data
+ // G: Get specified motor current and stor
+ // T: Set all motor current thresold
+ // *******************************************
+ if( buf[I2C_CP_COMMAND] == 'G' ){ // Get motor current
+ DEBUG_PRINT_L1("Bd4> i2c Get motor current\r\n");
+ if ( mcc.rdnchk_motorCurrent( buf[I2C_CP_MOTORNO], buf[I2C_CP_M_DIR], 5 ) == true )
+ {
+ msg[3] = 1;
+ //DEBUG_PRINT_L1("Bd4> Motor LC: pct=%03d, now=%f, center=%f, th=%d ###LOCK###\r\n", mcc.mc_abs_pct, mcc._cnt_now, mcc._cnt_center, mcc._cnt_th);
+
+ DEBUG_PRINT_L1("Bd4> Motor LC: pct=%03d, now=%f, center=%f, th=%d, %d, %d, %d, %d ###LOCK###\r\n", mcc.mc_abs_pct, mcc._cnt_now, mcc._cnt_center, mcc._cnt_th,
+ mcc.cnt_mclock_LBTFM_f,
+ mcc.cnt_mclock_LBTFM_r,
+ mcc.cnt_mclock_RFTFM_f,
+ mcc.cnt_mclock_RFTFM_r
+
+ );
+
+
+
+ }
+ else
+ {
+ msg[3] = 0;
+ // DEBUG_PRINT_L1("Bd4> Motor LC: pct=%03d, now=%f, center=%f, th=%d\r\n", mcc.mc_abs_pct, mcc._cnt_now, mcc._cnt_center, mcc._cnt_th);
+
+ DEBUG_PRINT_L1("Bd4> Motor LC: pct=%03d, now=%f, center=%f, th=%d, %d, %d, %d, %d\r\n", mcc.mc_abs_pct, mcc._cnt_now, mcc._cnt_center, mcc._cnt_th,
+ mcc.cnt_mclock_LBTFM_f,
+ mcc.cnt_mclock_LBTFM_r,
+ mcc.cnt_mclock_RFTFM_f,
+ mcc.cnt_mclock_RFTFM_r
+
+ );
+
+ }
+ }
+
+ else if( buf[I2C_CP_COMMAND] == 'T' ){ // Set motor threshold
+ DEBUG_PRINT_L1("Bd4> i2c Set motor threshold\r\n");
+ // LB CRW
+ if( buf[I2C_CP_MOTORNO] == MOTOR_LBCRW )
+ {
+ if( buf[I2C_CP_M_DIR] == MOTOR_DIR_FWD )
+ {
+ mcc.mc_th_LBCRW_f = ( buf[I2C_CP_M_CNTTH_U] ) << 8 | buf[I2C_CP_M_CNTTH_L];
+ DEBUG_PRINT_L1("Bd4> motor th (LBCRW F) : %d\r\n", mcc.mc_th_LBCRW_f);
+ }
+ else
+ {
+ mcc.mc_th_LBCRW_r = ( buf[I2C_CP_M_CNTTH_U] ) << 8 | buf[I2C_CP_M_CNTTH_L];
+ DEBUG_PRINT_L1("Bd4> motor th (LBCRW R) : %d\r\n", mcc.mc_th_LBCRW_r);
+ }
+ }
+ // RF CRW
+ else if( buf[I2C_CP_MOTORNO] == MOTOR_RFCRW )
+ {
+ if( buf[I2C_CP_M_DIR] == MOTOR_DIR_FWD )
+ {
+ mcc.mc_th_RFCRW_f = ( buf[I2C_CP_M_CNTTH_U] ) << 8 | buf[I2C_CP_M_CNTTH_L];
+ DEBUG_PRINT_L1("Bd4> motor th (RFCRW F) : %d\r\n", mcc.mc_th_RFCRW_f);
+ }
+ else
+ {
+ mcc.mc_th_RFCRW_r = ( buf[I2C_CP_M_CNTTH_U] ) << 8 | buf[I2C_CP_M_CNTTH_L];
+ DEBUG_PRINT_L1("Bd4> motor th (RFCRW R) : %d\r\n", mcc.mc_th_RFCRW_r);
+ }
+ }
+ // LB TFM
+ else if( buf[I2C_CP_MOTORNO] == MOTOR_LBTFM )
+ {
+ if( buf[I2C_CP_M_DIR] == MOTOR_DIR_FWD )
+ {
+ mcc.mc_th_LBTFM_f = ( buf[I2C_CP_M_CNTTH_U] ) << 8 | buf[I2C_CP_M_CNTTH_L];
+ DEBUG_PRINT_L1("Bd4> motor th (LBTFM F) : %d\r\n", mcc.mc_th_LBTFM_f);
+ }
+ else
+ {
+ mcc.mc_th_LBTFM_r = ( buf[I2C_CP_M_CNTTH_U] ) << 8 | buf[I2C_CP_M_CNTTH_L];
+ DEBUG_PRINT_L1("Bd4> motor th (LBTFM R) : %d\r\n", mcc.mc_th_LBTFM_r);
+ }
+ }
+ // RF TFM
+ else if( buf[I2C_CP_MOTORNO] == MOTOR_RFTFM )
+ {
+ if( buf[I2C_CP_M_DIR] == MOTOR_DIR_FWD )
+ {
+ mcc.mc_th_RFTFM_f = ( buf[I2C_CP_M_CNTTH_U] ) << 8 | buf[I2C_CP_M_CNTTH_L];
+ DEBUG_PRINT_L1("Bd4> motor th (RFTFM F) : %d\r\n", mcc.mc_th_RFTFM_f);
+ }
+ else
+ {
+ mcc.mc_th_RFTFM_r = ( buf[I2C_CP_M_CNTTH_U] ) << 8 | buf[I2C_CP_M_CNTTH_L];
+ DEBUG_PRINT_L1("Bd4> motor th (RFTFM R) : %d\r\n", mcc.mc_th_RFTFM_r);
+ }
+ }
+ // WINCH
+ else if( buf[I2C_CP_MOTORNO] == MOTOR_WINCH )
+ {
+ if( buf[I2C_CP_M_DIR] == MOTOR_DIR_FWD )
+ {
+ mcc.mc_th_WINCH_f = ( buf[I2C_CP_M_CNTTH_U] ) << 8 | buf[I2C_CP_M_CNTTH_L];
+ DEBUG_PRINT_L1("Bd4> motor th (WINCH F) : %d\r\n", mcc.mc_th_WINCH_f);
+ }
+ else
+ {
+ mcc.mc_th_WINCH_r = ( buf[I2C_CP_M_CNTTH_U] ) << 8 | buf[I2C_CP_M_CNTTH_L];
+ DEBUG_PRINT_L1("Bd4> motor th (WINCH R) : %d\r\n", mcc.mc_th_WINCH_r);
+ }
+ }
+ // PAN
+ else if( buf[I2C_CP_MOTORNO] == MOTOR_CMPAN )
+ {
+ if( buf[I2C_CP_M_DIR] == MOTOR_DIR_FWD )
+ {
+ mcc.mc_th_CMPAN_f = ( buf[I2C_CP_M_CNTTH_U] ) << 8 | buf[I2C_CP_M_CNTTH_L];
+ DEBUG_PRINT_L1("Bd4> motor th (CPAN F) : %d\r\n", mcc.mc_th_CMPAN_f);
+ }
+ else
+ {
+ mcc.mc_th_CMPAN_r = ( buf[I2C_CP_M_CNTTH_U] ) << 8 | buf[I2C_CP_M_CNTTH_L];
+ DEBUG_PRINT_L1("Bd4> motor th (CPAN R) : %d\r\n", mcc.mc_th_CMPAN_r);
+ }
+ }
+ // TILT
+ else if( buf[I2C_CP_MOTORNO] == MOTOR_CTILT )
+ {
+ if( buf[I2C_CP_M_DIR] == MOTOR_DIR_FWD )
+ {
+ mcc.mc_th_CTILT_f = ( buf[I2C_CP_M_CNTTH_U] ) << 8 | buf[I2C_CP_M_CNTTH_L];
+ DEBUG_PRINT_L1("Bd4> motor th (CTILT F) : %d\r\n", mcc.mc_th_CTILT_f);
+ }
+ else
+ {
+ mcc.mc_th_CTILT_r = ( buf[I2C_CP_M_CNTTH_U] ) << 8 | buf[I2C_CP_M_CNTTH_L];
+ DEBUG_PRINT_L1("Bd4> motor th (CTILT R) : %d\r\n", mcc.mc_th_CTILT_r);
+ }
+ }
+ }
+ else if( buf[I2C_CP_COMMAND] == 0x4f ){ // Clear motor current over counters
+ DEBUG_PRINT_L1("Bd4> i2c Clear motor current over counters\r\n");
+ mcc.cnt_mclock_LBCRW_f = 0; // motor lock counter
+ mcc.cnt_mclock_LBCRW_r = 0; // motor lock counter
+ mcc.cnt_mclock_RFCRW_f = 0; // motor lock counter
+ mcc.cnt_mclock_RFCRW_r = 0; // motor lock counter
+ mcc.cnt_mclock_LBTFM_f = 0; // motor lock counter
+ mcc.cnt_mclock_LBTFM_r = 0; // motor lock counter
+ mcc.cnt_mclock_RFTFM_f = 0; // motor lock counter
+ mcc.cnt_mclock_RFTFM_r = 0; // motor lock counter
+ mcc.cnt_mclock_CMPAN_f = 0; // motor lock counter
+ mcc.cnt_mclock_CMPAN_r = 0; // motor lock counter
+ mcc.cnt_mclock_CTILT_f = 0; // motor lock counter
+ mcc.cnt_mclock_CTILT_r = 0; // motor lock counter
+ mcc.cnt_mclock_WINCH_f = 0; // motor lock counter
+ mcc.cnt_mclock_WINCH_r = 0; // motor lock counter
+ }
+ else if( buf[I2C_CP_COMMAND] == 0x43 ){ // 'C': Set motor current base
+ DEBUG_PRINT_L1("Bd4> i2c Set motor current base value\r\n");
+ mcc.set_init_mc( 300 );
+ }
+ else if( buf[I2C_CP_COMMAND] == 'Z'){ // Position reset
+ DEBUG_PRINT_L1("Bd4> i2c Winch Position reset\r\n");
+ wheel.reset();
+ position_offset = 0;
+ }
+ // New command 2016.11.17 for customer request
+ else if( buf[I2C_CP_COMMAND] == 'S'){ // Preset winch pisition <--- B2Demo isn't use this.
+ DEBUG_PRINT_L1("Bd4> i2c Winch Position preset\r\n");
+ wheel.reset();
+ position_offset = 0;
+ for( int j = 0; j < NumberOfI2CCommand; j++)
+ pc.printf("%02x ", buf[j]);
+ pc.printf( "\r\n" );
+ position_offset = ( buf[ I2C_CP_PRESET_CPOS_UPPER] << 8 );
+ position_offset |= buf[I2C_CP_PRESET_CPOS_LOWER];
+ pc.printf("POSITION OFFSET = %d\r\n", position_offset);
+ }
+ else if( buf[I2C_CP_COMMAND] == 'R'){ // Set resolver calculation base parameter.
+ DEBUG_PRINT_L1("Bd4> i2c Set resolver calc base data\r\n");
+ for( int j = 0; j < NumberOfI2CCommand; j++)
+ {
+ pc.printf("%02x ", buf[j]);
+ }
+ pc.printf( "\r\n" );
+
+ dram_diameter = ( buf[I2C_CP_WDRAM_DIA_UPPER] << 8 );
+ dram_diameter |= buf[I2C_CP_WDRAM_DIA_LOWER];
+ dis_correct_value = ( buf[I2C_CP_CCABLE_DIA_UPPER] << 8 );
+ dis_correct_value |= buf[I2C_CP_CCABLE_DIA_LOWER];
+ rresolution = buf[I2C_CP_RESOLVER_RESO];
+
+ DEBUG_PRINT_L1("Bd4> ===========================================\r\n");
+ DEBUG_PRINT_L1("Bd4> Dram Diameter : %d\t(mm)\r\n", dram_diameter);
+ DEBUG_PRINT_L1("Bd4> CCable Diameter : %d\t(mm)\r\n", dis_correct_value);
+ DEBUG_PRINT_L1("Bd4> Resolver Resolution : %d\t(bit)\r\n", rresolution);
+ DEBUG_PRINT_L1("Bd4> -------------------------------------------\r\n", rresolution);
+ DEBUG_PRINT_L1("Bd4> Real Diameter : %d\t(mm)\r\n", (dram_diameter+dis_correct_value));
+ DEBUG_PRINT_L1("Bd4> Rotation Pulse / 1round : %d\t(pulse)\r\n", (int)(pow(2.0, (double)rresolution)*4));
+ DEBUG_PRINT_L1("Bd4> Distance / 1pulse : %lf\t(mm)\r\n", (double)( ((dram_diameter+dis_correct_value)/100) * PAI / (pow(2.0, (double)rresolution)*4 ) ) );
+ DEBUG_PRINT_L1("Bd4> ===========================================\r\n");
+
+ pulse = wheel.getPulses();
+ //pulse += 1;
+
+ DEBUG_PRINT_L1("Bd4> PULSE: %07d, ", pulse);
+ DEBUG_PRINT_L1("DISTANCE: %04d (mm)\r\n", (int)( pulse * ( (double)( ((dram_diameter+dis_correct_value)/100) * PAI / (pow(2.0, (double)rresolution)*4 ) ) ) ) );
+ DEBUG_PRINT_L1("Bd4> ---------------------------------------\r\n");
+
+ led2 = LED_ON;
+ }
+ break;
+ }
+
+ // Following instruction should do on thread, because this time of operation is lottle longer. !!!
+
+/*
+ DEBUG_PRINT_L0("Bd4> %05f", mcc.rd_motorCurrent(11)); DEBUG_PRINT_L0("\t");
+ DEBUG_PRINT_L0("%05f", mcc.rd_motorCurrent(12)); DEBUG_PRINT_L0("\t");
+ DEBUG_PRINT_L0("%05f", mcc.rd_motorCurrent(21)); DEBUG_PRINT_L0("\t");
+ DEBUG_PRINT_L0("%05f", mcc.rd_motorCurrent(22)); DEBUG_PRINT_L0("\t");
+ DEBUG_PRINT_L0("%05f", mcc.rd_motorCurrent(31)); DEBUG_PRINT_L0("\t");
+ DEBUG_PRINT_L0("%05f", mcc.rd_motorCurrent(32)); DEBUG_PRINT_L0("\r\n");
+*/
+
+
+
+
+
+ //Thread::wait(1);
+ wait_ms(1);
+
+ }
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/QEI.lib Thu Jul 26 00:19:47 2018 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/aberk/code/QEI/#5c2ad81551aa
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/common.h Thu Jul 26 00:19:47 2018 +0000
@@ -0,0 +1,105 @@
+/*
+ * mbed LPC824-Max header
+ *
+ */
+
+// If you use moving type winch , then should comment out followings !!
+// #define FFWinchPhaseSetting // Valid if Fix falling winch system is using.
+
+#define __DEBUG__
+#ifdef __DEBUG__
+#define DEBUG_PRINT(...) pc.printf(__VA_ARGS__)
+#else
+#define DEBUG_PRINT(...) ;
+//#define DEBUG_PRINT(...) 1 ? (void)0 : pc.printf(__VA_ARGS__)
+#endif
+/* Information */
+#define LatestUpDate "2016.09.27"
+#define ProgramRevision "Rev 2.30"
+#define Author "Y.Saito(zinsor)"
+#define Company "Revast Co.,Ltd"
+
+
+/* Debug macro */
+#define __DEBUG_L0__
+#define __DEBUG_L1__
+//#define __DEBUG_L2__
+//#define __DEBUG_L3__
+//#define __DEBUG_L4__
+
+#ifdef __DEBUG_L0__
+ #define DEBUG_PRINT_L0(...) pc.printf(__VA_ARGS__)
+#else
+ #define DEBUG_PRINT_L0(...)
+#endif
+#ifdef __DEBUG_L1__
+ #define DEBUG_PRINT_L1(...) pc.printf(__VA_ARGS__)
+#else
+ #define DEBUG_PRINT_L1(...)
+#endif
+#ifdef __DEBUG_L2__
+ #define DEBUG_PRINT_L2(...) pc.printf(__VA_ARGS__)
+#else
+ #define DEBUG_PRINT_L2(...)
+#endif
+#ifdef __DEBUG_L3__
+ #define DEBUG_PRINT_L3(...) pc.printf(__VA_ARGS__)
+#else
+ #define DEBUG_PRINT_L3(...)
+#endif
+#ifdef __DEBUG_L4__
+ #define DEBUG_PRINT_L4(...) pc.printf(__VA_ARGS__)
+#else
+ #define DEBUG_PRINT_L4(...)
+#endif
+
+enum{
+ MOTOR_RFCRW, // RF crawler
+ MOTOR_LBCRW, // LB crawler
+ MOTOR_RFTFM, // RF Transform
+ MOTOR_LBTFM, // LB transform
+ MOTOR_WINCH, // Winch
+ MOTOR_CMPAN, // Pan/Tilt
+ MOTOR_CTILT
+};
+
+enum{
+ MOTOR_DIR_FWD, // Motor forwaed rotation
+ MOTOR_DIR_RVS, // Motor reverse rotation
+ MOTOR_DIR_STP // Motor stop
+};
+
+#define NumberOfI2CCommand 12
+enum{
+ I2C_CP_COMMAND, // instruction command
+ I2C_CP_MOTORNO, // motor number
+ I2C_CP_M_DIR, // motor rotation direction
+ I2C_CP_M_CNTTH_U, // motor current limit detection threshold upper byte
+ I2C_CP_M_CNTTH_L, // motor current limit detection threshold lower byte
+ I2C_CP_WDRAM_DIA_UPPER, // winch dram motor diameter upper
+ I2C_CP_WDRAM_DIA_LOWER, // winch dram motor diameter lower
+ I2C_CP_CCABLE_DIA_UPPER, // cable diameter upper byte
+ I2C_CP_CCABLE_DIA_LOWER, // cable diameter lower byte
+ I2C_CP_RESOLVER_RESO, // resolver resolution (bit)
+ I2C_CP_PRESET_CPOS_UPPER, // preset position upper
+ I2C_CP_PRESET_CPOS_LOWER, // preset position lower
+};
+
+
+#define ROTATE_PER_RESOLUTION 24
+#define REAL_THREAD_DIAMETER 63 // 60+3
+//#define ROTATION_DISTANCE 197.82f // (60+3)*3.14
+#define ROTATION_PULSE_PER_1ROUND 16384 // 2^12(bit) * 4
+#define PAI 3.14159265359
+
+/* ***************** */
+/* Target definition */
+/* ***************** */
+#define I2C_ADDRESS_RESOLVER 0x02
+
+/* Command Packet */
+#define CLEAR_RESOLVER_POSITION 'X'
+#define HELLO_PACKET 'A'
+
+#define LED_ON 0
+#define LED_OFF 1
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Thu Jul 26 00:19:47 2018 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/f141b2784e32 \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/mcchk.cpp Thu Jul 26 00:19:47 2018 +0000
@@ -0,0 +1,536 @@
+#include "mbed.h"
+#include "common.h"
+#include <math.h>
+#include "mcchk.h"
+
+
+// Motor current
+AnalogIn mctr3_m2c(A5); // LB crawler
+AnalogIn mctr3_m1c(A4); // RF crawler
+AnalogIn mctr2_m2c(A3); // LB transform
+AnalogIn mctr2_m1c(A2); // RF Transform
+AnalogIn mctr1_m2c(A1); // Tilt
+AnalogIn mctr1_m1c(A0); // B1: Pan, B2:Winch
+
+
+mcchk::mcchk()
+{
+ cnt_mclock_LBCRW_f = 0; // motor lock counter
+ cnt_mclock_LBCRW_r = 0; // motor lock counter
+ cnt_mclock_RFCRW_f = 0; // motor lock counter
+ cnt_mclock_RFCRW_r = 0; // motor lock counter
+ cnt_mclock_LBTFM_f = 0; // motor lock counter
+ cnt_mclock_LBTFM_r = 0; // motor lock counter
+ cnt_mclock_RFTFM_f = 0; // motor lock counter
+ cnt_mclock_RFTFM_r = 0; // motor lock counter
+ cnt_mclock_CMPAN_f = 0; // motor lock counter
+ cnt_mclock_CMPAN_r = 0; // motor lock counter
+ cnt_mclock_CTILT_f = 0; // motor lock counter
+ cnt_mclock_CTILT_r = 0; // motor lock counter
+ cnt_mclock_WINCH_f = 0; // motor lock counter
+ cnt_mclock_WINCH_r = 0; // motor lock counter
+}
+
+
+// --------------------------------------------------------------
+// Motor Current Calibration Function
+// Arg: Motor Number 1 - 3
+// --------------------------------------------------------------
+void mcchk::set_init_mc( int cnt )
+{
+
+ m_LBCRW_center_value = 0;
+ m_RFCRW_center_value = 0;
+ m_LBTFM_center_value = 0;
+ m_RFTFM_center_value = 0;
+ m_WINCH_center_value = 0;
+ m_CMPAN_center_value = 0;
+ m_CTILT_center_value = 0;
+
+ for( int i = 0; i < cnt; i++ )
+ {
+ m_LBCRW_center_value += mctr3_m1c.read() * 100.0f;
+ m_RFCRW_center_value += mctr3_m2c.read() * 100.0f;
+ m_LBTFM_center_value += mctr2_m1c.read() * 100.0f;
+ m_RFTFM_center_value += mctr2_m2c.read() * 100.0f;
+ m_WINCH_center_value += mctr1_m1c.read() * 100.0f;
+ m_CMPAN_center_value += mctr1_m1c.read() * 100.0f;
+ m_CTILT_center_value += mctr1_m2c.read() * 100.0f;
+ }
+
+ m_LBCRW_center_value /= cnt;
+ m_RFCRW_center_value /= cnt;
+ m_LBTFM_center_value /= cnt;
+ m_RFTFM_center_value /= cnt;
+ m_WINCH_center_value /= cnt;
+ m_CMPAN_center_value /= cnt;
+ m_CTILT_center_value /= cnt;
+}
+
+
+float mcchk::rd_motorCurrent( int no )
+{
+ if( no == 11 ) return mctr1_m1c.read()*100.0f;
+ else if( no == 12 ) return mctr1_m2c.read()*100.0f;
+ else if( no == 21 ) return mctr2_m1c.read()*100.0f;
+ else if( no == 22 ) return mctr2_m2c.read()*100.0f;
+ else if( no == 31 ) return mctr3_m1c.read()*100.0f;
+ else if( no == 32 ) return mctr3_m2c.read()*100.0f;
+ return 0;
+}
+
+
+bool mcchk::rdnchk_motorCurrent(
+ int8_t motor_number,
+ int8_t motor_dir,
+ int8_t motor_lock_chk_cnt
+){
+ bool mcchk = false;
+ bool rts = false;
+
+ // Check RF-Crawler
+ if( motor_number == MOTOR_RFCRW )
+ {
+ if( motor_dir == MOTOR_DIR_FWD )
+ {
+ mcchk = chk_motor_lock( mctr3_m1c.read()*100.0f, m_RFCRW_center_value, mc_th_RFCRW_f);
+ if( mcchk == true )
+ {
+ cnt_mclock_RFCRW_f += 1;
+ }
+ else
+ { if( cnt_mclock_RFCRW_f > 0 )
+ {
+ cnt_mclock_RFCRW_f -= 1;
+ }
+ else
+ {
+ cnt_mclock_RFCRW_f = 0;
+ }
+ }
+ if( cnt_mclock_RFCRW_f > motor_lock_chk_cnt )
+ {
+ flg_mclock_RFCRW = 1;
+ }
+ else
+ {
+ flg_mclock_RFCRW = 0;
+ rts = false; // NO LOCK
+ }
+ }
+ else
+ {
+ mcchk = chk_motor_lock( mctr3_m1c.read()*100.0f, m_RFCRW_center_value, mc_th_RFCRW_r);
+ if( mcchk == true )
+ {
+ cnt_mclock_RFCRW_r += 1;
+ }
+ else
+ { if( cnt_mclock_RFCRW_r > 0 )
+ {
+ cnt_mclock_RFCRW_r -= 1;
+ }
+ else
+ {
+ cnt_mclock_RFCRW_r = 0;
+ }
+ }
+ if( cnt_mclock_RFCRW_r > motor_lock_chk_cnt )
+ {
+ flg_mclock_RFCRW = 1;
+ rts = true; // LOCK
+ }
+ else
+ {
+ flg_mclock_RFCRW = 0;
+ rts = false; // NO LOCK
+ }
+ }
+ }
+ // Check LB-Crawler
+ else if( motor_number == MOTOR_LBCRW )
+ {
+ if( motor_dir == MOTOR_DIR_FWD )
+ {
+ mcchk = chk_motor_lock( mctr3_m2c.read()*100.0f, m_LBCRW_center_value, mc_th_LBCRW_f);
+ if( mcchk == true )
+ {
+ cnt_mclock_LBCRW_f += 1;
+ }
+ else
+ { if( cnt_mclock_LBCRW_f > 0 )
+ {
+ cnt_mclock_LBCRW_f -= 1;
+ }
+ else
+ {
+ cnt_mclock_LBCRW_f = 0;
+ }
+ }
+ if( cnt_mclock_LBCRW_f > motor_lock_chk_cnt )
+ {
+ flg_mclock_LBCRW = 1;
+ rts = true; // LOCK
+ }
+ else
+ {
+ flg_mclock_LBCRW = 0;
+ rts = false; // LOCK
+ }
+ }
+ else
+ {
+ mcchk = chk_motor_lock( mctr3_m2c.read()*100.0f, m_LBCRW_center_value, mc_th_LBCRW_r);
+ if( mcchk == true )
+ {
+ cnt_mclock_LBCRW_r += 1;
+ }
+ else
+ { if( cnt_mclock_LBCRW_r > 0 )
+ {
+ cnt_mclock_LBCRW_r -= 1;
+ }
+ else
+ {
+ cnt_mclock_LBCRW_r= 0;
+ }
+ }
+ if( cnt_mclock_LBCRW_r > motor_lock_chk_cnt )
+ {
+ flg_mclock_LBCRW = 1;
+ rts = true; // LOCK
+ }
+ else
+ {
+ flg_mclock_LBCRW = 0;
+ rts = false; // LOCK
+ }
+ }
+ }
+ // Check RF-Crawler
+ else if( motor_number == MOTOR_RFTFM )
+ {
+ if( motor_dir == MOTOR_DIR_FWD )
+ {
+ mcchk = chk_motor_lock( mctr2_m1c.read()*100.0f, m_RFTFM_center_value, mc_th_RFTFM_f);
+ if( mcchk == true )
+ {
+ cnt_mclock_RFTFM_f += 1;
+ }
+ else
+ { if( cnt_mclock_RFTFM_f > 0 )
+ {
+ cnt_mclock_RFTFM_f -= 1;
+ }
+ else
+ {
+ cnt_mclock_RFTFM_f = 0;
+ }
+ }
+ if( cnt_mclock_RFTFM_f > motor_lock_chk_cnt )
+ {
+ flg_mclock_RFTFM = 1;
+ rts = true; // LOCK
+ }
+ else
+ {
+ flg_mclock_RFTFM = 0;
+ rts = false; // LOCK
+ }
+ }
+ else
+ {
+ rts = chk_motor_lock( mctr2_m1c.read()*100.0f, m_RFTFM_center_value, mc_th_RFTFM_r);
+ if( rts == true )
+ {
+ cnt_mclock_RFTFM_r += 1;
+ }
+ else
+ { if( cnt_mclock_RFTFM_r > 0 )
+ {
+ cnt_mclock_RFTFM_r -= 1;
+ }
+ else
+ {
+ cnt_mclock_RFTFM_r = 0;
+ }
+ }
+ if( cnt_mclock_RFTFM_r > motor_lock_chk_cnt )
+ {
+ flg_mclock_RFTFM = 1;
+ rts = true; // LOCK
+ }
+ else
+ {
+ flg_mclock_RFTFM = 0;
+ rts = false; // LOCK
+ }
+ }
+ }
+ // Check LB-Transform
+ else if( motor_number == MOTOR_LBTFM )
+ {
+ if( motor_dir == MOTOR_DIR_FWD )
+ {
+ mcchk = chk_motor_lock( mctr2_m2c.read()*100.0f, m_LBTFM_center_value, mc_th_LBTFM_f);
+ if( mcchk == true )
+ {
+ cnt_mclock_LBTFM_f += 1;
+ }
+ else
+ { if( cnt_mclock_LBTFM_f > 0 )
+ {
+ cnt_mclock_LBTFM_f -= 1;
+ }
+ else
+ {
+ cnt_mclock_LBTFM_f = 0;
+ }
+ }
+ if( cnt_mclock_LBTFM_f > motor_lock_chk_cnt )
+ {
+ flg_mclock_LBTFM = 1;
+ rts = true; // LOCK
+ }
+ else
+ {
+ flg_mclock_LBTFM = 0;
+ rts = false; // LOCK
+ }
+ }
+ else
+ {
+ mcchk = chk_motor_lock( mctr2_m2c.read()*100.0f, m_LBTFM_center_value, mc_th_LBTFM_r);
+ if( mcchk == true )
+ {
+ cnt_mclock_LBTFM_r += 1;
+ }
+ else
+ { if( cnt_mclock_LBTFM_r > 0 )
+ {
+ cnt_mclock_LBTFM_r -= 1;
+ }
+ else
+ {
+ cnt_mclock_LBTFM_r = 0;
+ }
+ }
+ if( cnt_mclock_LBTFM_r > motor_lock_chk_cnt )
+ {
+ flg_mclock_LBTFM = 1;
+ rts = true; // LOCK
+ }
+ else
+ {
+ flg_mclock_LBTFM = 0;
+ rts = false; // LOCK
+ }
+ }
+ }
+
+ // Check Winch
+ else if( motor_number == MOTOR_WINCH )
+ {
+ if( motor_dir == MOTOR_DIR_FWD )
+ {
+ mcchk= chk_motor_lock( mctr1_m1c.read()*100.0f, m_WINCH_center_value, mc_th_WINCH_f);
+ if( mcchk == true )
+ {
+ cnt_mclock_WINCH_f += 1;
+ }
+ else
+ { if( cnt_mclock_WINCH_f > 0 )
+ {
+ cnt_mclock_WINCH_f -= 1;
+ }
+ else
+ {
+ cnt_mclock_WINCH_f = 0;
+ }
+
+ }
+ if( cnt_mclock_WINCH_f > motor_lock_chk_cnt )
+ {
+ flg_mclock_WINCH = 1;
+ rts = true; // LOCK
+ }
+ else
+ {
+ flg_mclock_WINCH = 0;
+ rts = false; // LOCK
+ }
+ }
+ else
+ {
+ mcchk = chk_motor_lock( mctr1_m1c.read()*100.0f, m_WINCH_center_value, mc_th_WINCH_r);
+ if( mcchk == true )
+ {
+ cnt_mclock_WINCH_r += 1;
+ }
+ else
+ { if( cnt_mclock_WINCH_r > 0 )
+ {
+ cnt_mclock_WINCH_r -= 1;
+ }
+ else
+ {
+ cnt_mclock_WINCH_r = 0;
+ }
+ }
+ if( cnt_mclock_WINCH_r > motor_lock_chk_cnt )
+ {
+ flg_mclock_WINCH = 1;
+ rts = true; // LOCK
+ }
+ else
+ {
+ flg_mclock_WINCH = 0;
+ rts = false; // LOCK
+ }
+ }
+ }
+ // Check Tilt
+ else if( motor_number == MOTOR_CMPAN )
+ {
+ if( motor_dir == MOTOR_DIR_FWD )
+ {
+ mcchk = chk_motor_lock( mctr1_m1c.read()*100.0f, m_CMPAN_center_value, mc_th_CMPAN_f);
+ if( mcchk == true )
+ {
+ cnt_mclock_CMPAN_f += 1;
+ }
+ else
+ { if( cnt_mclock_CMPAN_f > 0 )
+ {
+ cnt_mclock_CMPAN_f -= 1;
+ }
+ else
+ {
+ cnt_mclock_CMPAN_f = 0;
+ }
+
+ }
+ if( cnt_mclock_CMPAN_f > motor_lock_chk_cnt )
+ {
+ flg_mclock_CMPAN = 1;
+ rts = true; // LOCK
+ }
+ else
+ {
+ flg_mclock_CMPAN = 0;
+ rts = false; // LOCK
+ }
+ }
+ else
+ {
+ mcchk = chk_motor_lock( mctr1_m1c.read()*100.0f, m_CMPAN_center_value, mc_th_CMPAN_r);
+ if( mcchk == true )
+ {
+ cnt_mclock_CMPAN_r += 1;
+ }
+ else
+ { if( cnt_mclock_CMPAN_r > 0 )
+ {
+ cnt_mclock_CMPAN_r -= 1;
+ }
+ else
+ {
+ cnt_mclock_CMPAN_r = 0;
+ }
+ }
+ if( cnt_mclock_CMPAN_r > motor_lock_chk_cnt )
+ {
+ flg_mclock_CMPAN = 1;
+ rts = true; // LOCK
+ }
+ else
+ {
+ flg_mclock_CMPAN = 0;
+ rts = false; // LOCK
+ }
+ }
+ }
+ // Check Tilt
+ else if( motor_number == MOTOR_CTILT )
+ {
+ if( motor_dir == MOTOR_DIR_FWD )
+ {
+ mcchk = chk_motor_lock( mctr1_m2c.read()*100.0f, m_CTILT_center_value, mc_th_CTILT_f);
+ if( mcchk == true )
+ {
+ cnt_mclock_CTILT_f += 1;
+ }
+ else
+ { if( cnt_mclock_CTILT_f > 0 )
+ {
+ cnt_mclock_CTILT_f -= 1;
+ }
+ else
+ {
+ cnt_mclock_CTILT_f = 0;
+ }
+ }
+ if( cnt_mclock_CTILT_f > motor_lock_chk_cnt )
+ {
+ flg_mclock_CTILT = 1;
+ rts = true; // LOCK
+ }
+ else
+ {
+ flg_mclock_CTILT = 0;
+ rts = false; // LOCK
+ }
+ }
+ else
+ {
+ mcchk = chk_motor_lock( mctr1_m2c.read()*100.0f, m_CTILT_center_value, mc_th_CTILT_r);
+ if( mcchk == true )
+ {
+ cnt_mclock_CTILT_r += 1;
+ }
+ else
+ { if( cnt_mclock_CTILT_r > 0 )
+ {
+ cnt_mclock_CTILT_r -= 1;
+ }
+ else
+ {
+ cnt_mclock_CTILT_r = 0;
+ }
+ }
+ if( cnt_mclock_CTILT_r > motor_lock_chk_cnt )
+ {
+ flg_mclock_CTILT = 1;
+ rts = true; // LOCK
+ }
+ else
+ {
+ flg_mclock_CTILT = 0;
+ rts = false; // LOCK
+ }
+ }
+ }
+ return rts;
+}
+
+bool mcchk::chk_motor_lock(
+ float motor_current, // motor current
+ float motor_center_value, // motor current center value
+ int mc_threshold // motor current threshold
+){
+// int32_t mc_abs_pct;
+
+ _cnt_now = motor_current;
+ _cnt_center = motor_center_value;
+ _cnt_th = mc_threshold;
+
+ mc_abs_pct = abs((int32_t)((motor_current - motor_center_value)*100.0f));
+ if( mc_abs_pct > mc_threshold )
+ {
+ return true; // Motor current is over threshold.
+ }
+ else
+ {
+ return false; // Motor current isn't over threshold.
+ }
+}
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/mcchk.h Thu Jul 26 00:19:47 2018 +0000
@@ -0,0 +1,66 @@
+class mcchk
+{
+private:
+ float m_LBCRW_center_value;
+ float m_RFCRW_center_value;
+ float m_LBTFM_center_value;
+ float m_RFTFM_center_value;
+ float m_WINCH_center_value;
+ float m_CMPAN_center_value;
+ float m_CTILT_center_value;
+
+ int8_t flg_mclock_LBCRW; // bit0: forward, bit1: reverse
+ int8_t flg_mclock_RFCRW; // bit0: forward, bit1: reverse
+ int8_t flg_mclock_LBTFM; // bit0: forward, bit1: reverse
+ int8_t flg_mclock_RFTFM; // bit0: forward, bit1: reverse
+ int8_t flg_mclock_CMPAN; // bit0: forward, bit1: reverse
+ int8_t flg_mclock_CTILT; // bit0: forward, bit1: reverse
+ int8_t flg_mclock_WINCH; // bit0: forward, bit1: reverse
+
+public:
+
+ int8_t cnt_mclock_LBCRW_f; // motor lock counter
+ int8_t cnt_mclock_LBCRW_r; // motor lock counter
+ int8_t cnt_mclock_RFCRW_f; // motor lock counter
+ int8_t cnt_mclock_RFCRW_r; // motor lock counter
+ int8_t cnt_mclock_LBTFM_f; // motor lock counter
+ int8_t cnt_mclock_LBTFM_r; // motor lock counter
+ int8_t cnt_mclock_RFTFM_f; // motor lock counter
+ int8_t cnt_mclock_RFTFM_r; // motor lock counter
+ int8_t cnt_mclock_CMPAN_f; // motor lock counter
+ int8_t cnt_mclock_CMPAN_r; // motor lock counter
+ int8_t cnt_mclock_CTILT_f; // motor lock counter
+ int8_t cnt_mclock_CTILT_r; // motor lock counter
+ int8_t cnt_mclock_WINCH_f; // motor lock counter
+ int8_t cnt_mclock_WINCH_r; // motor lock counter
+
+
+ int16_t mc_th_LBCRW_f;
+ int16_t mc_th_LBCRW_r;
+ int16_t mc_th_RFCRW_f;
+ int16_t mc_th_RFCRW_r;
+ int16_t mc_th_LBTFM_f;
+ int16_t mc_th_LBTFM_r;
+ int16_t mc_th_RFTFM_f;
+ int16_t mc_th_RFTFM_r;
+ int16_t mc_th_WINCH_f;
+ int16_t mc_th_WINCH_r;
+ int16_t mc_th_CMPAN_f;
+ int16_t mc_th_CMPAN_r;
+ int16_t mc_th_CTILT_f;
+ int16_t mc_th_CTILT_r;
+
+ int32_t mc_abs_pct;
+
+ float _cnt_now;
+ float _cnt_center;
+ int _cnt_th;
+
+ bool motor_lock_flg;
+
+ mcchk();
+ void set_init_mc( int );
+ bool rdnchk_motorCurrent( int8_t, int8_t, int8_t );
+ bool chk_motor_lock( float, float, int );
+ float mcchk::rd_motorCurrent( int no );
+};
\ No newline at end of file