Isaac Asimov wrote numerous stories about robots - the basic premise was that all robots would be governed by the "three laws":
1. A robot may not injure a human being or, through inaction, allow a human being to come to harm.
2. A robot must obey orders given it by human beings except where such orders would conflict with the First Law.
3. A robot must protect its own existence as long as such protection does not conflict with the First or Second Law.
Now, the European Union is on the verge of enacting legislation to force robot manufacturers to install these laws in all robots: https://www.rt.com/viral/373450-robot-kill-switches-status/
This has been published on Drudge as "Robots get human rights". Nope. They are still our servants.
Left to the manufacturers and programmers is exactly how you make a machine cognizant of "harm to a human"...