闭环步进电机

Dependencies:   mbed

Committer:
heroistired
Date:
Thu Apr 05 01:37:17 2018 +0000
Revision:
1:cbd6a3232d5b
New

Who changed what in which revision?

UserRevisionLine numberNew contents of line
heroistired 1:cbd6a3232d5b 1 #include "GUI.h"
heroistired 1:cbd6a3232d5b 2
heroistired 1:cbd6a3232d5b 3 int screen_buf1[140] = {};
heroistired 1:cbd6a3232d5b 4
heroistired 1:cbd6a3232d5b 5 int screen_buf2[140] = {};
heroistired 1:cbd6a3232d5b 6
heroistired 1:cbd6a3232d5b 7 int index = 0;
heroistired 1:cbd6a3232d5b 8
heroistired 1:cbd6a3232d5b 9 void RefreshGUI(Serial *Screen, int freq, float kp, float ki, int goal, int pos)
heroistired 1:cbd6a3232d5b 10 {
heroistired 1:cbd6a3232d5b 11 unsigned long int long_goal, long_pos;
heroistired 1:cbd6a3232d5b 12 long_goal = goal;
heroistired 1:cbd6a3232d5b 13 long_pos = pos;
heroistired 1:cbd6a3232d5b 14 SXY(Screen,8,35);
heroistired 1:cbd6a3232d5b 15 if(freq < 10)
heroistired 1:cbd6a3232d5b 16 DS16(Screen,90,20,int2string(freq)+" ",3);
heroistired 1:cbd6a3232d5b 17 else
heroistired 1:cbd6a3232d5b 18 DS16(Screen,90,20,int2string(freq)+" ",3);
heroistired 1:cbd6a3232d5b 19 DS16(Screen,90,40,float2string(kp),3);
heroistired 1:cbd6a3232d5b 20 DS16(Screen,90,60,float2string(ki),3);
heroistired 1:cbd6a3232d5b 21 DS16(Screen,90,80,int2string(goal)+" ",3);
heroistired 1:cbd6a3232d5b 22 DS16(Screen,90,100,int2string(pos)+" ",3);
heroistired 1:cbd6a3232d5b 23
heroistired 1:cbd6a3232d5b 24 SXY(Screen,170,75);
heroistired 1:cbd6a3232d5b 25 PS(Screen,index,screen_buf1[index],0);
heroistired 1:cbd6a3232d5b 26 //screen_buf1[index] = 110-long_goal/(16383/110);
heroistired 1:cbd6a3232d5b 27 screen_buf1[index] = 110-goal/(16383/110);
heroistired 1:cbd6a3232d5b 28 PS(Screen,index,screen_buf1[index],3);
heroistired 1:cbd6a3232d5b 29 PS(Screen,index,screen_buf2[index],0);
heroistired 1:cbd6a3232d5b 30 //screen_buf1[index] = 110-long_pos/(16383/110);
heroistired 1:cbd6a3232d5b 31 screen_buf2[index] = 110-pos/(16383/110);
heroistired 1:cbd6a3232d5b 32 PS(Screen,index,screen_buf2[index],5);
heroistired 1:cbd6a3232d5b 33 index++;
heroistired 1:cbd6a3232d5b 34 if(index == 140)
heroistired 1:cbd6a3232d5b 35 index = 0;
heroistired 1:cbd6a3232d5b 36
heroistired 1:cbd6a3232d5b 37
heroistired 1:cbd6a3232d5b 38 SXY(Screen,0,0);
heroistired 1:cbd6a3232d5b 39 ExecuteInstruction(Screen);
heroistired 1:cbd6a3232d5b 40 //wait(0.1);
heroistired 1:cbd6a3232d5b 41 }
heroistired 1:cbd6a3232d5b 42
heroistired 1:cbd6a3232d5b 43 void InitGUI(Serial *Screen)
heroistired 1:cbd6a3232d5b 44 {
heroistired 1:cbd6a3232d5b 45 char c1[18]={0xB1,0xD5,0xBB,0xB7,0xB2,0xBD,0xBD,0xF8,0xB5,0xE7,0xBB,0xFA,0xCA,0xD4,0xD1,0xE9,0xCC,0xA8};//闭环步进电机试验台
heroistired 1:cbd6a3232d5b 46 char c2[8]={0xBF,0xD8,0xD6,0xC6,0xD0,0xC5,0xCF,0xA2};//控制信息
heroistired 1:cbd6a3232d5b 47 char c3[4]={0xC6,0xB5,0xC2,0xCA};//频率
heroistired 1:cbd6a3232d5b 48 char c4[8]={0xB1,0xC8,0xC0,0xFD,0xCF,0xB5,0xCA,0xFD};//比例系数
heroistired 1:cbd6a3232d5b 49 char c5[8]={0xBB,0xFD,0xB7,0xD6,0xCF,0xB5,0xCA,0xFD};//积分系数
heroistired 1:cbd6a3232d5b 50 char c6[8]={0xC4,0xBF,0xB1,0xEA,0xCE,0xBB,0xD6,0xC3};//目标位置
heroistired 1:cbd6a3232d5b 51 char c7[8]={0xB5,0xB1,0xC7,0xB0,0xCE,0xBB,0xD6,0xC3};//当前位置
heroistired 1:cbd6a3232d5b 52 char c8[18] = {0xC7,0xE5,0xBB,0xAA,0xB4,0xF3,0xD1,0xA7,0xB4,0xB4,0xD0,0xC2,0xB9,0xA4,0xD7,0xF7,0xB7,0xBB};//清华大学创新工作坊
heroistired 1:cbd6a3232d5b 53 string str= "";
heroistired 1:cbd6a3232d5b 54
heroistired 1:cbd6a3232d5b 55 SXY(Screen,0,0);
heroistired 1:cbd6a3232d5b 56 CLS(Screen,0);
heroistired 1:cbd6a3232d5b 57 for(int i=0;i<18;i++) str+=c1[i];
heroistired 1:cbd6a3232d5b 58 DS16(Screen,90,4,str,12);
heroistired 1:cbd6a3232d5b 59 str= "";
heroistired 1:cbd6a3232d5b 60 PL(Screen,0,28,319,28,15);
heroistired 1:cbd6a3232d5b 61 PL(Screen,0,29,319,29,15);
heroistired 1:cbd6a3232d5b 62 PL(Screen,159,35,159,190,15);
heroistired 1:cbd6a3232d5b 63 PL(Screen,160,35,160,190,15);
heroistired 1:cbd6a3232d5b 64 SXY(Screen,8,35);
heroistired 1:cbd6a3232d5b 65 for(int i=0;i<8;i++) str+=c2[i];
heroistired 1:cbd6a3232d5b 66 DS16(Screen,30,0,str,2);
heroistired 1:cbd6a3232d5b 67 str= "";
heroistired 1:cbd6a3232d5b 68 for(int i=0;i<4;i++) str+=c3[i];
heroistired 1:cbd6a3232d5b 69 DS16(Screen,0,20,str,2);
heroistired 1:cbd6a3232d5b 70 str= "";
heroistired 1:cbd6a3232d5b 71 for(int i=0;i<8;i++) str+=c4[i];
heroistired 1:cbd6a3232d5b 72 DS16(Screen,0,40,str,2);
heroistired 1:cbd6a3232d5b 73 str= "";
heroistired 1:cbd6a3232d5b 74 for(int i=0;i<8;i++) str+=c5[i];
heroistired 1:cbd6a3232d5b 75 DS16(Screen,0,60,str,2);
heroistired 1:cbd6a3232d5b 76 str= "";
heroistired 1:cbd6a3232d5b 77 for(int i=0;i<8;i++) str+=c6[i];
heroistired 1:cbd6a3232d5b 78 DS16(Screen,0,80,str,2);
heroistired 1:cbd6a3232d5b 79 str= "";
heroistired 1:cbd6a3232d5b 80 for(int i=0;i<8;i++) str+=c7[i];
heroistired 1:cbd6a3232d5b 81 DS16(Screen,0,100,str,2);
heroistired 1:cbd6a3232d5b 82 str= "";
heroistired 1:cbd6a3232d5b 83
heroistired 1:cbd6a3232d5b 84 SXY(Screen,170,35);
heroistired 1:cbd6a3232d5b 85 for(int i=0;i<8;i++) str+=c6[i];
heroistired 1:cbd6a3232d5b 86 DS16(Screen,0,0,str,2);
heroistired 1:cbd6a3232d5b 87 str= "";
heroistired 1:cbd6a3232d5b 88 for(int i=0;i<8;i++) str+=c7[i];
heroistired 1:cbd6a3232d5b 89 DS16(Screen,0,20,str,2);
heroistired 1:cbd6a3232d5b 90 str= "";
heroistired 1:cbd6a3232d5b 91 PL(Screen,80,5,140,5,3);
heroistired 1:cbd6a3232d5b 92 PL(Screen,80,6,140,6,3);
heroistired 1:cbd6a3232d5b 93 PL(Screen,80,25,140,25,5);
heroistired 1:cbd6a3232d5b 94 PL(Screen,80,26,140,26,5);
heroistired 1:cbd6a3232d5b 95 SXY(Screen,0,0);
heroistired 1:cbd6a3232d5b 96 PL(Screen,0,195,319,195,15);
heroistired 1:cbd6a3232d5b 97 PL(Screen,0,196,319,196,15);
heroistired 1:cbd6a3232d5b 98 for(int i=0;i<18;i++) str+=c8[i];
heroistired 1:cbd6a3232d5b 99 DS16(Screen,85,203,str,12);
heroistired 1:cbd6a3232d5b 100 str= "";
heroistired 1:cbd6a3232d5b 101 DS16(Screen,50,222,"Developed by C.H. Dep of EEA",2);
heroistired 1:cbd6a3232d5b 102 SXY(Screen,0,0);
heroistired 1:cbd6a3232d5b 103 ExecuteInstruction(Screen);
heroistired 1:cbd6a3232d5b 104 wait(0.2);
heroistired 1:cbd6a3232d5b 105 }
heroistired 1:cbd6a3232d5b 106
heroistired 1:cbd6a3232d5b 107 void DS16(Serial *Screen, int x, int y, string str, int color)
heroistired 1:cbd6a3232d5b 108 {
heroistired 1:cbd6a3232d5b 109 string temp = "DS16("+int2string(x)+","+int2string(y)+",'"+str+"',"+int2string(color)+");";
heroistired 1:cbd6a3232d5b 110 Screen->printf(temp.c_str());
heroistired 1:cbd6a3232d5b 111 }
heroistired 1:cbd6a3232d5b 112
heroistired 1:cbd6a3232d5b 113 void DS24(Serial *Screen, int x, int y, string str, int color)
heroistired 1:cbd6a3232d5b 114 {
heroistired 1:cbd6a3232d5b 115 string temp = "DS24("+int2string(x)+","+int2string(y)+",'"+str+"',"+int2string(color)+");";
heroistired 1:cbd6a3232d5b 116 Screen->printf(temp.c_str());
heroistired 1:cbd6a3232d5b 117 }
heroistired 1:cbd6a3232d5b 118
heroistired 1:cbd6a3232d5b 119 void SXY(Serial *Screen, int x, int y)
heroistired 1:cbd6a3232d5b 120 {
heroistired 1:cbd6a3232d5b 121 string temp = "SXY("+int2string(x)+","+int2string(y)+");";
heroistired 1:cbd6a3232d5b 122 Screen->printf(temp.c_str());
heroistired 1:cbd6a3232d5b 123 }
heroistired 1:cbd6a3232d5b 124
heroistired 1:cbd6a3232d5b 125 void CLS(Serial *Screen, int color)
heroistired 1:cbd6a3232d5b 126 {
heroistired 1:cbd6a3232d5b 127 string temp = "CLS("+int2string(color)+");";
heroistired 1:cbd6a3232d5b 128 Screen->printf(temp.c_str());
heroistired 1:cbd6a3232d5b 129 }
heroistired 1:cbd6a3232d5b 130
heroistired 1:cbd6a3232d5b 131 void PL(Serial *Screen, int x1, int y1, int x2, int y2, int color)
heroistired 1:cbd6a3232d5b 132 {
heroistired 1:cbd6a3232d5b 133 string temp = "PL("+int2string(x1)+","+int2string(y1)+","+int2string(x2)+","+int2string(y2)+","+int2string(color)+");";
heroistired 1:cbd6a3232d5b 134 Screen->printf(temp.c_str());
heroistired 1:cbd6a3232d5b 135 }
heroistired 1:cbd6a3232d5b 136
heroistired 1:cbd6a3232d5b 137 void PS(Serial *Screen, int x1, int y1, int color)
heroistired 1:cbd6a3232d5b 138 {
heroistired 1:cbd6a3232d5b 139 string temp = "PS("+int2string(x1)+","+int2string(y1)+","+int2string(color)+");";
heroistired 1:cbd6a3232d5b 140 Screen->printf(temp.c_str());
heroistired 1:cbd6a3232d5b 141 }
heroistired 1:cbd6a3232d5b 142
heroistired 1:cbd6a3232d5b 143 void ExecuteInstruction(Serial *Screen)
heroistired 1:cbd6a3232d5b 144 {
heroistired 1:cbd6a3232d5b 145 Screen->printf("\r\n");
heroistired 1:cbd6a3232d5b 146 }
heroistired 1:cbd6a3232d5b 147
heroistired 1:cbd6a3232d5b 148 string int2string(int x)
heroistired 1:cbd6a3232d5b 149 {
heroistired 1:cbd6a3232d5b 150 stringstream ss;
heroistired 1:cbd6a3232d5b 151 ss << x;
heroistired 1:cbd6a3232d5b 152 string str = ss.str();
heroistired 1:cbd6a3232d5b 153 return str;
heroistired 1:cbd6a3232d5b 154 }
heroistired 1:cbd6a3232d5b 155
heroistired 1:cbd6a3232d5b 156 string float2string(float x)
heroistired 1:cbd6a3232d5b 157 {
heroistired 1:cbd6a3232d5b 158 int xx = x * 100;
heroistired 1:cbd6a3232d5b 159 float xxx = (float)xx / 100;
heroistired 1:cbd6a3232d5b 160 stringstream ss;
heroistired 1:cbd6a3232d5b 161 ss << xxx;
heroistired 1:cbd6a3232d5b 162 string str = ss.str() + ' ';
heroistired 1:cbd6a3232d5b 163 return str;
heroistired 1:cbd6a3232d5b 164 }
heroistired 1:cbd6a3232d5b 165
heroistired 1:cbd6a3232d5b 166