1
0

5 Commits 18f1d019d9 ... 4edb249252

Autor SHA1 Nachricht Datum
  Andrey Koryagin 4edb249252 version 0.1 binary only vor 1 Jahr
  Andrey Koryagin 18f1d019d9 version 0.1 binary only vor 1 Jahr
  Andrey Koryagin 1a5cd54b13 version 0.1 binary only vor 1 Jahr
  Andrey Koryagin 6bea19443c version 0.1 binary only vor 1 Jahr
  Andrey Koryagin 24abd09c65 version 0.1 binary only vor 1 Jahr

+ 21 - 0
Hardware/Boards/stm32f103c8t6/examples/example_base/Inc/user.h

@@ -0,0 +1,21 @@
+/*
+ * user.h
+ *
+ *    Author: Andrey Koryagin https://blog.avislab.com/
+ */
+
+#ifndef INC_USER_H_
+#define INC_USER_H_
+
+void USER_Init(void);
+void USER_While(void);
+void USER_Tasks(void);
+
+
+//#define USER_PROTOCOL_ENABLE
+
+#ifdef USER_PROTOCOL_ENABLE
+uint8_t USER_Packet_Handling(void);
+#endif
+
+#endif /* INC_USER_H_ */

+ 37 - 0
Hardware/Boards/stm32f103c8t6/examples/example_base/Src/user.c

@@ -0,0 +1,37 @@
+/*
+ * user.c
+ *
+ *  Created on: 2 июн. 2023 г.
+ *    Author: Andrey Koryagin https://blog.avislab.com/
+ */
+
+#include "u_foc.h"
+#include "position.h"
+
+extern volatile SettingsStruct Settings;
+
+void USER_Init(void) {
+
+}
+
+void USER_While(void) {
+	HAL_Delay(10);
+}
+
+void USER_Tasks(void) {
+
+}
+
+
+#ifdef USER_PROTOCOL_ENABLE
+
+extern uint8_t PACKET_REQUEST[PACKET_LENGTH];
+extern uint8_t PACKET_RESPONSE[PACKET_LENGTH];
+
+uint8_t USER_Packet_Handling(void) {
+
+
+	return 0;
+}
+
+#endif

+ 16 - 0
Hardware/Boards/stm32f103c8t6/examples/example_position1/Inc/position.h

@@ -0,0 +1,16 @@
+/*
+ * position.h
+ *
+ *    Author: Andrey Koryagin https://blog.avislab.com/
+ */
+
+#ifndef INC_POSITION_H_
+#define INC_POSITION_H_
+
+void Position_Init (void);
+void Position_Process (void);
+void Position_Set (uint16_t position);
+uint16_t Position_Get ();
+uint16_t Position_Get_Real ();
+
+#endif /* INC_POSITION_H_ */

+ 21 - 0
Hardware/Boards/stm32f103c8t6/examples/example_position1/Inc/user.h

@@ -0,0 +1,21 @@
+/*
+ * user.h
+ *
+ *    Author: Andrey Koryagin https://blog.avislab.com/
+ */
+
+#ifndef INC_USER_H_
+#define INC_USER_H_
+
+void USER_Init(void);
+void USER_While(void);
+void USER_Tasks(void);
+
+
+#define USER_PROTOCOL_ENABLE
+
+#ifdef USER_PROTOCOL_ENABLE
+uint8_t USER_Packet_Handling(void);
+#endif
+
+#endif /* INC_USER_H_ */

+ 73 - 0
Hardware/Boards/stm32f103c8t6/examples/example_position1/Src/position.c

@@ -0,0 +1,73 @@
+/*
+ * position.c
+ *
+ *    Author: Andrey Koryagin https://blog.avislab.com/
+ */
+
+#include "u_foc.h"
+
+#define POSITION_MAX 4095
+
+extern volatile SettingsStruct Settings;
+
+PIDstruct PositionPID;
+
+int32_t POSITION_TASK = 0;
+
+int32_t Position_Calc () {
+	int32_t pos;
+	pos = (int32_t)MC_ADC_GetRegularValue(ADC_POT) - POSITION_TASK;
+	//pos = POSITION_TASK - (int32_t)MC_ADC_GetRegularValue(ADC_POT);
+	if (pos > POSITION_MAX/2) {
+		pos = pos - POSITION_MAX;
+	}
+	if (pos < -POSITION_MAX/2) {
+		pos = POSITION_MAX + pos;
+	}
+	return pos;
+}
+
+void Position_Init () {
+	PositionPID.kp = 50000;
+	PositionPID.ki = 50000;
+	PositionPID.kd = 0;
+	PositionPID.max = Settings.TorqueMax;
+	PositionPID.min = -Settings.TorqueMax;
+	//PositionPID.max = Settings.RpmMax;
+	//PositionPID.min = -Settings.RpmMax;
+
+	PositionPID.task = 0;
+	PositionPID.input = Position_Calc();
+
+	MC_SetMode(MC_MODE_TORQUE);
+	MC_SetTorque(0);
+}
+
+void Position_Process () {
+	PositionPID.input = Position_Calc();
+	/*
+	int16_t speed = -(int16_t)PI_Compute(&PositionPID);
+	if ((-200 < speed) & (speed < 200) ) {
+		speed=0;
+	}
+	*/
+	//MC_SetSpeed(speed);
+	int16_t torque = -(int16_t)PI_Compute(&PositionPID);
+	MC_SetTorque(torque);
+}
+
+void Position_Set (uint16_t position) {
+	if (position > POSITION_MAX) {
+		position = POSITION_MAX;
+	}
+
+	POSITION_TASK = (int32_t)position;
+}
+
+uint16_t Position_Get () {
+	return (uint16_t)POSITION_TASK;
+}
+
+uint16_t Position_Get_Real () {
+	return (uint16_t) MC_ADC_GetRegularValue(ADC_POT);
+}

