By appropriately selecting a command value or an actually measured value as the angular velocity used for friction torque calculation, friction compensation can be effective in both cases when actively operating according to the angular velocity command and passively operating, that is, when being pushed by an external force . Also, after a collision is detected, when the motor rotation direction is opposite to the collision direction, the position control is switched to current control so that the motor generates torque in the opposite direction to the motor rotation direction, thereby reducing the motor rotation speed and weakening the collision energy. Thereafter, when the motor rotation speed becomes the set value or lower, the mode is set to flexible control, thereby eliminating deformation of the speed reduction device or the like caused by the collision. On the other hand, when the motor rotation direction and the collision direction are the same, the position control is directly transferred to the flexibility control without going through the current control. By performing operations according to the collision force, it is possible to weaken the collision torque.