Today is mainly to combine the results of the previous few days, receive sensor data through COM6 port, and then send it to Arduino driver board to control the LED light bulb.
Code:
-*- coding: UTF-8 -*- import serial import time #Delay functions need to be used def get_acc(self): #Processing acc information try: axh = int(datahex[6:8],16) axl = int(datahex[4:6], 16) ayh = int(datahex[10:12], 16) ayl = int(datahex[8:10], 16) azh = int(datahex[14:16], 16) azl = int(datahex[12:14], 16) except IOError: print("ReadError: gyro_acc") return (0, 0, 0) else: k_acc = 16 acc_x = (axh << 8 | axl) / 32768 * k_acc acc_y = (ayh << 8 | ayl) / 32768 * k_acc acc_z = (azh << 8 | azl) / 32768 * k_acc if acc_x >= k_acc: acc_x -= 2 * k_acc if acc_y >= k_acc: acc_y -= 2 * k_acc if acc_z >= k_acc: acc_z-= 2 * k_acc return acc_x,acc_y,acc_z def get_gyro(self): #Processing gyro information try: wxh = int(datahex[28:30], 16) wxl = int(datahex[26:28], 16) wyh = int(datahex[32:34], 16) wyl = int(datahex[30:32], 16) wzh = int(datahex[36:38], 16) wzl = int(datahex[34:36], 16) except IOError: print("ReadError: gyro_acc") return (0, 0, 0) else: k_gyro = 2000 gyro_x = (wxh << 8 | wxl) / 32768 * k_gyro gyro_y = (wyh << 8 | wyl) / 32768 * k_gyro gyro_z = (wzh << 8 | wzl) / 32768 * k_gyro if gyro_x >= k_gyro: gyro_x -= 2 * k_gyro if gyro_y >= k_gyro: gyro_y -= 2 * k_gyro if gyro_z >=k_gyro: gyro_z-= 2 * k_gyro return gyro_x,gyro_y,gyro_z def get_angle(self): #Processing angle Information try: rxh = int(datahex[50:52], 16) rxl = int(datahex[48:50], 16) ryh = int(datahex[54:56], 16) ryl = int(datahex[52:54], 16) rzh = int(datahex[58:60], 16) rzl = int(datahex[56:58], 16) except IOError: print("ReadError: gyro_acc") return (0, 0, 0) else: k_angle = 180 angle_x = (rxh << 8 | rxl) / 32768 * k_angle angle_y = (ryh << 8 | ryl) / 32768 * k_angle angle_z = (rzh << 8 | rzl) / 32768 * k_angle if angle_x >= k_angle: angle_x -= 2 * k_angle if angle_y >= k_angle: angle_y -= 2 * k_angle if angle_z >=k_angle: angle_z-= 2 * k_angle return angle_x,angle_y,angle_z def shakecharge(self): #Tremor judgment function ifshake = 0 #Tremor times ifshake_0 =0 #Does it return to zero? limit_a1 = 0.5 limit_a2 = 0.8 limit_a3 = 1.2 #Vibration classification interval_a = 5 #update interval acc_fre[0] = acc_fre[0] + 1 #Important! You can't use variables, you need to use list s. print(acc_fre[0]) if acc_fre[0] % interval_a == 0: #sampling acc_old[0] = acc[0] acc_old[1] = acc[1] acc_old[2] = acc[2] if acc_fre[0] % (interval_a * 10) == 0: #Reset if ifshake == ifshake_0: #If not updated ifshake = ifshake_0 = 0 print('---Reset!') else: #Updating Zero Threshold ifshake_0 = ifshake print('******To update! ') else: pass if acc_fre[0] > interval_a*2 and (abs(acc[0] - acc_old[0]) > limit_a3 or acc[1] - acc_old[1] > limit_a3 or acc[2] - acc_old[2] > limit_a3) : print("===Severe tremor!!!!!") #LED lights demo = b"3" ser1.write(demo) time.sleep(1) ifshake = 0 elif acc_fre[0] > interval_a*2 and (abs(acc[0] - acc_old[0]) > limit_a2 or acc[1] - acc_old[1] > limit_a2 or acc[2] - acc_old[2] > limit_a2): print("===Moderate tremor!!!!!") demo2 = b"2" ser1.write(demo2) time.sleep(0.5) ifshake = 0 elif acc_fre[0] > interval_a*2 and (abs(acc[0] - acc_old[0]) > limit_a1 or acc[1] - acc_old[1] > limit_a1 or acc[2] - acc_old[2] > limit_a1): print("===Mild tremor!!!!!") demo3 = b"1" ser1.write(demo3) time.sleep(0.5) ifshake = 0 else: demo4 = b"0" ser1.write(demo4) return if __name__ == "__main__": #ser = serial.Serial("com6", 115200, timeout=0.5) # open port ser1 = serial.Serial("com7", 9600, timeout=0.5) # open port #acc_old = [0,0,0] acc_old = [0,0,0] acc_fre = [1] print(acc_fre) print(acc_fre[0]) while(ser1.is_open): ser = serial.Serial("com6", 115200, timeout=0.5) # open port #print(ser.is_open) datahex = (ser.read(33).hex()) #Read Sensor Data acc = get_acc(datahex) gyro = get_gyro(datahex) angle = get_angle(datahex) shakecharge(acc) #print(acc_fre) ser.close() time.sleep(0.2) '''print(datahex) # Read a byte print(acc) print(gyro) print(angle)''' #print(acc_x,acc_y,acc_z)
Summary:
1. The basic data types of Python are sorted out:
- Immutable data (3): Number (number), String (string), Tuple (tuple);
- Variable data (3): List (list), Dictionary (dictionary), Set (collection).
Operators: arithmetic operations, assignment operations, comparison operations, logical operations, member operations
Basic data types:
Number-int Boolean-bool class
String - str class (double or single quotation marks) (index, slice, length, traversal deletion, segmentation, blank clearance, case conversion, judgment beginning, etc.) list - list class
List - list class (modifiable, square brackets) (index, slice, append, expand, insert, sort, remove)
tuple class (unmodifiable list, parentheses)
Dictionary-dict class (a series of key-value pairs, key definitions are immutable, and values can be of any data type)
Set - set classes (features: de-duplication, disorder, each element is an immutable type, namely hashable type)
Reference website: http://www.runoob.com/python3/python3-data-type.html python basic data types
2. Solutions to python global variables http://www.javaxxz.com/thread-367807-1-1.html
Usage of 3 time.sleep()
4 List operations (important) http://www.runoob.com/python/python-lists.html
5 LED flicker is not working properly, which is solved after sleep is added, but the reason is unknown and needs to be further solved.
The next step is to manufacture the actuator, Python multi-threading programming, development board transplantation and so on.
Attachment: C Language Processing Source Code
UARTTest.cpp : Define the entry point for console applications. // #include "stdafx.h" #include "Com.h" #include "windows.h" #include "time.h" #include "stdio.h" #include "math.h" unsigned char ucComNo[2] ={0,0}; signed char OpenCom(void) { static unsigned long ulNo=0; signed char cResult= 0; printf("Waiting for insertion Com%d! ",ucComNo[0]); do { cResult = OpenCOMDevice(ucComNo[0],115200); }while(cResult!=0); printf("Com%d Inserted\r\n",ucComNo[0]); return 0; } double a[3],w[3],Angle[3],T; double a_o[3],w_o[3],Angle_o[3],T_o; //Recording Past Sensor Information int a_n = 1; //Recording Acceleration Sampling Number int ifshake= 0; //Judge whether tremor or not int ifshake_0 = 0; //Judging whether to return to zero float limit_a = 0.5; //Deviation threshold of acceleration float limit_a_2 = 0.8; //Deviation threshold of acceleration float limit_a_3 = 1.2; //Deviation threshold of acceleration int interval_a = 5; //Acceleration information update interval void DecodeIMUData(unsigned char chrTemp[]) { //printf("limit_a =%f\n",limit_a); //printf("interval_a = %d\n",interval_a); // printf("a_n =%d \n",a_n); switch(chrTemp[1]) { case 0x51: a_n = a_n+1; a[0] = (short(chrTemp[3]<<8|chrTemp[2]))/32768.0*16; a[1] = (short(chrTemp[5]<<8|chrTemp[4]))/32768.0*16; a[2] = (short(chrTemp[7]<<8|chrTemp[6]))/32768.0*16; T = (short(chrTemp[9]<<8|chrTemp[8]))/340.0+36.25; if ( a_n %(interval_a*10) == 0) // Tremor returns to zero { if (ifshake == ifshake_0) { ifshake_0 = ifshake = 0; printf(" ----------------------------Reset!!!!!"); } else { ifshake_0 = ifshake; printf(" *****************************To update"); Sleep(100); } } if ( a_n %interval_a == 0) // update interval { a_o[0] = a[0]; a_o[1] = a[1]; a_o[2] = a[2]; //printf("a_o = %4.3f\t%4.3f\t%4.3f\t\r\n",a_o[0],a_o[1],a_o[2]); } else { printf("\n"); } //printf("a = %4.3f\t%4.3f\t%4.3f\t\r\n",a[0],a[1],a[2]); if (abs(a[0] - a_o[0]) > limit_a_3 || abs(a[1] - a_o[1]) > limit_a_3 || abs(a[2] - a_o[2]) > limit_a_3) // Judge whether tremor or not //If (abs (a [0] - a_o [0]) > 0.5 | | ABS (a [1] - a_o [1]) > 0.5 | | ABS (a [2] - a_o [2]) > 0.5) / / / Judge whether tremor not { ifshake = ifshake + 1; if(ifshake > 5) //The tremor is judged to be tremor more than five times. { printf(" ===============================================Severe tremor!!!!!===============================================================\n"); Beep(5000,5);//Controlling buzzer sounding ifshake = 0; } else { ifshake = ifshake; } } else if(abs(a[0] - a_o[0]) > limit_a_2 || abs(a[1] - a_o[1]) > limit_a_2 || abs(a[2] - a_o[2]) > limit_a_2) // Judge whether tremor or not //If (abs (a [0] - a_o [0]) > 0.5 | | ABS (a [1] - a_o [1]) > 0.5 | | ABS (a [2] - a_o [2]) > 0.5) / / / Judge whether tremor not { ifshake = ifshake + 1; if(ifshake > 5) //The tremor is judged to be tremor more than five times. { printf(" ===============================================Moderate tremor!!!!!===============================================================\n"); Beep(5000,5);//Controlling buzzer sounding ifshake = 0; } else { ifshake = ifshake; } } else if(abs(a[0] - a_o[0]) > limit_a || abs(a[1] - a_o[1]) > limit_a || abs(a[2] - a_o[2]) > limit_a) // Judge whether tremor or not //If (abs (a [0] - a_o [0]) > 0.5 | | ABS (a [1] - a_o [1]) > 0.5 | | ABS (a [2] - a_o [2]) > 0.5) / / / Judge whether tremor not { ifshake = ifshake + 1; if(ifshake > 5) //The tremor is judged to be tremor more than five times. { printf(" ===============================================Mild tremor!!!!!===============================================================\n"); Beep(5000,5);//Controlling buzzer sounding ifshake = 0; } else { ifshake = ifshake; } } break; case 0x52: w[0] = (short(chrTemp[3]<<8|chrTemp[2]))/32768.0*2000; w[1] = (short(chrTemp[5]<<8|chrTemp[4]))/32768.0*2000; w[2] = (short(chrTemp[7]<<8|chrTemp[6]))/32768.0*2000; T = (short(chrTemp[9]<<8|chrTemp[8]))/340.0+36.25; //printf("w = %4.3f\t%4.3f\t%4.3f\t\r\n",w[0],w[1],w[2]); break; case 0x53: Angle[0] = (short(chrTemp[3]<<8|chrTemp[2]))/32768.0*180; Angle[1] = (short(chrTemp[5]<<8|chrTemp[4]))/32768.0*180; Angle[2] = (short(chrTemp[7]<<8|chrTemp[6]))/32768.0*180; T = (short(chrTemp[9]<<8|chrTemp[8]))/340.0+36.25; //printf("Angle = %4.2f\t%4.2f\t%4.2f\tT=%4.2f\r\n",Angle[0],Angle[1],Angle[2],T); break; } } int _tmain(int argc, _TCHAR* argv[]) { char chrBuffer[1000]; unsigned char chrTemp[1000]; signed char cResult[2] = {0}; unsigned short usLength=0,usRxLength=0; FILE *fp; fp = fopen("Com.ini","r"); if (1!=fscanf(fp,"Com = %d",&ucComNo[0])) { printf("The port configuration file is incorrect!"); Sleep(5000); } fclose(fp); OpenCom(); chrBuffer[0] = 0x01; SendUARTMessageLength(ucComNo[0],chrBuffer,1); while(1) { usLength = CollectUARTData(ucComNo[0],chrBuffer); if (usLength>0) { usRxLength += usLength; while (usRxLength >= 11) { memcpy(chrTemp,chrBuffer,usRxLength); if (!((chrTemp[0] == 0x55) & ((chrTemp[1] == 0x51) | (chrTemp[1] == 0x52) | (chrTemp[1] == 0x53)))) { for (int i = 1; i < usRxLength; i++) chrBuffer[i - 1] = chrBuffer[i]; usRxLength--; continue; } DecodeIMUData(chrTemp); for (int i = 11; i < usRxLength; i++) chrBuffer[i - 11] = chrBuffer[i]; usRxLength -= 11; } } Sleep(200); } return 0; }