Linux device driver stepper motor - device-driver

hi i am new to Device driver, i am writing code to driver a stepper motor using ioctl...my code goes this way
while(is_transmit_empty() ==0);
outb(0x99, SERIAL_PORT_BASE);
printk("data sent\n");
mdelay(500);
while(is_transmit_empty() ==0);
outb(0xCC, SERIAL_PORT_BASE);
printk("data sent\n");
mdelay(500);
while(is_transmit_empty() ==0);
outb(0x66, SERIAL_PORT_BASE);
printk("data sent\n");
mdelay(500);
while(is_transmit_empty() ==0);
outb(0x33, SERIAL_PORT_BASE);
printk("data sent\n");
mdelay(500);
and my
define CLOCKWISE _IO(MAGIC_NO, 0 )...
with this code my stepper motor rotates only one step even though its in a loop for 50 for rotating 360-deg......
tell me where am i going wrong with my code .....

Related

Can't display anything on 16x2 display(I2C Board) with nodemcu?

I'm trying to display text on 16x2 display using a Nodemcu board. I have connected the display using a serial connector to board like below.
Vcc => 3v Pin
GND => G pin
SCL => D1 pin
SDA => D2 pin
This is the code I have tried
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x3F,16,2); // set the LCD address to 0x3F for a 16 chars and 2 line display
void setup()
{
lcd.init(); // initialize the lcd
lcd.backlight();
lcd.setCursor(0,0);
lcd.print("Hello world");
lcd.setCursor(1,0);
lcd.print("ESP32 I2C LCD");
}
void loop() {}
I was able to compile this code successfully and save it to the board using Arduino IDE, but nothing display on the screen, the backlights are working fine.
Does anyone know what is going on?
the example I followed https://www.instructables.com/id/Interface-LCD-Using-NodeMCU/
Try to change
lcd.backlight();
to
lcd.setBacklight((uint8_t)1);

Cannot connect to wi-fi network from NodeMCU board