+ 79 - 0
Hardware/Boards/stm32f103c8t6/examples/example_position1/Src/user.c

@@ -0,0 +1,79 @@
+/*
+ * user.c
+ *
+ *  Created on: 2 июн. 2023 г.
+ *    Author: Andrey Koryagin https://blog.avislab.com/
+ */
+
+#include "u_foc.h"
+#include "position.h"
+
+extern volatile SettingsStruct Settings;
+
+void USER_Init(void) {
+	MC_ProtocolTimeoutDisable(); // Disable motor stopping on U-FOC Protocol timeout
+	Position_Init();
+}
+
+void USER_While(void) {
+	HAL_Delay(10);
+
+	if (MC_GetState() == MC_STATE_IDLE) {
+		MC_Start();
+	}
+
+	if (MC_GetState() == MC_STATE_FAULT) {
+		MC_ResetFault();
+	}
+}
+
+void USER_Tasks(void) {
+	Position_Process();
+}
+
+
+
+
+
+
+#ifdef USER_PROTOCOL_ENABLE
+
+#define USER_PACKET_SET_POSITION	60
+#define USER_PACKET_GET_POSITION	61
+
+extern uint8_t PACKET_REQUEST[PACKET_LENGTH];
+extern uint8_t PACKET_RESPONSE[PACKET_LENGTH];
+
+uint8_t USER_Packet_Handling(void) {
+
+	if (PACKET_REQUEST[1] == USER_PACKET_SET_POSITION) { // Set Position
+		int16_t position;
+		position = (PACKET_REQUEST[2]<<8) | PACKET_REQUEST[3];
+		Position_Set(position);
+		return 0;
+	}
+
+	if (PACKET_REQUEST[1] == USER_PACKET_GET_POSITION) { // Get Position
+		PACKET_RESPONSE[0]= Settings.DevID;
+		PACKET_RESPONSE[1]= PACKET_REQUEST[1] + 127;
+
+		uint16_t position;
+		position = Position_Get_Real();
+		PACKET_RESPONSE[2]= ((position & 0x0000FF00) >> 8);
+		PACKET_RESPONSE[3]= (position & 0x000000FF);
+		position = Position_Get();
+		PACKET_RESPONSE[4]= ((position & 0x0000FF00) >> 8);
+		PACKET_RESPONSE[5]= (position & 0x000000FF);
+		PACKET_RESPONSE[6]= 0;
+		PACKET_RESPONSE[7]= 0;
+		return 1;
+	}
+
+	return 0;
+}
+
+#endif
+
+
+
+

+ 16 - 0
Hardware/Boards/stm32f103c8t6/examples/example_position2/Inc/position.h

@@ -0,0 +1,16 @@
+/*
+ * position.h
+ *
+ *    Author: Andrey Koryagin https://blog.avislab.com/
+ */
+
+#ifndef INC_POSITION_H_
+#define INC_POSITION_H_
+
+void Position_Init (void);
+void Position_Process (void);
+void Position_Set (uint16_t position);
+uint16_t Position_Get ();
+uint16_t Position_Get_Real ();
+
+#endif /* INC_POSITION_H_ */

+ 21 - 0
Hardware/Boards/stm32f103c8t6/examples/example_position2/Inc/user.h

@@ -0,0 +1,21 @@
+/*
+ * user.h
+ *
+ *    Author: Andrey Koryagin https://blog.avislab.com/
+ */
+
+#ifndef INC_USER_H_
+#define INC_USER_H_
+
+void USER_Init(void);
+void USER_While(void);
+void USER_Tasks(void);
+
+
+#define USER_PROTOCOL_ENABLE
+
+#ifdef USER_PROTOCOL_ENABLE
+uint8_t USER_Packet_Handling(void);
+#endif
+
+#endif /* INC_USER_H_ */

