----------------------------------------------------------------------------- -- Name: Motor Encoder Ratios -- Purpose: compute needed sheaves -- Author: Ya-Nvr-No -- Created: 1/13/2015 -- Copyright: (c) 2015 Ya-Nvr-No All rights reserved. -- Licence: BSD license ----------------------------------------------------------------------------- local frame = wx.wxFrame(wx.NULL, wx.wxID_ANY, "Motor Encoder Ratios", wx.wxPoint(125, 125), wx.wxSize(510, 222))--position top/left and the overall size local fileMenu = wx.wxMenu("", wx.wxMENU_TEAROFF) fileMenu:Append(wx.wxID_EXIT, "E&xit\tCtrl-X", "Quit the program") local helpMenu = wx.wxMenu("", wx.wxMENU_TEAROFF) helpMenu:Append(wx.wxID_ABOUT, "&About\tCtrl-A", "About the Motor Encoder Ratios Application") local menuBar = wx.wxMenuBar() menuBar:Append(fileMenu, "&File") menuBar:Append(helpMenu, "&Help") frame:SetMenuBar(menuBar) frame:CreateStatusBar(1) frame:SetStatusText("Welcome to Motor Encoder Ratios Wizard.") frame:Connect(wx.wxID_EXIT, wx.wxEVT_COMMAND_MENU_SELECTED, function (event) frame:Close() end ) frame:Connect(wx.wxID_ABOUT, wx.wxEVT_COMMAND_MENU_SELECTED, function (event) wx.wxMessageBox("Motor Encoder Ratios wizard \n\nAuthor: Ya-Nvr-No\nDate: 1/11/15\nThis wizard is to be used to calculate sheaves needed\n".. wxlua.wxLUA_VERSION_STRING.." built with "..wx.wxVERSION_STRING, "About wxLua", wx.wxOK + wx.wxICON_INFORMATION, frame ) end ) grid = wx.wxGrid(frame, wx.wxID_ANY) grid:EnableDragGridSize( False ) grid:SetMargins( 0, 0 ) grid:EnableDragColSize( True ) grid:SetColLabelSize( 0 ) --grid:SetColLabelAlignment( wx.wxALIGN_CENTER, wx.wxALIGN_CENTER ) -- Rows grid:AutoSizeRows() grid:EnableDragRowSize( True ) grid:SetRowLabelSize( 2 ) --grid:SetRowLabelAlignment( wx.wxALIGN_CENTRE, wx.wxALIGN_CENTRE ) -- Label Appearance -- Cell Defaults grid:SetDefaultCellAlignment( wx.wxALIGN_CENTER, wx.wxALIGN_TOP+wx.wxSTAY_ON_TOP ) grid:CreateGrid(9, 4) --grid:SetColSize(0, 8) grid:SetColSize(1, 170) --grid:SetColSize(2, 8) grid:SetColSize(3, 170) for i = 0 , 9 do grid:SetRowSize(i, 12) end for i = 0 , 3 do grid:SetCellBackgroundColour(i, 0, wx.wxGREEN) end for i = 5 , 6 do grid:SetCellBackgroundColour(i, 0, wx.wxGREEN) end for i = 5 , 6 do grid:SetCellBackgroundColour(i, 1,wx.wxColour(240,240,100)) -- yellow end for i = 0 , 1 do grid:SetCellBackgroundColour(8, i, wx.wxCYAN) end grid:SetCellValue(0, 0, "360") --should remain 360 degrees per revolution grid:SetCellValue(1, 0, "2000") --counts the encouter puts out grid:SetCellValue(2, 0, "100") --drive sheave grid:SetCellValue(3, 0, "100") --driven sheave grid:SetCellValue(5, 0, "5") --desired counts per degree grid:SetCellValue(6, 0, "80") --reference sheave ot compute from --[[ grid:SetCellValue(7, 0, "-") grid:SetCellValue(8, 0, "-") grid:SetCellValue(1, 2, "-") grid:SetCellValue(2, 2, "-") grid:SetCellValue(3, 2, "-") ]] grid:SetCellValue(0, 1, "rotational degrees (360 std)") grid:SetCellValue(1, 1, "encoder counts") grid:SetCellValue(2, 1, "drive sheave") grid:SetCellValue(3, 1, "driven sheave") grid:SetCellValue(5, 1, "Counts per degrees desired") grid:SetCellValue(6, 1, "Reference Sheave") grid:SetCellValue(7, 1, "Ratio computed") grid:SetCellValue(8, 1, "Computed Sheave") grid:SetCellValue(1, 3, "counts per degree no sheaves") grid:SetCellValue(2, 3, "drive sheave ratio") grid:SetCellValue(3, 3, "counts per degree with sheaves") --grid:SetCellFont(0, 0, wx.wxFont(10, wx.wxROMAN, wx.wxITALIC, wx.wxNORMAL)) --grid:SetCellTextColour(1, 1, wx.wxRED) --grid:SetCellBackgroundColour(2, 2, wx.wxCYAN) frame:Show(true) -- show the grid function main() TimerCalc() --call the caculator end function OnClose() gtimer:Stop(); --stop the timer end function TimerCalc() --whereyou do all the computing and updating local degrees=grid:GetCellValue(0, 0) local counts=grid:GetCellValue(1, 0) local drive_sheave=grid:GetCellValue(2, 0) local driven_sheave=grid:GetCellValue(3, 0) local counts_wanted=grid:GetCellValue(5, 0) local ref_sheave=grid:GetCellValue(6, 0) --local ratio_computed=grid:GetCellValue(7, 0) --local computed_sheave=grid:GetCellValue(8, 0) --local computed_counts=grid:GetCellValue(1, 2) --local sheave_ratio=grid:GetCellValue(2, 2) --local sheave_counts_degree=grid:GetCellValue(3, 2) local ratio_computed=((tonumber(counts_wanted))/(((tonumber(counts))/(tonumber(degrees))))) local computed_sheave=((tonumber(ref_sheave))*((tonumber(ratio_computed)))) local computed_counts=((tonumber(counts))/((tonumber(degrees)))) local sheave_ratio=((tonumber(driven_sheave))/((tonumber(drive_sheave)))) local sheave_counts_degree=((tonumber(sheave_ratio))*((tonumber(computed_counts)))) --ratio_computed = string.format("%0.4f",tostring(ratio_computed)) grid:SetCellValue(7, 0, string.format("%0.4f",tostring(ratio_computed))) grid:SetCellValue(8, 0, string.format("%0.1f",tostring(computed_sheave))) grid:SetCellValue(1, 2, string.format("%0.4f",tostring(computed_counts))) grid:SetCellValue(2, 2, string.format("%0.4f",tostring(sheave_ratio))) grid:SetCellValue(3, 2, string.format("%0.4f",tostring(sheave_counts_degree))) grid:ForceRefresh() --update the results event:Skip() end gtimer = wx.wxTimer(frame); --setup a timer frame:Connect(wx.wxEVT_TIMER, TimerCalc); --connect it to the frame gtimer:Start(100); --100 miliseconds frame:Connect(wx.wxEVT_CLOSE_WINDOW, function (event) OnClose() gtimer:Stop(); event:Skip(); end) -- Call wx.wxGetApp():MainLoop() last to start the wxWidgets event loop, -- otherwise the wxLua program will exit immediately. wx.wxGetApp():MainLoop()