Skip to content

Commit 2a0b856

Browse files
committed
updata mycobotservo.c
1 parent 72e14ee commit 2a0b856

File tree

3 files changed

+163
-1
lines changed

3 files changed

+163
-1
lines changed
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
#include "MycobotSaver.h"
2+
3+
MycobotSaver::MycobotSaver()
4+
{
5+
6+
}
7+
8+
9+
void MycobotSaver::listDir(fs::FS &fs, const char * dirname, uint8_t levels){
10+
Serial.printf("Listing directory: %s\r\n", dirname);
11+
12+
File root = fs.open(dirname);
13+
if(!root){
14+
Serial.println("- failed to open directory");
15+
return;
16+
}
17+
if(!root.isDirectory()){
18+
Serial.println(" - not a directory");
19+
return;
20+
}
21+
22+
File file = root.openNextFile(); //less 8 coloumns
23+
while(file){
24+
if(file.isDirectory()){
25+
Serial.print(" DIR : ");
26+
Serial.println(file.name());
27+
if(levels){
28+
listDir(fs, file.name(), levels -1);
29+
}
30+
} else {
31+
Serial.print(" FILE: ");
32+
Serial.print(file.name());
33+
Serial.print("\tSIZE: ");
34+
Serial.println(file.size());
35+
}
36+
file = root.openNextFile();
37+
}
38+
}
39+
40+
41+
void MycobotSaver::writeFile(fs::FS &fs, const char * path, const char * message){
42+
Serial.printf("Writing file: %s\r\n", path);
43+
44+
File file = fs.open(path, FILE_WRITE);
45+
if(!file){
46+
Serial.println("- failed to open file for writing");
47+
return;
48+
}
49+
if(file.print(message)){
50+
Serial.println("- file written");
51+
} else {
52+
Serial.println("- frite failed");
53+
}
54+
}
55+
56+
void MycobotSaver::appendFile(fs::FS &fs, const char * path, const char * message){
57+
58+
File file = fs.open(path, FILE_APPEND);
59+
if(!file){
60+
return;
61+
}
62+
file.print(message);
63+
}
64+
65+
void MycobotSaver::deleteFile(fs::FS &fs, const char * path){
66+
Serial.printf("Deleting file: %s\r\n", path);
67+
if(fs.remove(path)){
68+
Serial.println("- file deleted");
69+
} else {
70+
Serial.println("- delete failed");
71+
}
72+
}
73+
74+
void MycobotSaver::readFile(fs::FS &fs, const char * path){
75+
76+
File file = fs.open(path);
77+
if(!file || file.isDirectory()){
78+
Serial.println("- failed to open file for reading");
79+
return;
80+
}
81+
82+
String this_line = "";
83+
84+
while(file.available()){
85+
char this_char = char(file.read());
86+
this_line += this_char;
87+
if (this_char == '\n')
88+
{
89+
saver_angles_enc jae_this;
90+
jae_this = processStringIntoInts(this_line);
91+
92+
this_line = "";
93+
}
94+
95+
}
96+
97+
}
98+
99+
100+
MycobotSaver::saver_angles_enc MycobotSaver::processStringIntoInts(String string_input)
101+
{
102+
saver_angles_enc sae;
103+
int data_index = 0;
104+
String data_angle_string="";
105+
for (int i = 0; string_input[i] != '\n'; i++) {
106+
107+
if(string_input[i] == ',')
108+
{
109+
if ((data_index < 6)&&(i>1)){
110+
sae.joint_angle[data_index] = data_angle_string.toInt();
111+
}
112+
else
113+
{
114+
break;
115+
}
116+
data_angle_string = "";
117+
data_index ++;
118+
continue;
119+
}
120+
else
121+
{
122+
data_angle_string+=string_input[i];
123+
}
124+
}
125+
return sae;
126+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#ifndef mycobotsaver_h
2+
#define mycobotsaver_h
3+
4+
#include <Arduino.h>
5+
#include "FS.h"
6+
#include "SPIFFS.h"
7+
#define FORMAT_SPIFFS_IF_FAILED true
8+
9+
#define FILENAME "/Angles.txt"
10+
11+
12+
class MycobotSaver
13+
{
14+
public:
15+
16+
MycobotSaver();
17+
18+
typedef struct{
19+
int joint_angle[6];
20+
} saver_angles_enc;
21+
22+
void listDir(fs::FS &fs, const char * dirname, uint8_t levels);
23+
void writeFile(fs::FS &fs, const char * path, const char * message);
24+
void appendFile(fs::FS &fs, const char * path, const char * message);
25+
void deleteFile(fs::FS &fs, const char * path);
26+
void readFile(fs::FS &fs, const char * path);
27+
saver_angles_enc processStringIntoInts(String string_input);
28+
29+
30+
private:
31+
32+
33+
};
34+
35+
36+
#endif

Arduino/MycobotBasic/examples/MainControl/MainControl.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include <MycobotBasic.h>
1+
9#include <MycobotBasic.h>
22

33
MycobotBasic myCobot;
44

0 commit comments

Comments
 (0)