Skip to content

Commit

Permalink
fix for issue Makeblock-official#21: XY plotter sometimes plots spuri…
Browse files Browse the repository at this point in the history
…ous circle/path
  • Loading branch information
shimpe committed Jul 15, 2019
1 parent d965f1c commit fe5c303
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion software/GCodeParser/process_string.ino
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ void process_string(char instruction[], int size)
//look for the number that appears after the char key and return it
double search_string(char key, char instruction[], int string_size)
{
char temp[10] = "";
char temp[11] = "";

for (byte i=0; i<string_size; i++)
{
Expand All @@ -468,6 +468,7 @@ double search_string(char key, char instruction[], int string_size)
i++;
k++;
}
temp[k] = 0;
return strtod(temp, NULL);
}
}
Expand Down

8 comments on commit fe5c303

@TodorBalabanov
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to be done like this?

double search_string(char key, char instruction[], int string_size)
{
	for (byte i=0; i<string_size; i++)
	{
		if (instruction[i] != key)
		{
			continue;
		}

		double value;
		sscanf((instruction+i+1), "%lf", &value);
		
		return value;
	}
	
	return 0;
}

@shimpe
Copy link
Owner Author

@shimpe shimpe commented on fe5c303 Feb 21, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like your code might work as well, but I notice the original code had some limitation of maximum 10 characters which your code seems to be missing.
Not sure how relevant the difference would be in practice though.

@TodorBalabanov
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The variable temp is used as buffer of charters. It is not clear why limit of 10 was used. I do not think that such limit of 10 is good idea.

@shimpe
Copy link
Owner Author

@shimpe shimpe commented on fe5c303 Feb 21, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could e.g. be some limitation in the g-code spec (but I haven't researched the spec in detail and I'm not a specialist at all).

@TodorBalabanov
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not specialist too.

@TodorBalabanov
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did some extra testing and both functions give identical results, but I did tests on a desktop computer not on Arduino.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define byte char

double search_string_1(char key, char instruction[], int string_size)
{
	char temp[11] = "";

	for (byte i=0; i<string_size; i++)
	{
		if (instruction[i] == key)
		{
			i++;      
			int k = 0;
			while (i < string_size && k < 10)
			{
				if (instruction[i] == 0 || instruction[i] == ' ')
					break;

				temp[k] = instruction[i];
				i++;
				k++;
			}
                        temp[k] = 0;
			return strtod(temp, NULL);
		}
	}
	
	return 0;
}

double search_string_2(char key, char instruction[], int string_size)
{
	for (byte i=0; i<string_size; i++)
	{
		if (instruction[i] != key)
		{
			continue;
		}

		double value;
		sscanf((instruction+i+1), "%lf", &value);
		
		return value;
	}
	
	return 0;
}

int main() {
	char instruction[] = "a b c123.456 d e";

	printf("%lf\n", search_string_1('c', instruction, strlen(instruction)));
	printf("%lf\n", search_string_2('c', instruction, strlen(instruction)));

	return 0;
}

@shimpe
Copy link
Owner Author

@shimpe shimpe commented on fe5c303 Feb 22, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI: If you want to play a bit with the plotter, feel free to check another pet project I did: https://github.com/shimpe/squigglificator
I've used it to create some nice drawings, but I'm not entirely sure in what final state I left it - it will probably take until next summer holiday before I can revisit, if ever.

@TodorBalabanov
Copy link

@TodorBalabanov TodorBalabanov commented on fe5c303 Feb 22, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I will check it for sure. I am still fighting to run my Orion based device. It is still not running.

Please sign in to comment.