+ 67 - 0
Hardware/Boards/stm32f103c8t6/examples/example_position2/Src/position.c

@@ -0,0 +1,67 @@
+/*
+ * position.c
+ *
+ *    Author: Andrey Koryagin https://blog.avislab.com/
+ */
+
+#include "u_foc.h"
+
+#define POSITION_MAX 4095
+
+extern volatile SettingsStruct Settings;
+
+PIDstruct PositionPID;
+
+int32_t POSITION_TASK = 0;
+
+int32_t Position_Calc () {
+	int32_t pos;
+	//pos = (int32_t)MC_ADC_GetRegularValue(ADC_POT) - POSITION_TASK;
+	pos = POSITION_TASK - (int32_t)MC_ADC_GetRegularValue(ADC_POT);
+	if (pos > POSITION_MAX/2) {
+		pos = pos - POSITION_MAX;
+	}
+	if (pos < -POSITION_MAX/2) {
+		pos = POSITION_MAX + pos;
+	}
+	return pos;
+}
+
+void Position_Init () {
+	PositionPID.kp = 50000;
+	PositionPID.ki = 50000;
+	PositionPID.kd = 0;
+	PositionPID.max = Settings.RpmMax;
+	PositionPID.min = -Settings.RpmMax;
+
+	PositionPID.task = 0;
+	PositionPID.input = Position_Calc();
+
+	MC_SetMode(MC_MODE_SPEED);
+	MC_SetSpeed(0);
+}
+
+void Position_Process () {
+	PositionPID.input = Position_Calc();
+	int16_t speed = -(int16_t)PI_Compute(&PositionPID);
+	if ((-200 < speed) & (speed < 200) ) {
+		speed=0;
+	}
+	MC_SetSpeed(speed);
+}
+
+void Position_Set (uint16_t position) {
+	if (position > POSITION_MAX) {
+		position = POSITION_MAX;
+	}
+
+	POSITION_TASK = (int32_t)position;
+}
+
+uint16_t Position_Get () {
+	return (uint16_t)POSITION_TASK;
+}
+
+uint16_t Position_Get_Real () {
+	return (uint16_t) MC_ADC_GetRegularValue(ADC_POT);
+}

+ 71 - 0
Hardware/Boards/stm32f103c8t6/examples/example_position2/Src/user.c

@@ -0,0 +1,71 @@
+/*
+ * user.c
+ *
+ *  Created on: 2 июн. 2023 г.
+ *    Author: Andrey Koryagin https://blog.avislab.com/
+ */
+
+#include "u_foc.h"
+#include "position.h"
+
+extern volatile SettingsStruct Settings;
+
+void USER_Init(void) {
+	MC_ProtocolTimeoutDisable(); // Disable motor stopping on U-FOC Protocol timeout
+	Position_Init();
+}
+
+void USER_While(void) {
+	HAL_Delay(10);
+
+	if (MC_GetState() == MC_STATE_IDLE) {
+		MC_Start();
+	}
+
+	if (MC_GetState() == MC_STATE_FAULT) {
+		MC_ResetFault();
+	}
+}
+
+void USER_Tasks(void) {
+	Position_Process();
+}
+
+
+#ifdef USER_PROTOCOL_ENABLE
+
+#define USER_PACKET_SET_POSITION	60
+#define USER_PACKET_GET_POSITION	61
+
+extern uint8_t PACKET_REQUEST[PACKET_LENGTH];
+extern uint8_t PACKET_RESPONSE[PACKET_LENGTH];
+
+uint8_t USER_Packet_Handling(void) {
+
+	if (PACKET_REQUEST[1] == USER_PACKET_SET_POSITION) { // Set Position
+		int16_t position;
+		position = (PACKET_REQUEST[2]<<8) | PACKET_REQUEST[3];
+		Position_Set(position);
+		return 0;
+	}
+
+	if (PACKET_REQUEST[1] == USER_PACKET_GET_POSITION) { // Get Position
+		PACKET_RESPONSE[0]= Settings.DevID;
+		PACKET_RESPONSE[1]= PACKET_REQUEST[1] + 127;
+
+		uint16_t position;
+		position = Position_Get_Real();
+		PACKET_RESPONSE[2]= ((position & 0x0000FF00) >> 8);
+		PACKET_RESPONSE[3]= (position & 0x000000FF);
+		position = Position_Get();
+		PACKET_RESPONSE[4]= ((position & 0x0000FF00) >> 8);
+		PACKET_RESPONSE[5]= (position & 0x000000FF);
+		PACKET_RESPONSE[6]= 0;
+		PACKET_RESPONSE[7]= 0;
+		return 1;
+	}
+
+	return 0;
+}
+
+#endif