I am trying to connect to my wifi network on my NodeMCU board. Not sure if it's a hardware or software problem, but I could not find any help on the issue.
I am trying to use this code for connecting to the WiFi:
wifi.setmode(wifi.STATION)
station_cfg={};
station_cfg.ssid="netia9000";
station_cfg.pwd="mywifipassword";
wifi.sta.config(station_cfg)
wifi.sta.connect()
status_of_wifi = wifi.sta.status()
if status_of_wifi == wifi.STA_IDLE then print("IDLE") end;
if status_of_wifi == wifi.STA_CONNECTING then print("CONNECTING") end;
if status_of_wifi == wifi.STA_WRONGPWD then print("WRONG PS") end;
if status_of_wifi == wifi.STA_APNOTFOUND then print("404") end;
if status_of_wifi == wifi.STA_FAIL then print("500") end;
if status_of_wifi == wifi.STA_GOTIP then print("IP GOT") end;
print(wifi.sta.getip())
But on console I can read following:
CONNECTING
nil
I tried to put the wrong data - a WiFi SSID that does not exist, a wrong `password, but no matter what I am still getting the same output: "CONNECTING" and "nil".
I used this code to check for available networks:
wifi.setmode(wifi.STATION)
-- print ap list
function listap(t)
for ssid,v in pairs(t) do
authmode, rssi, bssid, channel =
string.match(v, "(%d),(-?%d+),(%x%x:%x%x:%x%x:%x%x:%x%x:%x%x),(%d+)")
print(ssid,authmode,rssi,bssid,channel)
end
end
wifi.sta.getap(listap)
And this worked perfectly fine. I got on the console:
netia9000 3 -52 e8:11:23:43:bf:a2:8f 10
-- other wi fi networks available nearby --
So it looks like the wifi module is fine and it's a software problem. I wrote the code according to the documentation. At this point I have no idea what's wrong. Any suggestions?
wifi.sta.connect() is not synchronous, so there's no guarantee that the AP would be done connecting by the time your .status() code runs. Indeed, the docs say it should be unnecessary unless .config()'s auto value is set false.
You could, however, add a callback to .config() like this:
function showip(params)
print("Connected to Wifi. Got IP: " .. params.IP)
end
...
station_cfg.got_ip_cb = showip
wifi.sta.config(station_cfg)
Keep in mind that wifi can go up and down all the time. If you need to pounce on connect (one-time or every connect), you really want to register a callback rather than assuming that there will be one constant connection.
The callback will have access to all your globals, so you can store software state there, just make sure you're OK with any possible race conditions you may conjure up (locking/sync is a discussion for another thread).

How can I reset ESP8266 MicroPython after main.py crashes?

I have a NodeMCU ESP8266 board running MicroPython. I'm running a web server on my ESP8266. This is my first IoT project based on one of these boards.
The below is a snippet of the code.
This is being executed within main.py. Every now and then, something causes the code to crash (perhaps timing and request based). When main.py exits, for whatever reason, I'm dropped back at the python CLI.
I'd like for the board to reset when this happens (if there isn't a better way).
What is the best method of restarting/reseting the ESP8266?
addr = socket.getaddrinfo('0.0.0.0', 80)[0][-1]
s = socket.socket()
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind(addr)
s.listen(5)
print('listening on', addr)
while True:
cl, addr = s.accept()
print('client connected from', addr)
cl_file = cl.makefile('rwb', 0)
print("Request:")
while True:
line = cl_file.readline()
print("Line:" , line)
if not line or line == b'\r\n':
print("breaking")
break
if line == b'GET /active HTTP/1.1\r\n':
MicroPython has machine.reset() function to reset a board.
Python (not just MicroPython) uses exception handling to handle errors.
Combining the two, you can easily achieve what you want. For example:
a = 4
b = 2
try:
a / b
except:
machine.reset()
If in the code above you replace value of b with 0, your board will reset. If you think about it for a bit, you probably will find out that it doesn't make much sense - you don't want your board suddenly reset if you just divide by 0 by mistake or otherwise. There're got to be better ways to handle errors! Likewise, you may want to think about your own case and see if resetting the board is really the best choice. If you think that yes, that's fine, just always keep in mind that you programmed your board to suddenly reset. Otherwise, your next question here may be "My board suddenly resets! Why???" ;-)
It may be late for the original question, but the answer I am going to share might help other people. Consider it is not a final solution, but in many scenarios, it may save a day. You can explore your case.
The solution is using the internal scheduling function of MicroPython. since its execution is guaranteed, then its behavior can be used as a tool to mimic a functional watchdog.
Following code will run with given timers and threshold which can be customized in your case, and if the timer reaches its threshold, and the value of wd_buffer is not updated for that time, then the function might be called, and we repeat the process again.
So in order to prevent the ESP getting restarted in this case after 12 sec, you have to in someplace in your code, periodically (shorter than 12 sec or adjust the timer and threshold according to your need) update the value of the Global wd_buffer variable. Hope it helps.
# Simple WD - Global Variable
wd_feeder = 0
wd_buffer = 0
wd_counter = 0
wd_threshold = 4
def wd_checker(calledvalue):
print('watchdog is checking... feeder= {} buffer= {}'.format(wd_feeder, wd_buffer))
global wd_counter
global wd_buffer
global wd_feeder
if wd_feeder == wd_buffer:
print('state is suspicious ... counter is {} incrementing the counter'.format(wd_counter))
wd_counter += 1
else:
wd_counter = 0
wd_feeder = wd_buffer
if wd_counter == wd_threshold:
print('Counter is reached its threshold, following function will be called')
wd_feeder = wd_buffer = wd_counter = 0
machine.reset()
if __name__ == '__main__':
scheduler_wd = machine.Timer(-1)
scheduler_wd.init(period=3000, mode=machine.Timer.PERIODIC, callback=wd_checker)
you could add a while loop checking for the Flash Button (GPIO pin 0) like this:
import machine
pin = machine.Pin(0, machine.Pin.IN, machine.Pin.PULL_UP)
while pin.value():
print('Put your code here...')
print('..this will looping until the Flash button is pressed...')
print('...and then it continues here.')
You could execute your code (which should be outside of the main.py -> other file) from the boot or the main.py. if it drops out it should execute the following code, which could trigger a reset.
You may have to catch the error first.
I hope I helped

NodeMCU WiFi auto connect

I am trying to resolve wifi connectivity using Lua language. I have been combing through the api to find a solution but nothing solid yet. I asked a previous question, dynamically switch between wifi networks and the answer did address the question in the way I asked it, but it didn't accomplish what I expected.
Basically, I have two different networks from two different providers. All I want the ESP8266 12e to do is detect when or if the current network has no internet access and automatically switch to the next network. It must continuously try to connect at say a 3 minute interval until it is successful and not just give up.
For testing purposes I tried this code below. The plan is to use the variable "effectiveRouter" and write some logic to switch based on the current router.
effectiveRouter = nil
function wifiConnect(id,pw)
counter = 0
wifi.sta.config(id,pw)
tmr.alarm(1, 1000, tmr.ALARM_SEMI, function()
counter = counter + 1
if counter < 10 then
if wifi.sta.getip() == nil then
print("NO IP yet! Trying on "..id)
tmr.start(1)
else
print("Connected, IP is "..wifi.sta.getip())
end
end
end)
end
wifiConnect("myNetwork","myPassword")
print(effectiveRouter)
When I run that code, I get effectiveRouter as nil on the console. This tells me that the print statement ran before the method call was complete, print(effectiveRouter). I am very very new to lua as this is my first time with the language. I am certain this boiler plate code must have been done before. Can someone please point me in the right direction? I am open to shifting to the arduino IDE as I already have it set up for the NodeMCU ESP8266. May be I can follow the logic better as I come from a java-OOP background.
I eventually sat down and tested my sketch from the previous answer. Two additional lines and we're good to go...
What I missed is that wifi.sta.config() resets the connection attempts if auto connect == true (which is the default). So, if you call it to connect to AP X while it's in the process of connecting to X it will start from scratch - and thus usually not get an IP before it's called again.
effectiveRouter = nil
counter = 0
wifi.sta.config("dlink", "password1")
tmr.alarm(1, 1000, tmr.ALARM_SEMI, function()
counter = counter + 1
if counter < 30 then
if wifi.sta.getip() == nil then
print("NO IP yet! Keep trying to connect to dlink")
tmr.start(1) -- restart
else
print("Connected to dlink, IP is "..wifi.sta.getip())
effectiveRouter = "dlink"
--startProgram()
end
elseif counter == 30 then
wifi.sta.config("cisco", "password2")
-- there should also be tmr.start(1) in here as suggested in the comment
elseif counter < 60 then
if wifi.sta.getip() == nil then
print("NO IP yet! Keep trying to connect to cisco")
tmr.start(1) -- restart
else
print("Connected to cisco, IP is "..wifi.sta.getip())
effectiveRouter = "cisco"
--startProgram()
end
else
print("Out of options, giving up.")
end
end)
You better to migrate a callback based architecture to be sure that you have successfully connected. Here is doc for it :
https://nodemcu.readthedocs.io/en/master/en/modules/wifi/#wifistaeventmonreg
You can listen for
wifi.STA_GOTIP
And make your custom operations in it. Do not forget to start eventmon.
P.s. I am not able to see your variable effectiveRouter in related function.

levelhelper-spritehelper-corona sdk- object collision

I'm trying to make a game like DoodleJump.
In the level(320x9600), position of an object(bar1_67)(of course there are lots of objects) is (177,263) and objects have physics shape. Requiring the "LevelHelper.LevelHelperLoader", I loaded the level:
...
local loader={}
--loading level
loader.level=LevelHelperLoader:initWithContentOfFile("level1.plhs")
loader.level:instantiateObjects()
loader.level:removeBackgroundColor()
local lhGroup=loader.level:layerWithUniqueName("MAIN_LAYER")
group:insert(lhGroup)
--to scroll the level, and start from the bottom of level
lhGroup.y=-loader.level.lhGameWorldRect.size.height+display.contentHeight
...
After this step, new Y position of the bar1_67 (according to the device screen) is about -9000s.
Then I added the player, requiring the "SpriteHelper.SpriteHelperLoader":
...
sLoader = require("SpriteHelper.SpriteHelperLoader")
local _player={}
_player.player=sLoader:createSpriteWithName("player","sprites","extra.pshs")
_player.player:setReferencePoint(display.centerReferencePoint)
_player.player.x=display.contentWidth*0.5
_player.player.y=display.contentHeight*0.4
...
When starting the simulator, yes level was scrolled, but player collided with something invisible and stayed in the air.
Then I added this code to see whats happening:
...
function aa(self,event)
if(event.phase=="began") then
print(event.other.x, event.other.y, event.other.lhUniqueName)
end
end
_player.player.collision=aa
_player.player:addEventListener( "collision", _player.player )
...
Output is :
177 263 bar1_67
But as I said level was scrolled and bar1_67 is out of screen. What can/should I do?
Please use gamedevhelper.com forum in order to help you with LevelHelper related questions.
I don't know whats going on in this case but would it be possible for you to send me the project to have a look?

Resources