MTM PPO mbed cz2

Dependencies:   LCD_DISCO_F429ZI mbed BSP_DISCO_F429ZI

Files at this revision

API Documentation at this revision

Comitter:
paweler
Date:
Mon Jun 15 14:47:01 2020 +0000
Commit message:
mbed cz2

Changed in this revision

BSP_DISCO_F429ZI.lib Show annotated file Show diff for this revision Revisions of this file
LCD_DISCO_F429ZI.lib Show annotated file Show diff for this revision Revisions of this file
command_decoder.cpp Show annotated file Show diff for this revision Revisions of this file
command_decoder.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
servo.cpp Show annotated file Show diff for this revision Revisions of this file
servo.h Show annotated file Show diff for this revision Revisions of this file
servo_gui.cpp Show annotated file Show diff for this revision Revisions of this file
servo_gui.h Show annotated file Show diff for this revision Revisions of this file
uart.cpp Show annotated file Show diff for this revision Revisions of this file
uart.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/BSP_DISCO_F429ZI.lib	Mon Jun 15 14:47:01 2020 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/teams/ST/code/BSP_DISCO_F429ZI/#53d9067a4feb
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LCD_DISCO_F429ZI.lib	Mon Jun 15 14:47:01 2020 +0000
@@ -0,0 +1,1 @@
+https://developer.mbed.org/teams/ST/code/LCD_DISCO_F429ZI/#dc55a068bc1a
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/command_decoder.cpp	Mon Jun 15 14:47:01 2020 +0000
@@ -0,0 +1,90 @@
+#include "command_decoder.h"
+#include <string.h>
+#include <algorithm>
+#include <stdio.h>
+
+#define NULL 0
+
+struct Keyword asKeywordList[MAX_KEYWORD_NR]=
+{
+    {ID, "id"},
+    {CLB, "callib"},
+    {GOTO, "goto"},
+    {STEP, "step"}
+};
+
+enum Result{OK, ERROR};
+
+unsigned char Decoder::ucFindTokensInString(char *pcString)
+{
+    unsigned char ucTokenPointer;
+    unsigned char ucTokenCounter = 0;
+    char cCurrentChar;
+    enum State {TOKEN, DELIMITER};
+    enum State eState = DELIMITER;
+
+    for(ucTokenPointer=0 ;; ucTokenPointer++) {
+        cCurrentChar = pcString[ucTokenPointer];
+        switch(eState) {
+            case DELIMITER:
+                if(cCurrentChar == NULL) {
+                    return ucTokenCounter;
+                } else if(cCurrentChar != ' ') {
+                    eState = TOKEN;
+                    asToken[ucTokenCounter].uValue.pcString = pcString + ucTokenPointer;
+                    ucTokenCounter++;
+                }
+                break;
+            case TOKEN:
+                if(cCurrentChar == NULL) {
+                    return ucTokenCounter;
+                } else if(ucTokenCounter == MAX_TOKEN_NR) {
+                    return ucTokenCounter;
+                } else if(cCurrentChar == ' ') {
+                    eState = DELIMITER;
+                }
+                break;
+        }
+    }
+}
+
+enum Result Decoder::eStringToKeyword(char pcStr[],enum KeywordCode *peKeywordCode)
+{
+    unsigned char ucKeywordIndex;
+
+    for(ucKeywordIndex=0 ; ucKeywordIndex < MAX_KEYWORD_NR ; ucKeywordIndex++) {
+        if(strcmp(pcStr, asKeywordList[ucKeywordIndex].cString) == 0) {
+            *peKeywordCode = asKeywordList[ucKeywordIndex].eCode;
+            return OK;
+        }
+    }
+    return ERROR;
+}
+
+void Decoder::DecodeTokens(void)
+{
+    unsigned char ucCurrentToken;
+    unsigned int uiHexValue;
+    enum KeywordCode Keyword;
+    struct Token* pasValue;
+
+    for(ucCurrentToken=0 ; ucCurrentToken < ucTokenNr ; ucCurrentToken++) {
+        pasValue = &asToken[ucCurrentToken];
+        if(eStringToKeyword(pasValue->uValue.pcString, &Keyword) == OK) {
+            pasValue->eType = KEYWORD;
+            pasValue->uValue.eKeyword = Keyword;
+        } else if(sscanf(pasValue->uValue.pcString, "%4x", &uiHexValue) == 1) {
+            pasValue->eType = NUMBER;
+            pasValue->uValue.uiNumber = uiHexValue;
+        } else {
+            pasValue->eType = STRING;
+        }
+    }
+}
+
+void Decoder::DecodeMsg(char *pcString)
+{
+    ucTokenNr=ucFindTokensInString(pcString);
+    std::replace(pcString, pcString + strlen(pcString), ' ', '\0');
+    DecodeTokens();
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/command_decoder.h	Mon Jun 15 14:47:01 2020 +0000
@@ -0,0 +1,46 @@
+#ifndef COMMAND_DECODER_H
+#define COMMAND_DECODER_H
+
+#define MAX_KEYWORD_STRING_LTH 10
+#define MAX_KEYWORD_NR 4
+#define MAX_TOKEN_NR 3
+
+enum KeywordCode {ID, CLB, GOTO, STEP};
+enum TokenType { KEYWORD, NUMBER, STRING};
+
+union TokenValue
+{
+    enum KeywordCode eKeyword; // jezeli KEYWORD
+    unsigned int uiNumber; // jezeli NUMBER
+    char *pcString; // jezeli STRING
+};
+
+struct Token
+{
+    enum TokenType eType; // KEYWORD, NUMBER, STRING
+    union TokenValue uValue; // enum, unsigned int, char*
+};
+
+struct Keyword
+{
+    enum KeywordCode eCode;
+    char cString[MAX_KEYWORD_STRING_LTH + 1];
+};
+
+class Decoder
+{
+    public:
+        void DecodeMsg(char *pcString);
+        
+        struct Token asToken[MAX_TOKEN_NR];
+        unsigned char ucTokenNr;
+    private:
+        unsigned char ucFindTokensInString(char *pcString);
+        enum Result eStringToKeyword(char pcStr[],enum KeywordCode *peKeywordCode);
+        void DecodeTokens(void);
+        
+        union TokenValue uTokenValue;
+        struct Keyword sKeyword;
+        
+};
+#endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Mon Jun 15 14:47:01 2020 +0000
@@ -0,0 +1,43 @@
+#include "servo.h"
+#include "command_decoder.h"
+#include "uart.h"
+
+Servo MyServo(0.01);
+Decoder MyDecoder;
+
+int main()
+{
+    char cCommand[10];
+    while(1)
+    {
+        gets(cCommand, sizeof(cCommand));
+        MyDecoder.DecodeMsg(cCommand);
+        if((MyDecoder.ucTokenNr > 0)&&(MyDecoder.asToken[0].eType == KEYWORD)) {
+            switch(MyDecoder.asToken[0].uValue.eKeyword) {
+                case ID:
+                    puts("Device ID: 0x01\n", sizeof("Device ID: 0x01\n"));
+                    break;
+                case CLB:
+                    MyServo.Callib();
+                    puts("OK\n", sizeof("OK\n"));
+                    break;
+                case GOTO:
+                    if(MyDecoder.asToken[1].eType == NUMBER) {
+                        MyServo.GoTo(MyDecoder.asToken[1].uValue.uiNumber);
+                        puts("OK\n", sizeof("OK\n"));
+                    }
+                    break;
+                case STEP:
+                    if(MyDecoder.asToken[1].eType == NUMBER) {
+                        MyServo.Step(MyDecoder.asToken[1].uValue.uiNumber);
+                        puts("OK\n", sizeof("OK\n"));
+                    }
+                    break;
+                default:
+                    break;
+            }
+        } else {
+            puts("unknowncommand\n", sizeof("unknowncommand\n"));
+        }
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Mon Jun 15 14:47:01 2020 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/mbed_official/code/mbed/builds/65be27845400
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/servo.cpp	Mon Jun 15 14:47:01 2020 +0000
@@ -0,0 +1,68 @@
+#include "servo.h"
+
+enum DetectorState Servo::eReadDetector()
+{
+    if(MyGui.ucReadDetector()==1)
+    {
+        return ACTIVE;
+    }
+    else
+    {
+        return INACTIVE;
+    }
+}
+
+Servo::Servo(float fStepDelay)
+{   
+    fDelay = fStepDelay;
+    Callib();
+}
+
+void Servo::Callib(void)
+{
+    uiDesiredPosition = 0;
+    uiCurrentPosition = 0;
+    eState = CALLIB;
+    while(eState != IDLE)
+    {
+        if(eReadDetector() == INACTIVE) {
+            MyGui.GuiStepLeft();
+        } else {
+            eState = IDLE;
+        }
+        wait(fDelay);
+    }
+}
+
+void Servo::GoTo(unsigned int uiPosition)
+{
+    uiDesiredPosition = uiPosition;
+    eState = IN_PROGRESS;
+    while(eState != IDLE){
+        if(uiCurrentPosition > uiDesiredPosition) {
+            uiCurrentPosition--;
+            MyGui.GuiStepLeft();
+        } else if(uiCurrentPosition < uiDesiredPosition) {
+            uiCurrentPosition++;
+            MyGui.GuiStepRight();
+        } else if(uiCurrentPosition == uiDesiredPosition) {
+            eState = IDLE;
+        }
+        wait(fDelay);
+    }
+}
+
+void Servo::Step(unsigned int uiSteps)
+{
+    uiDesiredPosition = uiCurrentPosition + uiSteps;
+    eState = IN_PROGRESS;
+    while(eState != IDLE){
+        if(uiCurrentPosition < uiDesiredPosition) {
+            uiCurrentPosition++;
+            MyGui.GuiStepRight();
+        } else if(uiCurrentPosition == uiDesiredPosition) {
+            eState = IDLE;
+        }
+        wait(fDelay);
+    }
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/servo.h	Mon Jun 15 14:47:01 2020 +0000
@@ -0,0 +1,29 @@
+#ifndef SERVO_H
+#define SERVO_H
+
+#include "servo_gui.h"
+
+enum DetectorState {ACTIVE,INACTIVE};
+enum ServoState {CALLIB, IDLE, IN_PROGRESS};
+
+//void DetectorInit(void);
+
+//void ServoInit(unsigned int uiServoFrequency);
+
+class Servo
+{
+    public:
+        Servo(float = 0.05);
+        enum DetectorState eReadDetector(void);
+        void Callib(void);
+        void GoTo(unsigned int);
+        void Step(unsigned int);
+    private:
+        enum ServoState eState;
+        unsigned int uiCurrentPosition;
+        unsigned int uiDesiredPosition;
+        float fDelay;
+        ServoGui MyGui;
+};
+
+#endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/servo_gui.cpp	Mon Jun 15 14:47:01 2020 +0000
@@ -0,0 +1,74 @@
+#include "servo_gui.h"
+#define PI 3.1415
+
+ServoGui::ServoGui(void)
+{
+    lcd.Clear(LCD_COLOR_WHITE);
+    lcd.SetTextColor(LCD_COLOR_BLACK);
+    lcd.FillCircle(120, 120, 98);
+    lcd.DrawCircle(30, 290, 20);
+    lcd.DrawCircle(90, 290, 20);
+    lcd.DrawCircle(150, 290, 20);
+    lcd.DrawCircle(210, 290, 20);
+    lcd.SetTextColor(LCD_COLOR_WHITE);
+    lcd.FillCircle(120, 120, 20);
+    lcd.DrawLine(120, 120, 120+98, 120);
+}
+
+void ServoGui::LedOn(unsigned char ucLedIndex)
+{
+    lcd.SetTextColor(LCD_COLOR_WHITE);
+    for(unsigned char ucLedCounter = 0; ucLedCounter < 4; ucLedCounter++) {
+        lcd.FillCircle(30+ucLedCounter*60, 290, 19);
+    }
+    lcd.SetTextColor(LCD_COLOR_BLUE);
+    lcd.FillCircle(30+ucLedIndex*60, 290, 19);
+}
+
+void ServoGui::LedStep(enum StepDirection eDirection)
+{
+    if(eDirection == LEFT) {
+        ucLedCtr--;
+    } else if(eDirection == RIGHT) {
+        ucLedCtr++;
+    }
+    LedOn(ucLedCtr%4);
+}
+
+void ServoGui::MotorStep(enum StepDirection eDirection)
+{
+    if(eDirection == LEFT) {
+        uiStepCounter--;
+    } else if(eDirection == RIGHT) {
+        uiStepCounter++;
+    }
+    dAngle = uiStepCounter % 360;
+    lcd.SetTextColor(LCD_COLOR_BLACK);
+    lcd.FillCircle(120, 120, 98);
+    lcd.SetTextColor(LCD_COLOR_WHITE);
+    lcd.DrawLine(120,120,120+98*cos(dAngle*PI/180), 120+98*sin(dAngle*PI/180));
+    lcd.FillCircle(120, 120, 20);
+}
+
+void ServoGui::GuiStepLeft(void)
+{
+    LedStep(LEFT);
+    MotorStep(LEFT);
+    //wait(0.04);
+}
+
+void ServoGui::GuiStepRight(void)
+{
+    LedStep(RIGHT);
+    MotorStep(RIGHT);
+    //wait(0.04);
+}
+
+unsigned char ServoGui::ucReadDetector(void)
+{
+    if(dAngle == 0) {
+        return 1;
+    } else {
+        return 0;
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/servo_gui.h	Mon Jun 15 14:47:01 2020 +0000
@@ -0,0 +1,25 @@
+#ifndef SERVO_GUI_H
+#define SERVO_GUI_H
+
+#include "LCD_DISCO_F429ZI.h"
+
+enum StepDirection {RIGHT, LEFT};
+
+class ServoGui
+{
+    public:
+        ServoGui(void);
+        void GuiStepLeft(void);
+        void GuiStepRight(void);
+        unsigned char ucReadDetector(void);
+    private:
+        void LedOn(unsigned char);
+        void LedStep(enum StepDirection);
+        void MotorStep(enum StepDirection);
+        LCD_DISCO_F429ZI lcd;
+        unsigned char ucLedCtr;
+        unsigned int uiStepCounter;
+        double dAngle;
+};
+
+#endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/uart.cpp	Mon Jun 15 14:47:01 2020 +0000
@@ -0,0 +1,34 @@
+#include "uart.h"
+#include "mbed.h"
+
+Serial pc(USBTX, USBRX);
+
+int puts(char *cBuffer, unsigned char ucBufferSize)
+{
+    for(unsigned char ucCharCounter = 0; cBuffer[ucCharCounter] != NULL; ucCharCounter++)
+    {
+        if(ucCharCounter == ucBufferSize)
+        {
+            return 1;
+        }
+    }
+    for(unsigned char ucCharCounter = 0; cBuffer[ucCharCounter] != NULL; ucCharCounter++)
+    {
+        pc.putc(cBuffer[ucCharCounter]);
+    }
+    pc.putc(0x0D);    //CR
+    return 0;
+}
+
+int gets(char *cBuffer, unsigned char ucBufferSize)
+{
+    for(unsigned char ucCharCounter = 0; ucCharCounter < ucBufferSize; ucCharCounter++)
+    {
+        cBuffer[ucCharCounter] = pc.getc();
+        if(cBuffer[ucCharCounter]==0x0D){
+            cBuffer[ucCharCounter] = NULL;
+            return 0;
+        }
+    }
+    return 1;
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/uart.h	Mon Jun 15 14:47:01 2020 +0000
@@ -0,0 +1,3 @@
+
+int puts(char *cBuffer, unsigned char ucBufferSize);
+int gets(char *cBuffer, unsigned char ucBufferSize);