Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- % This function takes in a square positon on the chess board,
- % e.g. 'A2' or 'h8'
- % Returns motor angles based off calibration values
- % +------------------------+
- % |a8 h8|
- % | |
- % | |
- % | |
- % | |
- % |a1 h1 |
- % +------------------------+
- % TODO:
- % If X,Y motor axes not perpendicular and parallel to
- % respective sides of chess board, need to calculate
- % based off the board being diagonal
- function [bigDC, smallDC] = squareToDegrees(spot)
- [ndata, text, alldata] = xlsread('locations.xlsx');
- % Make sure spot is 2 chars long
- if length(spot) ~= 2
- fprintf("Error: square spot not 2 chars.\n");
- return;
- end
- if length(alldata) <= 4
- fprintf("Error reading excel file"\n);
- return;
- end
- % Get ascii values for spot
- ascii = double(spot);
- %% Get shit from excel
- % ascii(2) = 49 for '1', 56 for '8' (-48 gives 1through8)
- row = (((ascii(2) - 48)));
- % ascii(1) = 97 for a, 104 for h (-96 gives 1through8)
- col = (ascii(1) - 96);
- %fprintf("row %d, col %d\n", row, col);
- coord = alldata{row, col};
- %fprintf("%s\n", coord);
- if length(coord) < 4
- % ESTIMATE
- [bigDC, smallDC] = estimateHorizontally(spot);
- %[bigDC, smallDC] = estimateVertically(spot);
- else
- [bigDC, smallDC] = coordToValues(coord);
- end
- %% Change string coordinate to xy vals
- function [x, y] = coordToValues(coord)
- coord = strrep(coord, '(', '');
- coord = strrep(coord, ')', '');
- coord = strrep(coord, ' ', '');
- split = strsplit(coord, ',');
- x = str2double(split{1});
- y = str2double(split{2});
- end
- function [x, y] = estimateHorizontally(spot)
- x = 0; y = 0;
- % get left coord
- lSpot = spot;
- coord = nan;
- while isnan(coord)
- lSpot = strcat(char(lSpot(1) - 1), lSpot(2));
- coord = alldata{lSpot(2) - 48, lSpot(1) - 96};
- end
- [leftX, leftY] = coordToValues(coord);
- % get right coord
- rSpot = spot;
- coord = nan;
- while isnan(coord)
- rSpot = strcat(char(rSpot(1) + 1), rSpot(2));
- coord = alldata{rSpot(2) - 48, rSpot(1) - 96};
- end
- [rightX, rightY] = coordToValues(coord);
- % Line b/t 2
- deltaX = rightX - leftX;
- slopeX = deltaX / (rSpot(1) - lSpot(1));
- x = leftX + slopeX * (spot(1) - lSpot(1));
- deltaY = rightY - leftY;
- slopeY = deltaY / (rSpot(1) - lSpot(1));
- y = leftY + slopeY * (spot(1) - lSpot(1));
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement