test4

Dependencies:   mbed BufferedSerial LS7366LIB2 FastPWM

Revision:
1:7b5469bf5994
Parent:
0:7cff999a7f5c
--- a/button.h	Mon May 11 08:47:18 2020 +0000
+++ b/button.h	Mon Jul 27 23:29:30 2020 +0000
@@ -4,15 +4,19 @@
 #include "MCP23S17.h"
 #include "7_segment.h"
 
-#define BUTTON_DELAY        50      // 75ms
-#define BUTTON_FAST_LIMIT   45
-#define FLASH_SAVE_DELAY    3000  // 400000, 10min
+#define BUTTON_DELAY            60      // 75ms
+#define BUTTON_FAST_LIMIT       50
+#define FLASH_SAVE_DELAY        3333  // 3333, 5 sec
+#define PGAIN_CORRETION_FACTOR  0.4
+
 void reset_speed_pid();
 void button_pid_info();
 
 bool menual_mode=false;
 bool set_mode = false;
+bool set_cmd_togle = false;
 uint8_t set_command = 0;
+uint16_t set_cmd_delay = 200;
 uint16_t axis_button=0x0fff;
 uint32_t axis_cmd_print_delay = 0;
 uint8_t btn1_push_check[12] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
@@ -46,41 +50,63 @@
 
 void Button_Init()
 {
-    insert_pgain = Convert_double_Pgain(Button_Read_Flash(0));
-    insert_input = Convert_double_Igain(Button_Read_Flash(4));
-
-    //printf("fisrt Gain pgain : %f, input : %f\r\n", insert_pgain, insert_input);
-    
-    for(int i = 1; i < 7; i++)
+    if (Button_Read_Flash(0) != 0xFFFFFFFF)
     {
-        set_data[i * 10 + 2] = insert_pgain;
-        position_Pgain[i-1] = insert_pgain;
-    }
-    for(int i = 1; i < 7; i++)
-    {
-        set_data[i * 10 + 3] = insert_input;
-        Position_input_filter[i-1] = insert_input;
-    }
-    
-    if(insert_pgain != 0xFFFFFFFF)
-    {
+        insert_pgain = Convert_double_Pgain(Button_Read_Flash(0));
+        insert_pgain *= PGAIN_CORRETION_FACTOR;
+        
+        for (int i = 1; i < 7; i++)
+        {
+            set_data[i * 10 + 2] = insert_pgain;
+            position_Pgain[i - 1] = insert_pgain;
+        }
         btn2_pgain_data[0] = (uint8_t)(Button_Read_Flash(0) / 100);
         btn2_pgain_data[1] = (uint8_t)((Button_Read_Flash(0) % 100) / 10);
-        btn2_pgain_data[2] = (uint8_t)((Button_Read_Flash(0) % 10) / 1);
+        btn2_pgain_data[2] = (uint8_t)(Button_Read_Flash(0) % 10);
         Pgain_segment(btn2_pgain_data[0], btn2_pgain_data[1], btn2_pgain_data[2], SEG_DELAY_TIME);
     }
     else
     {
-        Pgain_segment(2, 5, 0, SEG_DELAY_TIME);
+        insert_pgain = 25.0 * PGAIN_CORRETION_FACTOR;
+        for (int i = 1; i < 7; i++)
+        {
+            set_data[i * 10 + 2] = insert_pgain;
+            position_Pgain[i - 1] = insert_pgain;
+        }
+        btn2_pgain_data[0] = (uint8_t)2;
+        btn2_pgain_data[1] = (uint8_t)5;
+        btn2_pgain_data[2] = (uint8_t)0;
+        Pgain_segment(btn2_pgain_data[0], btn2_pgain_data[1], btn2_pgain_data[2], SEG_DELAY_TIME);
+        flash_save_flag = true;
     }
-    if(insert_input != 0xFFFFFFFF)
+
+
+    if (Button_Read_Flash(4) != 0xFFFFFFFF)
     {
+        insert_input = Convert_double_Igain(Button_Read_Flash(4));
+        
+        for (int i = 1; i < 7; i++)
+        {
+            set_data[i * 10 + 3] = insert_input;
+            Position_input_filter[i - 1] = insert_input;
+        }
         btn2_input_data[0] = (uint8_t)(Button_Read_Flash(4) / 10);
         btn2_input_data[1] = (uint8_t)(Button_Read_Flash(4) % 10);
         Input_segment(btn2_input_data[0], btn2_input_data[1], SEG_DELAY_TIME);
     }
+    else
     {
+        insert_input = 0.05;
+
+        for (int i = 1; i < 7; i++)
+        {
+            set_data[i * 10 + 3] = insert_input;
+            Position_input_filter[i - 1] = insert_input;
+        }
+        btn2_input_data[0] = (uint8_t)5;
+        btn2_input_data[1] = (uint8_t)0;
         Input_segment(5, 0, SEG_DELAY_TIME);
+        flash_save_flag = true;
     }
 }
 