+ 21 - 0
Hardware/Boards/stm32f103c8t6/examples/example_potentiometer/Inc/mc_pot.h

@@ -0,0 +1,21 @@
+/*
+ * mc_pot.h
+ *
+ *    Author: Andrey Koryagin https://blog.avislab.com/
+ */
+
+#ifndef INC_MC_POT_H_
+#define INC_MC_POT_H_
+
+#define POT_BUF_LEN	8
+
+#define POT_START_VALUE	200
+#define POT_STOP_VALUE	100
+#define POT_MAX_VALUE	3800
+
+#define POT_MIN_RPM		120
+#define POT_MAX_RPM		1500
+
+void POT_Process();
+
+#endif /* INC_MC_POT_H_ */

+ 21 - 0
Hardware/Boards/stm32f103c8t6/examples/example_potentiometer/Inc/user.h

@@ -0,0 +1,21 @@
+/*
+ * user.h
+ *
+ *    Author: Andrey Koryagin https://blog.avislab.com/
+ */
+
+#ifndef INC_USER_H_
+#define INC_USER_H_
+
+void USER_Init(void);
+void USER_While(void);
+void USER_Tasks(void);
+
+
+//#define USER_PROTOCOL_ENABLE
+
+#ifdef USER_PROTOCOL_ENABLE
+uint8_t USER_Packet_Handling(void);
+#endif
+
+#endif /* INC_USER_H_ */

+ 55 - 0
Hardware/Boards/stm32f103c8t6/examples/example_potentiometer/Src/mc_pot.c

@@ -0,0 +1,55 @@
+/*
+ * mc_pot.c
+ *
+ *    Author: Andrey Koryagin https://blog.avislab.com/
+ */
+
+#include "u_foc.h"
+
+uint32_t POT_BUF[POT_BUF_LEN];
+
+uint8_t POT_BUF_Index;
+uint32_t POT_AVER;
+uint16_t POT_SPEED;
+
+void POT_Process() {
+	uint8_t i;
+
+	POT_BUF[POT_BUF_Index] = MC_ADC_GetRegularValue(ADC_POT);
+
+	POT_BUF_Index++;
+	if (POT_BUF_Index >= POT_BUF_LEN) {
+		POT_BUF_Index = 0;
+	}
+
+	for (i=0; i<POT_BUF_LEN; i++) {
+		POT_AVER += POT_BUF[i];
+	}
+	POT_AVER = POT_AVER / POT_BUF_LEN;
+
+
+	if (POT_AVER > POT_START_VALUE) {
+		POT_SPEED = POT_MIN_RPM + POT_MAX_RPM * (POT_AVER - POT_START_VALUE) / (POT_MAX_VALUE-POT_START_VALUE);
+		if (POT_SPEED > POT_MAX_RPM) {
+			POT_SPEED = POT_MAX_RPM;
+		}
+
+		if (MC_GetState() == MC_STATE_IDLE) {
+			MC_Start();
+		}
+
+	} else {
+		if (POT_AVER < POT_STOP_VALUE) {
+			POT_SPEED = 0;
+			if (MC_GetState() == MC_STATE_RUN) {
+				MC_Stop();
+			}
+			if (MC_GetState() == MC_STATE_FAULT) {
+				MC_ResetFault();
+			}
+		}
+	}
+
+	MC_SetSpeedRamp(POT_SPEED, 10);
+}
+

+ 38 - 0
Hardware/Boards/stm32f103c8t6/examples/example_potentiometer/Src/user.c

@@ -0,0 +1,38 @@
+/*
+ * user.c
+ *
+ *  Created on: 2 июн. 2023 г.
+ *    Author: Andrey Koryagin https://blog.avislab.com/
+ */
+
+#include "u_foc.h"
+#include "position.h"
+
+extern volatile SettingsStruct Settings;
+
+void USER_Init(void) {
+
+}
+
+void USER_While(void) {
+	HAL_Delay(10);
+	POT_Process();
+}
+
+void USER_Tasks(void) {
+
+}
+
+
+#ifdef USER_PROTOCOL_ENABLE
+
+extern uint8_t PACKET_REQUEST[PACKET_LENGTH];
+extern uint8_t PACKET_RESPONSE[PACKET_LENGTH];
+
+uint8_t USER_Packet_Handling(void) {
+
+
+	return 0;
+}
+
+#endif

BIN
Software/PC-monitor/u-foc-pc-monitor-ubuntu.tar.gz


BIN
Software/PC-monitor/u-foc-pc-monitor-windows.zip