找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 4145|回复: 3
打印 上一主题 下一主题
收起左侧

arduino智能定量包装秤系统设计

[复制链接]
跳转到指定楼层
楼主
    采用变频器控制皮带给料机,通过调节电机转速灵活调节给料机的给料速度,改善了给料机构的性能;采用基于模糊控制的在线调整定量控制算法,旨在保证计量精度的前提下,尽量提高计量速度,并适应对多种物料多种定量的包装计量,减少物料特性变化对计量精度的影响。
1)定量包装秤整体结构设计;
2)自动定量包装秤定量控制算法设计;
3)基于模糊控制的在线调整定量控制优化算法设计;
4)自动定量包装秤模糊控制器设计;
5)自动定量包装秤控制系统硬件设计;
6)分不同物料、不同定量值进行对比实验。

制作出来的实物图:


arduino源程序如下:
  1. #include "HX711.h"
  2. #include <MsTimer2.h>
  3. #include<Keypad.h>
  4. #include <Wire.h>
  5. #include <LiquidCrystal_I2C.h>

  6. int Motor = 10;
  7. int LED   = 11;
  8. unsigned char Pwm_Motor = 0;
  9. const byte ROWS = 4;
  10. const byte COLS = 4;
  11. char hexaKeys[ROWS][COLS] = {
  12.     {'1','2','3','A'},
  13.     {'4','5','6','B'},
  14.     {'7','8','9','C'},
  15.     {'*','0','#','D'}
  16. };
  17. char key = NO_KEY;
  18. byte rowPins[ROWS] = {9, 8, 7, 6};
  19. byte colPins[COLS] = {5, 4, 3, 2};

  20. HX711 HX711_CH0(A0, A1, 400);
  21. LiquidCrystal_I2C lcd(0x27,20,4);

  22. long Weight[5] = {0,0,0,0,0};
  23. long gWeight      = 0;

  24. unsigned char  gWeight_ge   = 0;
  25. unsigned char  gWeight_shi  = 0;
  26. unsigned char  gWeight_bai  = 0;
  27. unsigned char  gWeight_qian = 0;
  28. long gSetWeight      = 0;
  29. long gSetWeight_Out  = 0;
  30. unsigned char  gSetWeight_Flag  = 0;
  31. unsigned char  gSetWeight_Start = 0;
  32. unsigned char  gSetWeight_ge    = 0;
  33. unsigned char  gSetWeight_shi   = 0;
  34. unsigned char  gSetWeight_bai   = 0;
  35. unsigned char  gSetWeight_qian  = 0;

  36. unsigned char gIndex = 0;
  37. static unsigned char Tmr20msFlag = 0;
  38. static unsigned char gMainLoop100msCnt  = 1;  
  39. static unsigned char gMainLoop500msCnt  = 2;
  40. static unsigned char gMainLoop1000msCnt = 3;
  41. static unsigned char gSchedularType     = 0;

  42. Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);

  43. void flash()                        //中断处理函数,改变灯的状态
  44. {                        
  45.   Tmr20msFlag = 1;
  46.   //Serial.println("20ms");
  47. }

  48. void  Task_20ms(void)
  49. {
  50.     Weight[gIndex] = HX711_CH0.Get_Weight();   
  51.     if(Weight[gIndex] < 0 )Weight[gIndex] = 0;
  52.    
  53.     gIndex++;  
  54.     if( gIndex >=  5 ) gIndex = 0;
  55.     //Serial.println("20ms");
  56. }
  57. void  Task_100ms(void)
  58. {
  59.     long lWeight = 0;
  60.     gWeight = (Weight[0] + Weight[1] + Weight[2] + Weight[3] + Weight[4]) / 5;
  61.     if(gSetWeight_Start == 1 )
  62.     {
  63.         if( gSetWeight_Out > gWeight )
  64.         {
  65.              lWeight = gSetWeight_Out - gWeight;
  66.              if(lWeight > 1000 ) Pwm_Motor = 250;
  67.              else if( lWeight > 600 ) Pwm_Motor = lWeight / 5;
  68.              else if( lWeight > 299 ) Pwm_Motor = lWeight / 6;
  69.              else  Pwm_Motor = 40;
  70.              analogWrite(Motor,Pwm_Motor);
  71.         }
  72.         else
  73.         {
  74.               gSetWeight_Start = 0;
  75.               Pwm_Motor = 0;
  76.               analogWrite(Motor,Pwm_Motor);
  77.         }      
  78.     }
  79.     lcd.setCursor(0, 1);
  80.     lcd.print("GetValue: ");
  81.     if( gWeight > 5000)
  82.     {
  83.       lcd.print("ERR ");
  84.     }
  85.     else
  86.     {
  87.         gWeight_ge =    gWeight         % 10;
  88.         gWeight_shi = ( gWeight /  10 ) % 10;
  89.         gWeight_bai = ( gWeight / 100 ) % 10;
  90.         gWeight_qian =  gWeight / 1000;
  91.         lcd.print(gWeight_qian,DEC);      
  92.         lcd.print(gWeight_bai,DEC);
  93.         lcd.print(gWeight_shi,DEC);
  94.         lcd.print(gWeight_ge,DEC);
  95.     }
  96. }
  97. void  Task_500ms(void)
  98. {
  99.   analogWrite(Motor,Pwm_Motor);
  100.   //Serial.println("500ms
  101. }
  102. void  Task_1000ms(void)
  103. {
  104.     //Serial.println("1000ms");
  105. }

  106. void  Task_IDLE(void)
  107. {
  108.   char NewValue = 0;
  109.   char NewValueFlag = 0;
  110.   char SetValueFlag = 0;
  111.    key = customKeypad.getKey();
  112.    if (key != NO_KEY)
  113.    {
  114.        switch( key )
  115.        {
  116.            case '1':  NewValue = 1; NewValueFlag = 1;         break;
  117.            case '2':  NewValue = 2; NewValueFlag = 1;         break;
  118.            case '3':  NewValue = 3; NewValueFlag = 1;         break;
  119.            case '4':  NewValue = 4; NewValueFlag = 1;         break;
  120.            case '5':  NewValue = 5; NewValueFlag = 1;         break;
  121.            case '6':  NewValue = 6; NewValueFlag = 1;         break;
  122.            case '7':  NewValue = 7; NewValueFlag = 1;         break;
  123.            case '8':  NewValue = 8; NewValueFlag = 1;         break;
  124.            case '9':  NewValue = 9; NewValueFlag = 1;         break;
  125.            case '0':  NewValue = 0; NewValueFlag = 1;         break;
  126.            case 'A':            break;
  127.            case 'B':            break;
  128.            case 'C':            break;
  129.            case 'D':            break;
  130.            case '#':   if(gSetWeight_Flag  == 1){ gSetWeight_Start = 1;  gSetWeight_Flag = 0; }         break;
  131.            case '*':    SetValueFlag = 1;   break;
  132.         }
  133.         if( 1 == NewValueFlag )
  134.         {
  135.             gSetWeight = gSetWeight * 10 + NewValue;
  136.             if(gSetWeight > 9999) gSetWeight = gSetWeight % 10000;
  137.             
  138.             gSetWeight_ge =    gSetWeight         % 10;
  139.             gSetWeight_shi = ( gSetWeight /  10 ) % 10;
  140.             gSetWeight_bai = ( gSetWeight / 100 ) % 10;
  141.             gSetWeight_qian =  gSetWeight / 1000;        
  142.             lcd.setCursor(0, 0);
  143.             lcd.print("SetValue: ");
  144.             lcd.print(gSetWeight_qian,DEC);      
  145.             lcd.print(gSetWeight_bai,DEC);
  146.             lcd.print(gSetWeight_shi,DEC);
  147.             lcd.print(gSetWeight_ge,DEC);
  148.         }
  149.         if( 1 == SetValueFlag )
  150.         {
  151.             if(gSetWeight > 5000)
  152.             {
  153.                 lcd.setCursor(0, 0);
  154.                 lcd.print("SetValue: ERR  ");
  155.                 gSetWeight = 0;
  156.             }
  157.             else
  158.             {
  159.                 lcd.setCursor(0, 0);
  160.                 lcd.print("SetValue:     ");
  161.                 delay(500);
  162.                 lcd.setCursor(0, 0);
  163.                 lcd.print("SetValue:");               
  164.                 gSetWeight_ge =    gSetWeight         % 10;
  165.                 gSetWeight_shi = ( gSetWeight /  10 ) % 10;
  166.                 gSetWeight_bai = ( gSetWeight / 100 ) % 10;
  167.                 gSetWeight_qian =  gSetWeight / 1000;        
  168.                 lcd.setCursor(0, 0);
  169.                 lcd.print("SetValue: ");
  170.                 lcd.print(gSetWeight_qian,DEC);      
  171.                 lcd.print(gSetWeight_bai,DEC);
  172.                 lcd.print(gSetWeight_shi,DEC);
  173.                 lcd.print(gSetWeight_ge,DEC);
  174.                 delay(500);
  175.                 lcd.setCursor(0, 0);
  176.                 lcd.print("SetValue:     ");
  177.                 delay(500);
  178.                 lcd.setCursor(0, 0);
  179.                 lcd.print("SetValue:");               
  180.                 gSetWeight_ge =    gSetWeight         % 10;
  181.                 gSetWeight_shi = ( gSetWeight /  10 ) % 10;
  182.                 gSetWeight_bai = ( gSetWeight / 100 ) % 10;
  183.                 gSetWeight_qian =  gSetWeight / 1000;        
  184.                 lcd.setCursor(0, 0);
  185.                 lcd.print("SetValue: ");
  186.                 lcd.print(gSetWeight_qian,DEC);      
  187.                 lcd.print(gSetWeight_bai,DEC);
  188.                 lcd.print(gSetWeight_shi,DEC);
  189.                 lcd.print(gSetWeight_ge,DEC);
  190.                 gSetWeight_Out = gSetWeight;   
  191.                 gSetWeight_Flag = 1;
  192.             }
  193.         }
  194.    }  
  195.    //Serial.println("IDLE");
  196. }

  197. void setup()
  198. {
  199.   pinMode(Motor, OUTPUT);
  200.   analogWrite(Motor,0);
  201.   pinMode(LED, OUTPUT);       
  202.   digitalWrite(LED, HIGH);
  203.    
  204.   Serial.begin(115200);
  205.   //Serial.print("Welcome to use!\n");
  206.   HX711_CH0.begin();
  207.   delay(1500);       
  208.   
  209.   lcd.init();
  210.   lcd.backlight();
  211.   lcd.print("SetValue: XXXX g");
  212.   lcd.setCursor(0, 1);
  213.   lcd.print("GetValue: XXXX g");
  214.   
  215.   delay(1500);
  216.   HX711_CH0.begin();
  217.   digitalWrite(LED, HIGH);
  218.   //Serial.print("Begin to use!\n");
  219.   MsTimer2::set(20, flash);        // 中断设置函数,每 20ms 进入一次中断
  220.   MsTimer2::start();                //开始计时
  221.   Weight[0] = Weight[1] = Weight[2] = Weight[3] = Weight[4] = 0;

  222.   digitalWrite(LED, LOW);
  223. }
  224. void loop()
  225. {
  226.     if(1 == Tmr20msFlag)
  227.     {                  
  228.         Tmr20msFlag = 0;   Task_20ms();
  229.         
  230.         if(gMainLoop100msCnt <= 0)    {   gMainLoop100msCnt = 4;   Task_100ms(); }            
  231.         else                          {   gMainLoop100msCnt--;                   }
  232.         
  233.         if(gMainLoop500msCnt <= 0)    {   gMainLoop500msCnt = 24;  Task_500ms(); }
  234.         else                          {   gMainLoop500msCnt--;                   }
  235.         
  236.         if(gMainLoop1000msCnt <= 0)   {   gMainLoop1000msCnt = 49; Task_1000ms();}
  237.         else                          {   gMainLoop1000msCnt--;                  }
  238.     }
  239.     Task_IDLE();
  240. }
复制代码


所有资料51hei提供下载:
MyMainCode.rar (2.95 KB, 下载次数: 20)


评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏2 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:327071 发表于 2018-5-11 09:22 | 只看该作者
厉害厉害
回复

使用道具 举报

板凳
ID:327071 发表于 2018-5-11 09:23 | 只看该作者
论坛新手,没办法下载了
回复

使用道具 举报

地板
ID:598872 发表于 2019-8-10 16:11 | 只看该作者
很好,有具体的图吗
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表