@@ -290,6 +316,7 @@
         }
         btn2_push_check[1] = 0;
         insert_pgain = (double)btn2_pgain_data[0] * 10 + (double)btn2_pgain_data[1] + (double)btn2_pgain_data[2] * 0.1; // inser set_data
+        insert_pgain *= PGAIN_CORRETION_FACTOR;
         for(int i = 1; i < 7; i++)
         {
             set_data[i * 10 + 2] = insert_pgain;
@@ -326,6 +353,7 @@
         }
         btn2_push_check[0] = 0;
         insert_pgain = (double)btn2_pgain_data[0] * 10 + (double)btn2_pgain_data[1] + (double)btn2_pgain_data[2] * 0.1; // inser set_data
+        insert_pgain *= PGAIN_CORRETION_FACTOR;
         for(int i = 1; i < 7; i++)
         {
             set_data[i * 10 + 2] = insert_pgain;
@@ -407,60 +435,33 @@
 {
     uint8_t button_dataA = MCP_Read(MCP_BTN1_CS, MCP_GPIOA);
     uint8_t button_dataB = MCP_Read(MCP_BTN1_CS, MCP_GPIOB);
-    
+        
     axis_button=(((uint16_t)button_dataB & 0x000f )<<8 )+button_dataA;
+        
     if(!set_mode)
-    {    
-        if(((set_command == 0) || (set_command == 1))&& ((axis_button & 0b000000100000) == 0)) //bit6 > AXIS1(+)
-        {
-            set_command = 1;
-        }
-        else if(((set_command == 1) || (set_command == 2)) && ((axis_button & 0b100000000000) == 0)) //bit12 > AXIS1(-)
-        {
-            set_command = 2;
-        }
-        else if(((set_command == 2) || (set_command == 3)) && ((axis_button & 0b000000010000) == 0)) //bit5 > AXIS2(+)
-        {
-            set_command = 3;
-        }
-        else if(((set_command == 3) || (set_command == 4)) && ((axis_button & 0b010000000000) == 0)) //bit11 > AXIS2(-)
-        {
-            set_command = 4;
-        }
-        else if(((set_command == 4) || (set_command == 5)) && ((axis_button & 0b000000001000) == 0)) //bit4 > AXIS3(+)
-        {
-            set_command = 5;
-        }
-        else if(((set_command == 5) || (set_command == 6)) && ((axis_button & 0b001000000000) == 0)) //bit10 > AXIS3(-)
+    {
+        set_cmd_delay++;
+        if(set_cmd_delay > 200) set_cmd_delay = 200;
+        
+        if((axis_button & 0b000000100000) == 0) //bit6 > AXIS1(+)
         {
-            set_command = 6;
-        }
-        else if(((set_command == 6) || (set_command == 7)) && ((axis_button & 0b000000000100) == 0)) //bit3 > AXIS4(+)
-        {
-            set_command = 7;
-        }
-        else if(((set_command == 7) || (set_command == 8)) && ((axis_button & 0b000100000000) == 0)) //bit9 > AXIS4(-)
-        {
-            set_command = 8;
-        }
-        else if(((set_command == 8) || (set_command == 9)) && ((axis_button & 0b000000000010) == 0)) //bit2 > AXIS5(+)
-        {
-            set_command = 9;
+            if(set_cmd_delay > 100)
+            {
+                set_cmd_togle = true;
+                set_cmd_delay = 0;
+            }
         }
-        else if(((set_command == 9) || (set_command == 10)) && ((axis_button & 0b000010000000) == 0)) //bit8 > AXIS5(-)
-        {
-            set_command = 10;
-        }
-        else if(((set_command == 10) || (set_command == 11)) && ((axis_button & 0b000000000001) == 0)) //bit1 > AXIS6(+)
+        else if((set_cmd_togle == true) && (axis_button  == 0b111111111111))
         {
-            set_command = 11;
+            set_cmd_togle = false;
+            set_command++;
+        
+            if(set_command >= 5)
+            {
+                set_mode = true;
+            }
         }
-        else if((set_command == 11) && ((axis_button & 0b000001000000) == 0)) //bit7 > AXIS6(-)
-        {
-            set_command = 0;
-            set_mode = true;
-        }
-        else if(axis_button  == 0b111111111111)
+        else if(axis_button == 0b111111111111)
         {
         }
         else
@@ -472,62 +473,62 @@
     {                
         if( (axis_button & 0b000001000000) == 0) //bit1 > AXIS6(-)
         {
-            btn1_push_check[5]--;
+            button_offset_posion[5]--;
             menual_mode=true;
         }
         else if( (axis_button & 0b000010000000) == 0) //bit2 > AXIS5(-)
         {
-            btn1_push_check[4]--;
+            button_offset_posion[4]--;
             menual_mode=true;
         }
         else if( (axis_button & 0b000100000000) == 0) //bit3 > AXIS4(-)
         {
-            btn1_push_check[3]--;
+            button_offset_posion[3]--;
             menual_mode=true;
         }
         else if( (axis_button & 0b001000000000) == 0) //bit4 > AXIS3(-)
         {
-            btn1_push_check[2]--;
+            button_offset_posion[2]--;
             menual_mode=true;
         }
         else if( (axis_button & 0b000000000010) == 0) //bit5 > AXIS5(+)
         {
-            btn1_push_check[4]++;
+            button_offset_posion[4]++;
             menual_mode=true;
         }
         else if( (axis_button & 0b000000000001) == 0) //bit6 > AXIS6(+)
         {
-            btn1_push_check[5]++;
+            button_offset_posion[5]++;
             menual_mode=true;
         }
         else if( (axis_button & 0b010000000000) == 0) //bit7 > AXIS2(-)
         {
-            btn1_push_check[1]--;
+            button_offset_posion[1]--;
             menual_mode=true;
         }
         else if( (axis_button & 0b100000000000) == 0) //bit8 > AXIS1(-)
         {
-            btn1_push_check[0]--;
+            button_offset_posion[0]--;
             menual_mode=true;
         }
         else if( (axis_button & 0b000000100000) == 0) //bit9 > AXIS1(+)
         {
-            btn1_push_check[0]++;
+            button_offset_posion[0]++;
             menual_mode=true;
         }
         else if( (axis_button & 0b000000010000) == 0) //bit10 > AXIS2(+)
         {
-            btn1_push_check[1]++;
+            button_offset_posion[1]++;
             menual_mode=true;
         }
         else if( (axis_button & 0b000000001000) == 0) //bit11 > AXIS3(+)
         {
-            btn1_push_check[2]++;
+            button_offset_posion[2]++;
             menual_mode=true;
         }
         else if( (axis_button & 0b000000000100) == 0) //bit12 > AXIS4(+)
         {
-            btn1_push_check[3]++;
+            button_offset_posion[3]++;
             menual_mode=true;
             
         }