Advertisement
Guest User

Untitled

a guest
Nov 20th, 2019
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.62 KB | None | 0 0
  1. // khai bao thu vien
  2.  
  3. #include <wiringPi.h>
  4.  
  5. #include "hx711.h"
  6.  
  7. #include "precisionTiming.h"
  8.  
  9. #include <time.h>
  10.  
  11. #include <unistd.h>
  12.  
  13. #include <stdio.h>
  14.  
  15. #include <string.h>
  16.  
  17. #include <stdint.h>
  18.  
  19. #include <mysql.h>
  20.  
  21. #include <stdlib.h>
  22.  
  23. #include <lcd1602.h>
  24.  
  25. #include <math.h>
  26.  
  27. #define stepPin 17
  28.  
  29. #define dirPin 27
  30.  
  31. #define enaPin 22
  32.  
  33. #define stepperSpeed 2000
  34.  
  35. #define stepperStep 200
  36.  
  37. #define tarePin 21
  38.  
  39. #define feedPin 20
  40.  
  41. #define getDataPin 16
  42.  
  43. #define buzzerPin 12
  44.  
  45. #define sochia -361.0
  46.  
  47. //khai bao database
  48.  
  49. MYSQL *conn;
  50.  
  51. MYSQL_RES *res;
  52.  
  53. MYSQL_ROW row;
  54.  
  55. char *server = "localhost";
  56.  
  57. char *user = "hungdz";
  58.  
  59. char *password = "123465"; /* set me first */
  60.  
  61. char *database = "database";
  62.  
  63. // khai bao bien
  64.  
  65. static int DT_PIN = 19;
  66.  
  67. static int SCK_PIN = 26;
  68.  
  69.  
  70.  
  71. static int readValue = 0;
  72.  
  73. static int _offset = 0;
  74.  
  75. static double _div = 0;
  76.  
  77. static long int dropCount = 0;
  78.  
  79.  
  80.  
  81. static volatile int reading = 0; //Keeps the read data
  82.  
  83.  
  84.  
  85. static precisionTime_t readAverage = 70; //Keeps the average reading time of a single sample
  86.  
  87.  
  88.  
  89. static float getMillisDiff (clock_t t1, clock_t t2);
  90.  
  91. static float getDistFromTime (float timeDiff);
  92.  
  93.  
  94.  
  95. static void edge ();
  96.  
  97. double initdataremove;
  98.  
  99.  
  100.  
  101. int storeid;
  102.  
  103. char tareCounter = 0;
  104.  
  105. char feedCounter = 0;
  106.  
  107. char dataCounter = 0;
  108.  
  109. double docgiatricannang;
  110.  
  111.  
  112.  
  113. // Convert
  114.  
  115. char *my_itoa(int num, char *str)
  116.  
  117. {
  118.  
  119. if(str == NULL)
  120.  
  121. {
  122.  
  123. return NULL;
  124.  
  125. }
  126.  
  127. sprintf(str, "%d", num);
  128.  
  129. return str;
  130.  
  131. }
  132.  
  133. char *my_dtoa(double numread, char *strread)
  134.  
  135. {
  136.  
  137. if(strread == NULL)
  138.  
  139. {
  140.  
  141. return NULL;
  142.  
  143. }
  144.  
  145. sprintf(strread, "%5.2f", numread);
  146.  
  147. return strread;
  148.  
  149. }
  150.  
  151. //Database Func
  152.  
  153. void DataDone(int idofpack)
  154.  
  155. {
  156.  
  157. char cmd[256]={0};
  158.  
  159. printf("%d \n",idofpack);
  160.  
  161. sprintf(cmd,"UPDATE store SET status = 'done', dateofpack=NOW() WHERE id = %d",idofpack);
  162.  
  163. mysql_query(conn, cmd);
  164.  
  165. printf("query okay");
  166.  
  167. mysql_close(conn);
  168.  
  169. }
  170.  
  171. int MysqlGetData()
  172.  
  173. {
  174.  
  175. int storeid;
  176.  
  177. mysql_real_connect(conn, server,user, password, database, 0, NULL, 0);
  178.  
  179. mysql_query(conn, "select id from store where status = 'submitted' order by dateofsubmit asc limit 1");
  180.  
  181. res = mysql_use_result(conn);
  182.  
  183. row = mysql_fetch_row(res);
  184.  
  185. storeid=atoi(row[0]);
  186.  
  187. mysql_free_result(res);
  188.  
  189. return storeid;
  190.  
  191. }
  192.  
  193. // Button Interrupt
  194.  
  195. void tarePressed()
  196.  
  197. {
  198.  
  199. delay(60);
  200.  
  201. if (digitalRead(tarePin)==HIGH)
  202.  
  203. {
  204.  
  205. Buzzer();
  206.  
  207. printf("tare pressed \n");
  208.  
  209. tareCounter++;
  210.  
  211. }
  212.  
  213. }
  214.  
  215. void feedPressed()
  216.  
  217. {
  218.  
  219. delay(60);
  220.  
  221. if (digitalRead(feedPin)==HIGH)
  222.  
  223. {
  224.  
  225. Buzzer();
  226.  
  227. printf("Feed pressed \n");
  228.  
  229. feedCounter++;
  230.  
  231. }
  232.  
  233. }
  234.  
  235. void dataPressed()
  236.  
  237. {
  238.  
  239. delay(60);
  240.  
  241. if (digitalRead(getDataPin)==HIGH)
  242.  
  243. {
  244.  
  245. Buzzer();
  246.  
  247. printf("Data pressed \n");
  248.  
  249. dataCounter++;
  250.  
  251. }
  252.  
  253. }
  254.  
  255. void Buzzer()
  256.  
  257. {
  258.  
  259. digitalWrite(buzzerPin,HIGH);
  260.  
  261. delay(400);
  262.  
  263. digitalWrite(buzzerPin,LOW);
  264.  
  265. }
  266.  
  267. // Stepper Function ----------------------------------------
  268.  
  269. void Stepper_Init()
  270.  
  271. {
  272.  
  273. pinMode(stepPin,OUTPUT);
  274.  
  275. pinMode(enaPin,OUTPUT);
  276.  
  277. pinMode(dirPin,OUTPUT);
  278.  
  279. pinMode(buzzerPin,OUTPUT);
  280.  
  281. digitalWrite(enaPin,HIGH);
  282.  
  283. digitalWrite(dirPin,HIGH);
  284.  
  285. digitalWrite(stepPin,LOW);
  286.  
  287. }
  288.  
  289. void Rotation(int customdelay)
  290.  
  291. {
  292.  
  293. digitalWrite(stepPin,HIGH);
  294.  
  295. delayMicroseconds(customdelay);
  296.  
  297. digitalWrite(stepPin,LOW);
  298.  
  299. delayMicroseconds(customdelay);
  300.  
  301. }
  302.  
  303. void CWOneStepStepper()
  304.  
  305. {
  306.  
  307. digitalWrite(dirPin,HIGH);
  308.  
  309. Rotation(stepperSpeed);
  310.  
  311. }
  312.  
  313. void CW25StepStepper() // 25 steps func = 45 degree rotation
  314.  
  315. {
  316.  
  317. digitalWrite(enaPin,LOW);
  318.  
  319. digitalWrite(dirPin,HIGH);
  320.  
  321. for(int i=0;i<25;i++)
  322.  
  323. {
  324.  
  325. Rotation(stepperSpeed);
  326.  
  327. }
  328.  
  329. digitalWrite(enaPin,HIGH);
  330.  
  331. }
  332.  
  333. // LCD Function -------------------------------------------
  334.  
  335. void lcd_i2c_setup()
  336.  
  337. {
  338.  
  339. int rc;
  340.  
  341. rc = lcd1602Init(1, 0x27);
  342.  
  343. if (rc)
  344.  
  345. {
  346.  
  347. printf("Initialization failed; aborting...\n");
  348.  
  349. }
  350.  
  351. lcd1602SetCursor(0, 0);
  352.  
  353. lcd1602WriteString("Initializing..");
  354.  
  355. delay(100);
  356.  
  357. }
  358.  
  359. // Hx711 Function
  360.  
  361. void tare()
  362.  
  363. {
  364.  
  365. int i, rawReading=0;
  366.  
  367. int sumtare=0;
  368.  
  369. for (i = 0; i < 10; i++)
  370.  
  371. {
  372.  
  373. rawReading=getRawReading();
  374.  
  375. printf ("RAW %d \t", rawReading);
  376.  
  377. if(rawReading<5000)
  378.  
  379. {
  380.  
  381. printf("false data\n");
  382.  
  383. i--;
  384.  
  385. }
  386.  
  387. else
  388.  
  389. {
  390.  
  391. printf ("GOOD \n");
  392.  
  393. sumtare+=rawReading;
  394.  
  395.  
  396.  
  397. }
  398.  
  399. delay(100);
  400.  
  401. }
  402.  
  403. sumtare/=10;
  404.  
  405. printf("Tare RAW %d\n",sumtare);
  406.  
  407. char buffertare[20];
  408.  
  409. my_itoa(sumtare, buffertare);
  410.  
  411. lcd1602Clear();
  412.  
  413. delay(100);
  414.  
  415. lcd1602SetCursor(0, 0);
  416.  
  417. lcd1602WriteString("T:");
  418.  
  419. lcd1602WriteString(buffertare);
  420.  
  421. setupHX711(sumtare, sochia);
  422.  
  423. initdataremove=getReading();
  424.  
  425. printf("tare done\n");
  426.  
  427. }
  428.  
  429.  
  430.  
  431. void initHX711 (int dtPin, int sckPin, int offset, double div)
  432.  
  433. {
  434.  
  435.  
  436.  
  437. DT_PIN = dtPin;
  438.  
  439. SCK_PIN = sckPin;
  440.  
  441. pinMode(DT_PIN, INPUT);
  442.  
  443. pinMode(SCK_PIN, OUTPUT);
  444.  
  445. wiringPiISR (DT_PIN, INT_EDGE_FALLING, edge);
  446.  
  447. setupHX711(offset, div);
  448.  
  449. }
  450.  
  451.  
  452.  
  453. int extendSign (int val)
  454.  
  455. {
  456.  
  457. if ((val & (1<<23))>0)
  458.  
  459. {
  460.  
  461. val += 0xFF<<24;
  462.  
  463. }
  464.  
  465.  
  466.  
  467. return val;
  468.  
  469. }
  470.  
  471.  
  472.  
  473. double getReading ()
  474.  
  475. {
  476.  
  477. int val = extendSign(readValue);
  478.  
  479.  
  480.  
  481. return (val + _offset)/_div;
  482.  
  483. }
  484.  
  485.  
  486.  
  487. int getRawReading ()
  488.  
  489. {
  490.  
  491. return extendSign(readValue);
  492.  
  493. }
  494.  
  495.  
  496.  
  497. long getAverageReadingTime ()
  498.  
  499. {
  500.  
  501. return readAverage;
  502.  
  503. }
  504.  
  505.  
  506.  
  507. long int getDropCount ()
  508.  
  509. {
  510.  
  511. return dropCount;
  512.  
  513. }
  514.  
  515.  
  516.  
  517. // Callback function called at falling edge on DT_PIN
  518.  
  519. static void edge ()
  520.  
  521. {
  522.  
  523. if (!reading && !digitalRead(DT_PIN))
  524.  
  525. {
  526.  
  527. // printf("Reading \n");
  528.  
  529.  
  530.  
  531. precisionTime_t microS = getTimeMicro();
  532.  
  533.  
  534.  
  535. reading = 1;
  536.  
  537.  
  538.  
  539. int read = 0;
  540.  
  541.  
  542.  
  543. int i;
  544.  
  545.  
  546.  
  547. precisionDelayMicro (1);
  548.  
  549.  
  550.  
  551. for (i = 0; i < 24; i++)
  552.  
  553. {
  554.  
  555. digitalWrite(SCK_PIN,HIGH);
  556.  
  557. read <<= 1;
  558.  
  559.  
  560.  
  561.  
  562.  
  563. precisionDelayMicro (1);
  564.  
  565.  
  566.  
  567. digitalWrite(SCK_PIN,LOW);
  568.  
  569.  
  570.  
  571. if (digitalRead(DT_PIN)){
  572.  
  573. #ifdef DEBUG
  574.  
  575. // printf ("%d ", i);
  576.  
  577. #endif
  578.  
  579. read++;}
  580.  
  581. }
  582.  
  583.  
  584.  
  585.  
  586.  
  587.  
  588.  
  589. digitalWrite(SCK_PIN,HIGH);
  590.  
  591. precisionDelayMicro (1);
  592.  
  593. digitalWrite(SCK_PIN,LOW);
  594.  
  595.  
  596.  
  597.  
  598.  
  599. // Calculates the reading time for this sample
  600.  
  601. precisionTime_t readTime = getTimeMicro() - microS;
  602.  
  603.  
  604.  
  605. // Calculates a weighted average through shifts to waste less time
  606.  
  607. // It's like doing (readAverage*7 + readTime)/8
  608.  
  609. // Probably the copiler would do this as well, but for being sure I used shifts
  610.  
  611. readAverage = ((readAverage << 2) + (readAverage << 1) + readAverage + readTime)>>3;
  612.  
  613.  
  614.  
  615. // This sample is valid if its reading time is less than average + 25% of the average
  616.  
  617. int valid = !(readTime > (readAverage + (readAverage>>2)));
  618.  
  619.  
  620.  
  621. #ifdef DEBUG2
  622.  
  623. printf("\nAverage: %lld, Read Time: %lld, valid: %d\n", readAverage, readTime, valid);
  624.  
  625. #endif
  626.  
  627.  
  628.  
  629.  
  630.  
  631. if (valid)
  632.  
  633. {
  634.  
  635. readValue = read;
  636.  
  637. }
  638.  
  639. else
  640.  
  641. {
  642.  
  643. dropCount++;
  644.  
  645. #ifdef DEBUGDROP
  646.  
  647. printf("DROP: %d\n", read);
  648.  
  649. #endif
  650.  
  651. }
  652.  
  653.  
  654.  
  655.  
  656.  
  657. reading = 0;
  658.  
  659. }
  660.  
  661.  
  662.  
  663.  
  664.  
  665. }
  666.  
  667. void setupHX711 (int offset, double div)
  668.  
  669. {
  670.  
  671. _offset = offset;
  672.  
  673. _div = div;
  674.  
  675. }
  676.  
  677. void feed (void)
  678.  
  679. {
  680.  
  681. int feedreading=0;
  682.  
  683. do
  684.  
  685. {
  686.  
  687. CW25StepStepper();
  688.  
  689. lcd1602SetCursor(10, 0);
  690.  
  691. lcd1602WriteString(" ");
  692.  
  693. delay(300);
  694.  
  695. docgiatricannang=getReading();
  696.  
  697. feedreading=docgiatricannang-initdataremove;
  698.  
  699. printf("reading %d\n",feedreading);
  700.  
  701. lcd1602SetCursor(10, 0);
  702.  
  703. lcd1602WriteString("W:");
  704.  
  705. char bufferfeed[20];
  706.  
  707. my_itoa(feedreading,bufferfeed);
  708.  
  709. lcd1602WriteString(bufferfeed);
  710.  
  711. }
  712.  
  713. while(feedreading<1000);
  714.  
  715. lcd1602SetCursor(0, 0);
  716.  
  717. lcd1602WriteString(" ");
  718.  
  719. lcd1602SetCursor(0, 0);
  720.  
  721. lcd1602WriteString("Done");
  722.  
  723. printf("Feed done\n");
  724.  
  725. }
  726.  
  727. // Main
  728.  
  729. int main (void)
  730.  
  731. {
  732.  
  733. wiringPiSetupGpio();
  734.  
  735. conn = mysql_init(NULL);
  736.  
  737. Stepper_Init();
  738.  
  739. lcd_i2c_setup();
  740.  
  741. initHX711(19, 26, 10000, sochia);
  742.  
  743. tare();
  744.  
  745. wiringPiISR(tarePin, INT_EDGE_RISING, &tarePressed);
  746.  
  747. wiringPiISR(feedPin, INT_EDGE_RISING, &feedPressed);
  748.  
  749. wiringPiISR(getDataPin, INT_EDGE_RISING, &dataPressed);
  750.  
  751.  
  752.  
  753. while(1)
  754.  
  755. {
  756.  
  757. if(tareCounter>1)
  758.  
  759. {
  760.  
  761. lcd1602SetCursor(0, 1);
  762.  
  763. lcd1602WriteString(" ");
  764.  
  765. lcd1602SetCursor(0, 1);
  766.  
  767. lcd1602WriteString("SETTING ZEROS...!");
  768.  
  769. tare();
  770.  
  771. tareCounter = 0;
  772.  
  773. }
  774.  
  775. if(dataCounter>1)
  776.  
  777. {
  778.  
  779. int processingid;
  780.  
  781. processingid=MysqlGetData();
  782.  
  783. printf("%d \n",processingid);
  784.  
  785. char processingidconverted[20];
  786.  
  787. my_itoa(processingid,processingidconverted);
  788.  
  789. lcd1602SetCursor(0, 0);
  790.  
  791. lcd1602WriteString(" ");
  792.  
  793. lcd1602SetCursor(0, 1);
  794.  
  795. lcd1602WriteString(" ");
  796.  
  797. lcd1602SetCursor(0, 0);
  798.  
  799. lcd1602WriteString("Feed Mode! ID:");
  800.  
  801. lcd1602WriteString(processingidconverted);
  802.  
  803. lcd1602SetCursor(0, 1);
  804.  
  805. lcd1602WriteString("Pr feed to continue");
  806.  
  807. printf("press feed button to continue");
  808.  
  809. while(feedCounter<1) {
  810.  
  811. delay(1000);
  812.  
  813. }
  814.  
  815. lcd1602SetCursor(0, 1);
  816.  
  817. lcd1602WriteString(" ");
  818.  
  819. lcd1602SetCursor(0, 1);
  820.  
  821. lcd1602WriteString("FEEDING...!");
  822.  
  823. feed();
  824.  
  825. DataDone(processingid);
  826.  
  827. printf("okay\n");
  828.  
  829. feedCounter=0;
  830.  
  831. dataCounter=0;
  832.  
  833. lcd1602SetCursor(0, 0);
  834.  
  835. lcd1602WriteString(" ");
  836.  
  837. lcd1602SetCursor(0, 0);
  838.  
  839. lcd1602WriteString("Scale Mode");
  840.  
  841.  
  842.  
  843. }
  844.  
  845. docgiatricannang=getReading();
  846.  
  847. docgiatricannang-=initdataremove;
  848.  
  849. printf("reading values: %5.2f, rawReading values %d\n",docgiatricannang,getRawReading());
  850.  
  851. delay(1000);
  852.  
  853. char getReadingMain[20];
  854.  
  855. my_dtoa(docgiatricannang,getReadingMain);
  856.  
  857. lcd1602SetCursor(0,1);
  858.  
  859. lcd1602WriteString(" ");
  860.  
  861. lcd1602SetCursor(0,1);
  862.  
  863. lcd1602WriteString(getReadingMain);
  864.  
  865. }
  866.  
  867. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement