当前位置:网站首页>STM32 state machine programming example - full automatic washing machine (Part 2)
STM32 state machine programming example - full automatic washing machine (Part 2)
2022-07-26 03:49:00 【Code farmer loves learning】
Last article , Through state machine programming , The logic control of the automatic washing machine is realized , And display each status through serial port printing .
This article , In order to feel the operation of the state machine more intuitively , Use 0.96 " OLED To display various statuses , And match the corresponding dynamic icons to reflect the various states of the washing machine .
Let's see the demonstration effect first :https://www.bilibili.com/video/BV1xT411E7pY

1 OLED Picture shows
In order to be convenient in OLED Display text and pictures on , Some graphic libraries can help us display , What we use here is U8g2 Graphics library .
1.1 U8g2 Library migration
U8g2 Library in STM32 Transplantation on , The previous article has already introduced , For the specific migration process, please refer to this :
After successful transplantation , You can use test routines to verify U8g2 Display effect of Library .

1.2 Picture shows
Compare pictures with words , It can show more rich content , Therefore, this article shows the working state of the washing machine through simple monochrome pictures .
U8g2 The library displays pictures , have access to u8g2_DrawXBM function , You need to convert the image into an array first .
You can use this online web page to convert image data :https://tools.clz.me/image-to-bitmap-array
Here you can use your favorite pictures , To display , For example, I selected the washing machine icon with different water volume to display the current water volume of the washing machine , Use the alternate display of multiple pictures to produce the animation effect of washing machine cleaning .

2 More status outputs
OLED The screen should show the working state of the washing machine , You need to get the specific working state of the state machine . Here are some customized data needed for presentation , Form a structure , The state machine is running , Modify each member variable , then OLED Get these data at the end , Show it again .
typedef struct
{
WASHER_STATUS washerStatus; /* The working state of the washing machine */
int targetWaterLevel; /* Target water level of washing machine */
int targetWashTimes; /* Target cleaning times of washing machine */
int remainingTime; /* The remaining working time of the washing machine ( Not used yet )*/
int curWaterLevel; /* The current water level of the washing machine */
bool hasNewData; /* Is there any new data ( Used to tell OLED Whether to refresh the display )*/
}WASHER_OUTPUT_DATA;
about OLED The display logic of , Here is after each cycle of the state machine , Call the following program logic to show :
void show_washer_status(WASHER_OUTPUT_DATA washerOutPutData)
{
if (washerOutPutData.hasNewData)
{
WASHER_STATUS s = washerOutPutData.washerStatus;
printf("u8g2 get status:%d(%s)\r\n", s, washer_status_name[s]);
switch(s)
{
case WS_INIT: showWasherInit(&u8g2, washerOutPutData); break;
case WS_IDLE: showWasherIdle(&u8g2, washerOutPutData); break;
case WS_ADD_WATER: showWasherAddWater(&u8g2, washerOutPutData); break;
case WS_WASH: showWasherWash(&u8g2, washerOutPutData); break;
case WS_DRAIN_WATER: showWasherDrainWater(&u8g2, washerOutPutData); break;
case WS_SPIN_DRY: showWasherSpinDry(&u8g2, washerOutPutData); break;
case WS_PAUSE: showWasherPause(&u8g2, washerOutPutData); break;
case WS_DONE: showWasherDone(&u8g2, washerOutPutData); break;
default: break;
}
}
}
When new data is generated in this round of state cycle , Then according to the main state of the state machine , Respectively display the pictures or animations in the corresponding state .
For example, adding water , According to the current water level , Constantly update the water level shown in the picture :
void drawCurWaterLevel(u8g2_t *u8g2, int level)
{
switch(level)
{
case 0: u8g2_DrawXBM(u8g2,64, 16, 48, 48, pic_water_0); break;
case 1: u8g2_DrawXBM(u8g2,64, 16, 48, 48, pic_water_1); break;
case 2: u8g2_DrawXBM(u8g2,64, 16, 48, 48, pic_water_2); break;
case 3: u8g2_DrawXBM(u8g2,64, 16, 48, 48, pic_water_3); break;
case 4: u8g2_DrawXBM(u8g2,64, 16, 48, 48, pic_water_4); break;
case 5: u8g2_DrawXBM(u8g2,64, 16, 48, 48, pic_water_5); break;
case 6: u8g2_DrawXBM(u8g2,64, 16, 48, 48, pic_water_6); break;
case 7: u8g2_DrawXBM(u8g2,64, 16, 48, 48, pic_water_7); break;
default: break;
}
}
void showWasherAddWater(u8g2_t *u8g2, WASHER_OUTPUT_DATA data)
{
char strStatus[14] = "AddWater";
u8g2_ClearBuffer(u8g2);
u8g2_SetFont(u8g2,u8g2_font_ncenB10_tr);
u8g2_DrawStr(u8g2,0,15,strStatus);
drawCurWaterLevel(u8g2, data.curWaterLevel);
u8g2_SendBuffer(u8g2);
}
For the structure of the main program , It's the same as the last one , Just added OLED Display of :
int main(void)
{
delay_init(); // Delay function initialization
LED_Init(); // Initialization and LED Connected hardware interface
oled_init();
KEY_Init();
uart_init(115200);
TIM3_Int_Init(500-1,7200-1); // Call the timer to make 50ms Create an interrupt
printf("hello\r\n");
while(1)
{
washer_run_loop();
WASHER_OUTPUT_DATA data = get_washer_output_data();
show_washer_status(data);
delay_ms(100);
}
}
Every time the state machine runs a cycle , Get the specific status data , And then use OLED Show the specific status data .
3 Concrete demonstration
Let's compare this state diagram , Experiment to test the execution of state machine .

- Normal laundry process
Do not consider suspending this state , After the washing machine starts , Experience in turn Free 、 Add water 、 cleaning 、 Drainage 、 spin-dry These processes are over , If the cleaning count is set, I don't know 1 Time , be Add water 、 cleaning 、 Drainage this 3 Each action will be executed by the corresponding number of times .
- Pause and continue in the laundry process
In the running state of the washing machine : Add water 、 cleaning 、 Drainage 、 spin-dry , By the pause button , You can pause the execution of these States , At this time, the status opportunity runs in pause mode , Press again to continue ( Pause / A button to continue ), Will continue to perform laundry work .
- After suspension, modify the water volume or times before continuing
In the washing process , If you want to Modify the amount or times of laundry , You can pause the operation of the washing machine by pressing the pause button , Then press the water level or number button , Switch the state machine from the suspended state to the idle state first , After adjusting the water level or times , Go on , The washing program will continue to run according to the new setting parameters .
For example, the original cleaning water level is 3, The cleaning times are 1, Press pause when adding water for the first cleaning , Then modify the cleaning parameters , For example, the water level is set to 5, The number is set to 2, After continuing , It will enter the state of adding water again , And fill the water level to 5 after , Keep cleaning , And clean 2 End of pass .
notes : This state machine still has room for further optimization , such as :
- Water will only be added , This round of cleaning will not discharge . For example, the first set water level is 5, In addition to 3 Time , Pause and change to 2, After continuing , If it is judged to be greater than the target water level, it will directly start cleaning , It will not be decided by the water level 3 Then drain to the water level 2 Wash again
- Any cleaning state ( Add water 、 cleaning 、 Drainage ) Press pause to adjust the water level , Go on , By default, it will jump to the new cleaning cycle with water , If it is in the drainage state , After adjusting the water level , The water has not been ranked this time , Just add water again and start washing , Not quite reasonable
above 3 Demonstration effect of three test methods , You can compare and watch the demonstration video :
https://www.bilibili.com/video/BV1xT411E7pY
4 summary
This article is based on the state machine programming example of the previous full-automatic washing machine , Added OLED To update the intuitive display of the working state of the washing machine , And pass 3 A test scenario to show the execution of the washing machine working state machine .
边栏推荐
- C language functions (2)
- 中国数据库 OceanBase 入选 Forrester Translytical 数据平台报告
- php中可以使用取绝对值函数 abs() 将负数转成正数
- 5年1.4W倍,NFT OG 的封神之路|Web3专栏
- How to use graffiti magic color product development kit
- 网络模型及协议
- Aike AI frontier promotion (7.18)
- 【程序员必备】七夕表白攻略:”月遇从云,花遇和风,晚上的夜空很美“。(附源码合集)
- Dat of deep learning
- Where can Lora and nb-iot be used
猜你喜欢

One stop monitoring of the software and hardware infrastructure of the whole university, and Suzhou University replaces PostgreSQL with time series database

Visio:甘特图如何合并单元格?解决方案:覆盖单元格

DDD landing is called an advanced

ASEMI整流桥GBU1510参数,GBU1510规格,GBU1510封装

Can't the container run? The Internet doesn't have to carry the blame

PHP connects to MySQL database, and database connects to static tool classes to simplify the connection.

HCIP第十四天

第十八章:2位a~b进制中均位奇观探索,指定整数的 3x+1 转化过程,指定区间验证角谷猜想,探求4份黑洞数,验证3位黑洞数

《opencv学习笔记》-- 边缘检测和canny算子、sobel算子、LapIacian 算子、scharr滤波器

A 4W word redis interview tutorial
随机推荐
Basic line chart: the most intuitive presentation of data trends and changes
leetcode: 102. 二叉树的层序遍历
Portable power fast charging scheme 30W automatic pressure rise and fall PD fast charging
开源许可证的传染性问题浅析
研发了 5 年的时序数据库,到底要解决什么问题?
leetcode-202.快乐数
[mathematical modeling - Summary of planning model] | matlab solution
6-40v input fixed 5V 3.3V output 1.1a current 23-5 package
基于SSM选课信息管理系统
Analysis on the infectious problem of open source license
C语言函数(2)
DDD landing is called an advanced
If you want to do a good job in software testing, you can first understand ast, SCA and penetration testing
电商运营小白,如何快速入门学习数据分析?
涂鸦幻彩产品开发包如何使用
《opencv学习笔记》-- 边缘检测和canny算子、sobel算子、LapIacian 算子、scharr滤波器
Uncaught TypeError: $(...).onmouseenter is not a function js错误,解决办法:
php 实现从1累加到100的算法
MySQL索引失效场景以及解决方案
【虚拟化】查看vCenter和ESXi主机的Log日志文件